aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs31
-rw-r--r--crates/ra_hir/src/code_model/docs.rs97
-rw-r--r--crates/ra_hir/src/db.rs17
-rw-r--r--crates/ra_hir/src/from_id.rs25
-rw-r--r--crates/ra_hir/src/lib.rs4
-rw-r--r--crates/ra_hir/src/test_db.rs1
6 files changed, 43 insertions, 132 deletions
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 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3pub(crate) mod src; 3pub(crate) mod src;
4pub(crate) mod docs;
5 4
6use std::sync::Arc; 5use std::sync::Arc;
7 6
@@ -9,11 +8,12 @@ use hir_def::{
9 adt::VariantData, 8 adt::VariantData,
10 body::scope::ExprScopes, 9 body::scope::ExprScopes,
11 builtin_type::BuiltinType, 10 builtin_type::BuiltinType,
11 docs::Documentation,
12 nameres::per_ns::PerNs, 12 nameres::per_ns::PerNs,
13 resolver::{HasResolver, TypeNs}, 13 resolver::{HasResolver, TypeNs},
14 type_ref::TypeRef, 14 type_ref::TypeRef,
15 AdtId, ContainerId, CrateModuleId, EnumVariantId, HasModule, ImplId, LocalEnumVariantId, 15 ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup,
16 LocalStructFieldId, Lookup, ModuleId, StructFieldId, UnionId, 16 ModuleId, UnionId,
17}; 17};
18use hir_expand::{ 18use hir_expand::{
19 diagnostics::DiagnosticSink, 19 diagnostics::DiagnosticSink,
@@ -1024,18 +1024,17 @@ pub trait HasAttrs {
1024 1024
1025impl<T: Into<AttrDef>> HasAttrs for T { 1025impl<T: Into<AttrDef>> HasAttrs for T {
1026 fn attrs(self, db: &impl DefDatabase) -> Attrs { 1026 fn attrs(self, db: &impl DefDatabase) -> Attrs {
1027 let def = self.into(); 1027 let def: AttrDef = self.into();
1028 match def { 1028 db.attrs(def.into())
1029 AttrDef::Module(it) => db.attrs(it.id.into()), 1029 }
1030 AttrDef::StructField(it) => db.attrs(StructFieldId::from(it).into()), 1030}
1031 AttrDef::Adt(it) => db.attrs(AdtId::from(it).into()), 1031
1032 AttrDef::Function(it) => db.attrs(it.id.into()), 1032pub trait Docs {
1033 AttrDef::EnumVariant(it) => db.attrs(EnumVariantId::from(it).into()), 1033 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>;
1034 AttrDef::Static(it) => db.attrs(it.id.into()), 1034}
1035 AttrDef::Const(it) => db.attrs(it.id.into()), 1035impl<T: Into<AttrDef> + Copy> Docs for T {
1036 AttrDef::Trait(it) => db.attrs(it.id.into()), 1036 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
1037 AttrDef::TypeAlias(it) => db.attrs(it.id.into()), 1037 let def: AttrDef = (*self).into();
1038 AttrDef::MacroDef(it) => db.attrs(it.id.into()), 1038 db.documentation(def.into())
1039 }
1040 } 1039 }
1041} 1040}
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 @@
1//! FIXME: write short doc here
2
3use std::sync::Arc;
4
5use ra_syntax::ast;
6
7use crate::{
8 db::{AstDatabase, DefDatabase, HirDatabase},
9 Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
10 Struct, StructField, Trait, TypeAlias, Union,
11};
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum DocDef {
15 Module(Module),
16 StructField(StructField),
17 Adt(Adt),
18 EnumVariant(EnumVariant),
19 Static(Static),
20 Const(Const),
21 Function(Function),
22 Trait(Trait),
23 TypeAlias(TypeAlias),
24 MacroDef(MacroDef),
25}
26
27impl_froms!(
28 DocDef: Module,
29 StructField,
30 Adt(Struct, Enum, Union),
31 EnumVariant,
32 Static,
33 Const,
34 Function,
35 Trait,
36 TypeAlias,
37 MacroDef
38);
39
40/// Holds documentation
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct Documentation(Arc<str>);
43
44impl Documentation {
45 fn new(s: &str) -> Documentation {
46 Documentation(s.into())
47 }
48
49 pub fn as_str(&self) -> &str {
50 &*self.0
51 }
52}
53
54impl Into<String> for Documentation {
55 fn into(self) -> String {
56 self.as_str().to_owned()
57 }
58}
59
60pub trait Docs {
61 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>;
62}
63
64pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> {
65 node.doc_comment_text().map(|it| Documentation::new(&it))
66}
67
68pub(crate) fn documentation_query(
69 db: &(impl DefDatabase + AstDatabase),
70 def: DocDef,
71) -> Option<Documentation> {
72 match def {
73 DocDef::Module(it) => docs_from_ast(&it.declaration_source(db)?.value),
74 DocDef::StructField(it) => match it.source(db).value {
75 FieldSource::Named(named) => docs_from_ast(&named),
76 FieldSource::Pos(..) => None,
77 },
78 DocDef::Adt(it) => match it {
79 Adt::Struct(it) => docs_from_ast(&it.source(db).value),
80 Adt::Enum(it) => docs_from_ast(&it.source(db).value),
81 Adt::Union(it) => docs_from_ast(&it.source(db).value),
82 },
83 DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).value),
84 DocDef::Static(it) => docs_from_ast(&it.source(db).value),
85 DocDef::Const(it) => docs_from_ast(&it.source(db).value),
86 DocDef::Function(it) => docs_from_ast(&it.source(db).value),
87 DocDef::Trait(it) => docs_from_ast(&it.source(db).value),
88 DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).value),
89 DocDef::MacroDef(it) => docs_from_ast(&it.source(db).value),
90 }
91}
92
93impl<T: Into<DocDef> + Copy> Docs for T {
94 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
95 db.documentation((*self).into())
96 }
97}
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;
5use ra_db::salsa; 5use ra_db::salsa;
6 6
7use crate::{ 7use crate::{
8 debug::HirDebugDatabase,
9 ids, 8 ids,
10 ty::{ 9 ty::{
11 method_resolution::CrateImplBlocks, 10 method_resolution::CrateImplBlocks,
@@ -18,23 +17,17 @@ use crate::{
18 17
19pub use hir_def::db::{ 18pub use hir_def::db::{
20 BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery, 19 BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery,
21 DefDatabase2, DefDatabase2Storage, EnumDataQuery, ExprScopesQuery, FunctionDataQuery, 20 DefDatabase2, DefDatabase2Storage, DocumentationQuery, EnumDataQuery, ExprScopesQuery,
22 GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, LangItemQuery, 21 FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage,
23 ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery, StaticDataQuery, 22 LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery,
24 StructDataQuery, TraitDataQuery, TypeAliasDataQuery, 23 StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery,
25}; 24};
26pub use hir_expand::db::{ 25pub use hir_expand::db::{
27 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, 26 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
28 ParseMacroQuery, 27 ParseMacroQuery,
29}; 28};
30 29
31// This database uses `AstDatabase` internally, 30pub use DefDatabase2 as DefDatabase;
32#[salsa::query_group(DefDatabaseStorage)]
33#[salsa::requires(AstDatabase)]
34pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
35 #[salsa::invoke(crate::code_model::docs::documentation_query)]
36 fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
37}
38 31
39#[salsa::query_group(HirDatabaseStorage)] 32#[salsa::query_group(HirDatabaseStorage)]
40#[salsa::requires(salsa::Database)] 33#[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 @@
4//! are splitting the hir. 4//! are splitting the hir.
5 5
6use hir_def::{ 6use hir_def::{
7 AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, 7 AdtId, AssocItemId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
8 ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId, 8 GenericDefId, ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId,
9}; 9};
10 10
11use crate::{ 11use crate::{
12 ty::{CallableDef, TypableDef}, 12 ty::{CallableDef, TypableDef},
13 Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef, 13 Adt, AssocItem, AttrDef, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef,
14 Static, StructField, TypeAlias, VariantDef, 14 ModuleDef, Static, StructField, TypeAlias, VariantDef,
15}; 15};
16 16
17impl From<ra_db::CrateId> for Crate { 17impl From<ra_db::CrateId> for Crate {
@@ -240,3 +240,20 @@ impl From<StructField> for StructFieldId {
240 StructFieldId { parent: def.parent.into(), local_id: def.id } 240 StructFieldId { parent: def.parent.into(), local_id: def.id }
241 } 241 }
242} 242}
243
244impl From<AttrDef> for AttrDefId {
245 fn from(def: AttrDef) -> Self {
246 match def {
247 AttrDef::Module(it) => AttrDefId::ModuleId(it.id),
248 AttrDef::StructField(it) => AttrDefId::StructFieldId(it.into()),
249 AttrDef::Adt(it) => AttrDefId::AdtId(it.into()),
250 AttrDef::Function(it) => AttrDefId::FunctionId(it.id),
251 AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()),
252 AttrDef::Static(it) => AttrDefId::StaticId(it.id),
253 AttrDef::Const(it) => AttrDefId::ConstId(it.id),
254 AttrDef::Trait(it) => AttrDefId::TraitId(it.id),
255 AttrDef::TypeAlias(it) => AttrDefId::TypeAliasId(it.id),
256 AttrDef::MacroDef(it) => AttrDefId::MacroDefId(it.id),
257 }
258 }
259}
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;
50 50
51pub use crate::{ 51pub use crate::{
52 code_model::{ 52 code_model::{
53 docs::{DocDef, Docs, Documentation},
54 src::{HasBodySource, HasSource}, 53 src::{HasBodySource, HasSource},
55 Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Enum, 54 Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum,
56 EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock, 55 EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock,
57 Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, 56 Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField,
58 Trait, TypeAlias, Union, VariantDef, 57 Trait, TypeAlias, Union, VariantDef,
@@ -70,6 +69,7 @@ pub use crate::{
70 69
71pub use hir_def::{ 70pub use hir_def::{
72 builtin_type::BuiltinType, 71 builtin_type::BuiltinType,
72 docs::Documentation,
73 nameres::{per_ns::PerNs, raw::ImportId}, 73 nameres::{per_ns::PerNs, raw::ImportId},
74 path::{Path, PathKind}, 74 path::{Path, PathKind},
75 type_ref::Mutability, 75 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};
14 ra_db::SourceDatabaseStorage, 14 ra_db::SourceDatabaseStorage,
15 db::InternDatabaseStorage, 15 db::InternDatabaseStorage,
16 db::AstDatabaseStorage, 16 db::AstDatabaseStorage,
17 db::DefDatabaseStorage,
18 db::DefDatabase2Storage, 17 db::DefDatabase2Storage,
19 db::HirDatabaseStorage 18 db::HirDatabaseStorage
20)] 19)]