diff options
Diffstat (limited to 'crates/ra_hir/src/code_model/docs.rs')
-rw-r--r-- | crates/ra_hir/src/code_model/docs.rs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/crates/ra_hir/src/code_model/docs.rs b/crates/ra_hir/src/code_model/docs.rs new file mode 100644 index 000000000..da2b9b854 --- /dev/null +++ b/crates/ra_hir/src/code_model/docs.rs | |||
@@ -0,0 +1,96 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use ra_syntax::ast; | ||
4 | |||
5 | use crate::{ | ||
6 | HirDatabase, DefDatabase, AstDatabase, HasSource, | ||
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 | ); | ||
40 | |||
41 | /// Holds documentation | ||
42 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
43 | pub struct Documentation(Arc<str>); | ||
44 | |||
45 | impl Documentation { | ||
46 | fn new(s: &str) -> Documentation { | ||
47 | Documentation(s.into()) | ||
48 | } | ||
49 | |||
50 | pub fn as_str(&self) -> &str { | ||
51 | &*self.0 | ||
52 | } | ||
53 | } | ||
54 | |||
55 | impl Into<String> for Documentation { | ||
56 | fn into(self) -> String { | ||
57 | self.as_str().to_owned() | ||
58 | } | ||
59 | } | ||
60 | |||
61 | pub trait Docs { | ||
62 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>; | ||
63 | } | ||
64 | |||
65 | pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> { | ||
66 | node.doc_comment_text().map(|it| Documentation::new(&it)) | ||
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)?.ast), | ||
75 | DocDef::StructField(it) => match it.source(db).ast { | ||
76 | FieldSource::Named(named) => docs_from_ast(&*named), | ||
77 | FieldSource::Pos(..) => return None, | ||
78 | }, | ||
79 | DocDef::Struct(it) => docs_from_ast(&*it.source(db).ast), | ||
80 | DocDef::Enum(it) => docs_from_ast(&*it.source(db).ast), | ||
81 | DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).ast), | ||
82 | DocDef::Static(it) => docs_from_ast(&*it.source(db).ast), | ||
83 | DocDef::Const(it) => docs_from_ast(&*it.source(db).ast), | ||
84 | DocDef::Function(it) => docs_from_ast(&*it.source(db).ast), | ||
85 | DocDef::Union(it) => docs_from_ast(&*it.source(db).ast), | ||
86 | DocDef::Trait(it) => docs_from_ast(&*it.source(db).ast), | ||
87 | DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).ast), | ||
88 | DocDef::MacroDef(it) => docs_from_ast(&*it.source(db).ast), | ||
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 | } | ||