diff options
Diffstat (limited to 'crates/ra_hir/src/docs.rs')
-rw-r--r-- | crates/ra_hir/src/docs.rs | 81 |
1 files changed, 73 insertions, 8 deletions
diff --git a/crates/ra_hir/src/docs.rs b/crates/ra_hir/src/docs.rs index 5db72c08a..1b0f84de5 100644 --- a/crates/ra_hir/src/docs.rs +++ b/crates/ra_hir/src/docs.rs | |||
@@ -1,24 +1,60 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
1 | use ra_syntax::ast; | 3 | use ra_syntax::ast; |
2 | 4 | ||
3 | use crate::HirDatabase; | 5 | use crate::{ |
6 | HirDatabase, DefDatabase, AstDatabase, | ||
7 | Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource, MacroDef, | ||
8 | }; | ||
9 | |||
10 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
11 | pub 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 | MacroDef(MacroDef), | ||
24 | } | ||
25 | |||
26 | impl_froms!( | ||
27 | DocDef: Module, | ||
28 | StructField, | ||
29 | Struct, | ||
30 | Enum, | ||
31 | EnumVariant, | ||
32 | Static, | ||
33 | Const, | ||
34 | Function, | ||
35 | Union, | ||
36 | Trait, | ||
37 | TypeAlias, | ||
38 | MacroDef | ||
39 | ); | ||
4 | 40 | ||
5 | /// Holds documentation | 41 | /// Holds documentation |
6 | #[derive(Debug, Clone)] | 42 | #[derive(Debug, Clone, PartialEq, Eq)] |
7 | pub struct Documentation(String); | 43 | pub struct Documentation(Arc<str>); |
8 | 44 | ||
9 | impl Documentation { | 45 | impl Documentation { |
10 | pub fn new(s: &str) -> Self { | 46 | fn new(s: &str) -> Documentation { |
11 | Self(s.into()) | 47 | Documentation(s.into()) |
12 | } | 48 | } |
13 | 49 | ||
14 | pub fn contents(&self) -> &str { | 50 | pub fn as_str(&self) -> &str { |
15 | &self.0 | 51 | &*self.0 |
16 | } | 52 | } |
17 | } | 53 | } |
18 | 54 | ||
19 | impl Into<String> for Documentation { | 55 | impl Into<String> for Documentation { |
20 | fn into(self) -> String { | 56 | fn into(self) -> String { |
21 | self.contents().into() | 57 | self.as_str().to_owned() |
22 | } | 58 | } |
23 | } | 59 | } |
24 | 60 | ||
@@ -29,3 +65,32 @@ pub trait Docs { | |||
29 | pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> { | 65 | pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> { |
30 | node.doc_comment_text().map(|it| Documentation::new(&it)) | 66 | node.doc_comment_text().map(|it| Documentation::new(&it)) |
31 | } | 67 | } |
68 | |||
69 | pub(crate) fn documentation_query( | ||
70 | db: &(impl DefDatabase + AstDatabase), | ||
71 | def: DocDef, | ||
72 | ) -> Option<Documentation> { | ||
73 | match def { | ||
74 | DocDef::Module(it) => docs_from_ast(&*it.declaration_source(db)?.1), | ||
75 | DocDef::StructField(it) => match it.source(db).1 { | ||
76 | FieldSource::Named(named) => docs_from_ast(&*named), | ||
77 | FieldSource::Pos(..) => return None, | ||
78 | }, | ||
79 | DocDef::Struct(it) => docs_from_ast(&*it.source(db).1), | ||
80 | DocDef::Enum(it) => docs_from_ast(&*it.source(db).1), | ||
81 | DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).1), | ||
82 | DocDef::Static(it) => docs_from_ast(&*it.source(db).1), | ||
83 | DocDef::Const(it) => docs_from_ast(&*it.source(db).1), | ||
84 | DocDef::Function(it) => docs_from_ast(&*it.source(db).1), | ||
85 | DocDef::Union(it) => docs_from_ast(&*it.source(db).1), | ||
86 | DocDef::Trait(it) => docs_from_ast(&*it.source(db).1), | ||
87 | DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).1), | ||
88 | DocDef::MacroDef(it) => docs_from_ast(&*it.source(db).1), | ||
89 | } | ||
90 | } | ||
91 | |||
92 | impl<T: Into<DocDef> + Copy> Docs for T { | ||
93 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { | ||
94 | db.documentation((*self).into()) | ||
95 | } | ||
96 | } | ||