aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-06-08 12:36:39 +0100
committerAleksey Kladov <[email protected]>2019-06-08 12:36:39 +0100
commit2c28f5245dd8ede715d99b55fdad0ceb3de9dbe7 (patch)
tree442fe94012c14d52143290b11af1161465a9d41a /crates
parent33026c654e3a667e25ea27004c22be138ed83d33 (diff)
make documenation a query
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs2
-rw-r--r--crates/ra_hir/src/db.rs3
-rw-r--r--crates/ra_hir/src/docs.rs123
3 files changed, 62 insertions, 66 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 861d6aefe..cf16ed94d 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -196,7 +196,7 @@ impl Module {
196 /// `None` for the crate root. 196 /// `None` for the crate root.
197 pub fn declaration_source( 197 pub fn declaration_source(
198 self, 198 self,
199 db: &impl HirDatabase, 199 db: &(impl DefDatabase + AstDatabase),
200 ) -> Option<(HirFileId, TreeArc<ast::Module>)> { 200 ) -> Option<(HirFileId, TreeArc<ast::Module>)> {
201 let def_map = db.crate_def_map(self.krate); 201 let def_map = db.crate_def_map(self.krate);
202 let decl = def_map[self.module_id].declaration?; 202 let decl = def_map[self.module_id].declaration?;
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 3afd0994c..d2a372f3b 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -127,6 +127,9 @@ pub trait DefDatabase: SourceDatabase {
127 127
128 #[salsa::invoke(crate::lang_item::LangItems::lang_item_query)] 128 #[salsa::invoke(crate::lang_item::LangItems::lang_item_query)]
129 fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>; 129 fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>;
130
131 #[salsa::invoke(crate::docs::documentation_query)]
132 fn documentation(&self, def: crate::docs::DocDef) -> Option<crate::docs::Documentation>;
130} 133}
131 134
132#[salsa::query_group(HirDatabaseStorage)] 135#[salsa::query_group(HirDatabaseStorage)]
diff --git a/crates/ra_hir/src/docs.rs b/crates/ra_hir/src/docs.rs
index 38c0922eb..900fd2aa8 100644
--- a/crates/ra_hir/src/docs.rs
+++ b/crates/ra_hir/src/docs.rs
@@ -1,10 +1,44 @@
1use std::sync::Arc;
2
1use ra_syntax::ast; 3use ra_syntax::ast;
2 4
3use crate::{HirDatabase, Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource}; 5use crate::{
6 HirDatabase, DefDatabase, AstDatabase,
7 Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource
8};
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub enum DocDef {
12 Module(Module),
13 StructField(StructField),
14 Struct(Struct),
15 Enum(Enum),
16 EnumVariant(EnumVariant),
17 Static(Static),
18 Const(Const),
19 Function(Function),
20 Union(Union),
21 Trait(Trait),
22 TypeAlias(TypeAlias),
23}
24
25impl_froms!(
26 DocDef: Module,
27 StructField,
28 Struct,
29 Enum,
30 EnumVariant,
31 Static,
32 Const,
33 Function,
34 Union,
35 Trait,
36 TypeAlias
37);
4 38
5/// Holds documentation 39/// Holds documentation
6#[derive(Debug, Clone)] 40#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Documentation(String); 41pub struct Documentation(Arc<str>);
8 42
9impl Documentation { 43impl Documentation {
10 fn new(s: &str) -> Documentation { 44 fn new(s: &str) -> Documentation {
@@ -12,13 +46,13 @@ impl Documentation {
12 } 46 }
13 47
14 pub fn as_str(&self) -> &str { 48 pub fn as_str(&self) -> &str {
15 &self.0 49 &*self.0
16 } 50 }
17} 51}
18 52
19impl Into<String> for Documentation { 53impl Into<String> for Documentation {
20 fn into(self) -> String { 54 fn into(self) -> String {
21 self.0.clone() 55 self.as_str().to_owned()
22 } 56 }
23} 57}
24 58
@@ -30,71 +64,30 @@ pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documen
30 node.doc_comment_text().map(|it| Documentation::new(&it)) 64 node.doc_comment_text().map(|it| Documentation::new(&it))
31} 65}
32 66
33impl Docs for Module { 67pub(crate) fn documentation_query(
34 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { 68 db: &(impl DefDatabase + AstDatabase),
35 self.declaration_source(db).and_then(|it| docs_from_ast(&*it.1)) 69 def: DocDef,
36 } 70) -> Option<Documentation> {
37} 71 match def {
38 72 DocDef::Module(it) => docs_from_ast(&*it.declaration_source(db)?.1),
39impl Docs for StructField { 73 DocDef::StructField(it) => match it.source(db).1 {
40 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
41 match self.source(db).1 {
42 FieldSource::Named(named) => docs_from_ast(&*named), 74 FieldSource::Named(named) => docs_from_ast(&*named),
43 FieldSource::Pos(..) => return None, 75 FieldSource::Pos(..) => return None,
44 } 76 },
45 } 77 DocDef::Struct(it) => docs_from_ast(&*it.source(db).1),
46} 78 DocDef::Enum(it) => docs_from_ast(&*it.source(db).1),
47 79 DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).1),
48impl Docs for Struct { 80 DocDef::Static(it) => docs_from_ast(&*it.source(db).1),
49 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { 81 DocDef::Const(it) => docs_from_ast(&*it.source(db).1),
50 docs_from_ast(&*self.source(db).1) 82 DocDef::Function(it) => docs_from_ast(&*it.source(db).1),
51 } 83 DocDef::Union(it) => docs_from_ast(&*it.source(db).1),
52} 84 DocDef::Trait(it) => docs_from_ast(&*it.source(db).1),
53 85 DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).1),
54impl Docs for Union {
55 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
56 docs_from_ast(&*self.source(db).1)
57 }
58}
59
60impl Docs for Enum {
61 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
62 docs_from_ast(&*self.source(db).1)
63 }
64}
65
66impl Docs for EnumVariant {
67 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
68 docs_from_ast(&*self.source(db).1)
69 }
70}
71
72impl Docs for Function {
73 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
74 docs_from_ast(&*self.source(db).1)
75 }
76}
77
78impl Docs for Const {
79 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
80 docs_from_ast(&*self.source(db).1)
81 }
82}
83
84impl Docs for Static {
85 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
86 docs_from_ast(&*self.source(db).1)
87 }
88}
89
90impl Docs for Trait {
91 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
92 docs_from_ast(&*self.source(db).1)
93 } 86 }
94} 87}
95 88
96impl Docs for TypeAlias { 89impl<T: Into<DocDef> + Copy> Docs for T {
97 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { 90 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
98 docs_from_ast(&*self.source(db).1) 91 db.documentation((*self).into())
99 } 92 }
100} 93}