From 958862093e83083b188427246323047a2c9e7bab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:43:38 +0300 Subject: [PATCH] Move docs to hir_def --- crates/ra_assists/src/test_db.rs | 1 - crates/ra_hir/src/code_model.rs | 31 +++++---- crates/ra_hir/src/code_model/docs.rs | 97 ---------------------------- crates/ra_hir/src/db.rs | 17 ++--- crates/ra_hir/src/from_id.rs | 25 +++++-- crates/ra_hir/src/lib.rs | 4 +- crates/ra_hir/src/test_db.rs | 1 - crates/ra_hir_def/src/db.rs | 6 ++ crates/ra_hir_def/src/docs.rs | 68 +++++++++++++++++++ crates/ra_hir_def/src/lib.rs | 1 + crates/ra_ide_api/src/db.rs | 1 - 11 files changed, 118 insertions(+), 134 deletions(-) delete mode 100644 crates/ra_hir/src/code_model/docs.rs create mode 100644 crates/ra_hir_def/src/docs.rs diff --git a/crates/ra_assists/src/test_db.rs b/crates/ra_assists/src/test_db.rs index 5be7383edcd..715270344a7 100644 --- a/crates/ra_assists/src/test_db.rs +++ b/crates/ra_assists/src/test_db.rs @@ -9,7 +9,6 @@ ra_db::SourceDatabaseStorage, hir::db::InternDatabaseStorage, hir::db::AstDatabaseStorage, - hir::db::DefDatabaseStorage, hir::db::DefDatabase2Storage, hir::db::HirDatabaseStorage )] diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 496b4ee8a4e..4b3ec5457d5 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1,7 +1,6 @@ //! FIXME: write short doc here pub(crate) mod src; -pub(crate) mod docs; use std::sync::Arc; @@ -9,11 +8,12 @@ adt::VariantData, body::scope::ExprScopes, builtin_type::BuiltinType, + docs::Documentation, nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, - AdtId, ContainerId, CrateModuleId, EnumVariantId, HasModule, ImplId, LocalEnumVariantId, - LocalStructFieldId, Lookup, ModuleId, StructFieldId, UnionId, + ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, + ModuleId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -1024,18 +1024,17 @@ pub trait HasAttrs { impl> HasAttrs for T { fn attrs(self, db: &impl DefDatabase) -> Attrs { - let def = self.into(); - match def { - AttrDef::Module(it) => db.attrs(it.id.into()), - AttrDef::StructField(it) => db.attrs(StructFieldId::from(it).into()), - AttrDef::Adt(it) => db.attrs(AdtId::from(it).into()), - AttrDef::Function(it) => db.attrs(it.id.into()), - AttrDef::EnumVariant(it) => db.attrs(EnumVariantId::from(it).into()), - AttrDef::Static(it) => db.attrs(it.id.into()), - AttrDef::Const(it) => db.attrs(it.id.into()), - AttrDef::Trait(it) => db.attrs(it.id.into()), - AttrDef::TypeAlias(it) => db.attrs(it.id.into()), - AttrDef::MacroDef(it) => db.attrs(it.id.into()), - } + let def: AttrDef = self.into(); + db.attrs(def.into()) + } +} + +pub trait Docs { + fn docs(&self, db: &impl HirDatabase) -> Option; +} +impl + Copy> Docs for T { + fn docs(&self, db: &impl HirDatabase) -> Option { + let def: AttrDef = (*self).into(); + db.documentation(def.into()) } } diff --git a/crates/ra_hir/src/code_model/docs.rs b/crates/ra_hir/src/code_model/docs.rs deleted file mode 100644 index e40efef34ae..00000000000 --- a/crates/ra_hir/src/code_model/docs.rs +++ /dev/null @@ -1,97 +0,0 @@ -//! FIXME: write short doc here - -use std::sync::Arc; - -use ra_syntax::ast; - -use crate::{ - db::{AstDatabase, DefDatabase, HirDatabase}, - Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static, - Struct, StructField, Trait, TypeAlias, Union, -}; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum DocDef { - Module(Module), - StructField(StructField), - Adt(Adt), - EnumVariant(EnumVariant), - Static(Static), - Const(Const), - Function(Function), - Trait(Trait), - TypeAlias(TypeAlias), - MacroDef(MacroDef), -} - -impl_froms!( - DocDef: Module, - StructField, - Adt(Struct, Enum, Union), - EnumVariant, - Static, - Const, - Function, - Trait, - TypeAlias, - MacroDef -); - -/// Holds documentation -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Documentation(Arc); - -impl Documentation { - fn new(s: &str) -> Documentation { - Documentation(s.into()) - } - - pub fn as_str(&self) -> &str { - &*self.0 - } -} - -impl Into for Documentation { - fn into(self) -> String { - self.as_str().to_owned() - } -} - -pub trait Docs { - fn docs(&self, db: &impl HirDatabase) -> Option; -} - -pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option { - node.doc_comment_text().map(|it| Documentation::new(&it)) -} - -pub(crate) fn documentation_query( - db: &(impl DefDatabase + AstDatabase), - def: DocDef, -) -> Option { - match def { - DocDef::Module(it) => docs_from_ast(&it.declaration_source(db)?.value), - DocDef::StructField(it) => match it.source(db).value { - FieldSource::Named(named) => docs_from_ast(&named), - FieldSource::Pos(..) => None, - }, - DocDef::Adt(it) => match it { - Adt::Struct(it) => docs_from_ast(&it.source(db).value), - Adt::Enum(it) => docs_from_ast(&it.source(db).value), - Adt::Union(it) => docs_from_ast(&it.source(db).value), - }, - DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).value), - DocDef::Static(it) => docs_from_ast(&it.source(db).value), - DocDef::Const(it) => docs_from_ast(&it.source(db).value), - DocDef::Function(it) => docs_from_ast(&it.source(db).value), - DocDef::Trait(it) => docs_from_ast(&it.source(db).value), - DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).value), - DocDef::MacroDef(it) => docs_from_ast(&it.source(db).value), - } -} - -impl + Copy> Docs for T { - fn docs(&self, db: &impl HirDatabase) -> Option { - db.documentation((*self).into()) - } -} diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index cae305f9852..2dca0f637ed 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -5,7 +5,6 @@ use ra_db::salsa; use crate::{ - debug::HirDebugDatabase, ids, ty::{ method_resolution::CrateImplBlocks, @@ -18,23 +17,17 @@ pub use hir_def::db::{ BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery, - DefDatabase2, DefDatabase2Storage, EnumDataQuery, ExprScopesQuery, FunctionDataQuery, - GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, LangItemQuery, - ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery, StaticDataQuery, - StructDataQuery, TraitDataQuery, TypeAliasDataQuery, + DefDatabase2, DefDatabase2Storage, DocumentationQuery, EnumDataQuery, ExprScopesQuery, + FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, + LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery, + StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery, }; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, }; -// This database uses `AstDatabase` internally, -#[salsa::query_group(DefDatabaseStorage)] -#[salsa::requires(AstDatabase)] -pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { - #[salsa::invoke(crate::code_model::docs::documentation_query)] - fn documentation(&self, def: crate::DocDef) -> Option; -} +pub use DefDatabase2 as DefDatabase; #[salsa::query_group(HirDatabaseStorage)] #[salsa::requires(salsa::Database)] diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index 5d7af00ab98..529ac825172 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -4,14 +4,14 @@ //! are splitting the hir. use hir_def::{ - AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, - ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId, + AdtId, AssocItemId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, + GenericDefId, ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId, }; use crate::{ ty::{CallableDef, TypableDef}, - Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef, - Static, StructField, TypeAlias, VariantDef, + Adt, AssocItem, AttrDef, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, + ModuleDef, Static, StructField, TypeAlias, VariantDef, }; impl From for Crate { @@ -240,3 +240,20 @@ fn from(def: StructField) -> Self { StructFieldId { parent: def.parent.into(), local_id: def.id } } } + +impl From for AttrDefId { + fn from(def: AttrDef) -> Self { + match def { + AttrDef::Module(it) => AttrDefId::ModuleId(it.id), + AttrDef::StructField(it) => AttrDefId::StructFieldId(it.into()), + AttrDef::Adt(it) => AttrDefId::AdtId(it.into()), + AttrDef::Function(it) => AttrDefId::FunctionId(it.id), + AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()), + AttrDef::Static(it) => AttrDefId::StaticId(it.id), + AttrDef::Const(it) => AttrDefId::ConstId(it.id), + AttrDef::Trait(it) => AttrDefId::TraitId(it.id), + AttrDef::TypeAlias(it) => AttrDefId::TypeAliasId(it.id), + AttrDef::MacroDef(it) => AttrDefId::MacroDefId(it.id), + } + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 22da05a970e..868df5b6b4b 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -50,9 +50,8 @@ fn from(it: $sv) -> $e { pub use crate::{ code_model::{ - docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource}, - Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Enum, + Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, @@ -70,6 +69,7 @@ fn from(it: $sv) -> $e { pub use hir_def::{ builtin_type::BuiltinType, + docs::Documentation, nameres::{per_ns::PerNs, raw::ImportId}, path::{Path, PathKind}, type_ref::Mutability, diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs index 1caa2e8759d..c369a80d476 100644 --- a/crates/ra_hir/src/test_db.rs +++ b/crates/ra_hir/src/test_db.rs @@ -14,7 +14,6 @@ ra_db::SourceDatabaseStorage, db::InternDatabaseStorage, db::AstDatabaseStorage, - db::DefDatabaseStorage, db::DefDatabase2Storage, db::HirDatabaseStorage )] diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index e87bd525a30..1481868d0bc 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -10,6 +10,7 @@ attr::Attrs, body::{scope::ExprScopes, Body, BodySourceMap}, data::{ConstData, FunctionData, ImplData, TraitData, TypeAliasData}, + docs::Documentation, generics::GenericParams, lang_item::{LangItemTarget, LangItems}, nameres::{ @@ -101,4 +102,9 @@ fn raw_items_with_source_map( #[salsa::invoke(LangItems::lang_item_query)] fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option; + + // FIXME(https://github.com/rust-analyzer/rust-analyzer/issues/2148#issuecomment-550519102) + // Remove this query completely, in favor of `Attrs::docs` method + #[salsa::invoke(Documentation::documentation_query)] + fn documentation(&self, def: AttrDefId) -> Option; } diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs new file mode 100644 index 00000000000..1b5c8543741 --- /dev/null +++ b/crates/ra_hir_def/src/docs.rs @@ -0,0 +1,68 @@ +//! FIXME: write short doc here + +use std::sync::Arc; + +use hir_expand::either::Either; +use ra_syntax::ast; + +use crate::{db::DefDatabase2, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup}; + +/// Holds documentation +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Documentation(Arc); + +impl Into for Documentation { + fn into(self) -> String { + self.as_str().to_owned() + } +} + +impl Documentation { + fn new(s: &str) -> Documentation { + Documentation(s.into()) + } + + pub fn as_str(&self) -> &str { + &*self.0 + } + + pub(crate) fn documentation_query( + db: &impl DefDatabase2, + def: AttrDefId, + ) -> Option { + match def { + AttrDefId::ModuleId(module) => { + let def_map = db.crate_def_map(module.krate); + let src = def_map[module.module_id].declaration_source(db)?; + docs_from_ast(&src.value) + } + AttrDefId::StructFieldId(it) => { + let src = it.parent.child_source(db); + match &src.value[it.local_id] { + Either::A(_tuple) => None, + Either::B(record) => docs_from_ast(record), + } + } + AttrDefId::AdtId(it) => match it { + AdtId::StructId(it) => docs_from_ast(&it.0.source(db).value), + AdtId::EnumId(it) => docs_from_ast(&it.source(db).value), + AdtId::UnionId(it) => docs_from_ast(&it.0.source(db).value), + }, + AttrDefId::EnumVariantId(it) => { + let src = it.parent.child_source(db); + docs_from_ast(&src.value[it.local_id]) + } + AttrDefId::StaticId(it) => docs_from_ast(&it.source(db).value), + AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value), + AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id.to_node(db)), + AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), + AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value), + AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value), + AttrDefId::ImplId(_) => None, + } + } +} + +pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option { + node.doc_comment_text().map(|it| Documentation::new(&it)) +} diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 899510be4bb..1ba7c7ee387 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -20,6 +20,7 @@ pub mod resolver; pub mod data; pub mod lang_item; +pub mod docs; mod trace; diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index c96465b6a3d..7d35d213756 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -21,7 +21,6 @@ symbol_index::SymbolsDatabaseStorage, hir::db::InternDatabaseStorage, hir::db::AstDatabaseStorage, - hir::db::DefDatabaseStorage, hir::db::DefDatabase2Storage, hir::db::HirDatabaseStorage )]