From 958862093e83083b188427246323047a2c9e7bab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:43:38 +0300 Subject: Move docs to hir_def --- 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 - 6 files changed, 43 insertions(+), 132 deletions(-) delete mode 100644 crates/ra_hir/src/code_model/docs.rs (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 496b4ee8a..4b3ec5457 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 @@ use hir_def::{ 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 e40efef34..000000000 --- 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 cae305f98..2dca0f637 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -5,7 +5,6 @@ use std::sync::Arc; use ra_db::salsa; use crate::{ - debug::HirDebugDatabase, ids, ty::{ method_resolution::CrateImplBlocks, @@ -18,23 +17,17 @@ use crate::{ 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 5d7af00ab..529ac8251 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 @@ impl From for StructFieldId { 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 22da05a97..868df5b6b 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -50,9 +50,8 @@ mod marks; 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 @@ pub use crate::{ 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 1caa2e875..c369a80d4 100644 --- a/crates/ra_hir/src/test_db.rs +++ b/crates/ra_hir/src/test_db.rs @@ -14,7 +14,6 @@ use crate::{db, debug::HirDebugHelper}; ra_db::SourceDatabaseStorage, db::InternDatabaseStorage, db::AstDatabaseStorage, - db::DefDatabaseStorage, db::DefDatabase2Storage, db::HirDatabaseStorage )] -- cgit v1.2.3 From fc1e543f7abb69b8cab308410fa0a127950ee1c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:44:43 +0300 Subject: Get rid of DefDatabase2 --- crates/ra_hir/src/db.rs | 4 +--- crates/ra_hir/src/test_db.rs | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 2dca0f637..399101b83 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -17,7 +17,7 @@ use crate::{ pub use hir_def::db::{ BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery, - DefDatabase2, DefDatabase2Storage, DocumentationQuery, EnumDataQuery, ExprScopesQuery, + DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery, StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery, @@ -27,8 +27,6 @@ pub use hir_expand::db::{ ParseMacroQuery, }; -pub use DefDatabase2 as DefDatabase; - #[salsa::query_group(HirDatabaseStorage)] #[salsa::requires(salsa::Database)] pub trait HirDatabase: DefDatabase + AstDatabase { diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs index c369a80d4..03c7ac155 100644 --- a/crates/ra_hir/src/test_db.rs +++ b/crates/ra_hir/src/test_db.rs @@ -2,7 +2,7 @@ use std::{panic, sync::Arc}; -use hir_def::{db::DefDatabase2, ModuleId}; +use hir_def::{db::DefDatabase, ModuleId}; use hir_expand::diagnostics::DiagnosticSink; use parking_lot::Mutex; use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase}; @@ -14,7 +14,7 @@ use crate::{db, debug::HirDebugHelper}; ra_db::SourceDatabaseStorage, db::InternDatabaseStorage, db::AstDatabaseStorage, - db::DefDatabase2Storage, + db::DefDatabaseStorage, db::HirDatabaseStorage )] #[derive(Debug, Default)] -- cgit v1.2.3 From 32658a73b4c9078421a1cab92ec459bb5b7236f5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:45:37 +0300 Subject: Note that debugging infra is broken --- crates/ra_hir/src/debug.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/debug.rs b/crates/ra_hir/src/debug.rs index 4f3e922c3..8ec371f6e 100644 --- a/crates/ra_hir/src/debug.rs +++ b/crates/ra_hir/src/debug.rs @@ -1,3 +1,5 @@ +//! XXX: This does not work at the moment. +//! //! printf debugging infrastructure for rust-analyzer. //! //! When you print a hir type, like a module, using `eprintln!("{:?}", module)`, -- cgit v1.2.3