diff options
Diffstat (limited to 'crates/hir_def/src/docs.rs')
-rw-r--r-- | crates/hir_def/src/docs.rs | 48 |
1 files changed, 3 insertions, 45 deletions
diff --git a/crates/hir_def/src/docs.rs b/crates/hir_def/src/docs.rs index 3e59a8f47..6a27effef 100644 --- a/crates/hir_def/src/docs.rs +++ b/crates/hir_def/src/docs.rs | |||
@@ -5,16 +5,9 @@ | |||
5 | 5 | ||
6 | use std::sync::Arc; | 6 | use std::sync::Arc; |
7 | 7 | ||
8 | use either::Either; | ||
9 | use itertools::Itertools; | 8 | use itertools::Itertools; |
10 | use syntax::{ast, SmolStr}; | 9 | use syntax::{ast, SmolStr}; |
11 | 10 | ||
12 | use crate::{ | ||
13 | db::DefDatabase, | ||
14 | src::{HasChildSource, HasSource}, | ||
15 | AdtId, AttrDefId, Lookup, | ||
16 | }; | ||
17 | |||
18 | /// Holds documentation | 11 | /// Holds documentation |
19 | #[derive(Debug, Clone, PartialEq, Eq)] | 12 | #[derive(Debug, Clone, PartialEq, Eq)] |
20 | pub struct Documentation(Arc<str>); | 13 | pub struct Documentation(Arc<str>); |
@@ -26,7 +19,7 @@ impl Into<String> for Documentation { | |||
26 | } | 19 | } |
27 | 20 | ||
28 | impl Documentation { | 21 | impl Documentation { |
29 | fn new(s: &str) -> Documentation { | 22 | pub fn new(s: &str) -> Documentation { |
30 | Documentation(s.into()) | 23 | Documentation(s.into()) |
31 | } | 24 | } |
32 | 25 | ||
@@ -40,42 +33,6 @@ impl Documentation { | |||
40 | pub fn as_str(&self) -> &str { | 33 | pub fn as_str(&self) -> &str { |
41 | &*self.0 | 34 | &*self.0 |
42 | } | 35 | } |
43 | |||
44 | pub(crate) fn documentation_query( | ||
45 | db: &dyn DefDatabase, | ||
46 | def: AttrDefId, | ||
47 | ) -> Option<Documentation> { | ||
48 | match def { | ||
49 | AttrDefId::ModuleId(module) => { | ||
50 | let def_map = db.crate_def_map(module.krate); | ||
51 | let src = def_map[module.local_id].declaration_source(db)?; | ||
52 | docs_from_ast(&src.value) | ||
53 | } | ||
54 | AttrDefId::FieldId(it) => { | ||
55 | let src = it.parent.child_source(db); | ||
56 | match &src.value[it.local_id] { | ||
57 | Either::Left(_tuple) => None, | ||
58 | Either::Right(record) => docs_from_ast(record), | ||
59 | } | ||
60 | } | ||
61 | AttrDefId::AdtId(it) => match it { | ||
62 | AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
63 | AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
64 | AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
65 | }, | ||
66 | AttrDefId::EnumVariantId(it) => { | ||
67 | let src = it.parent.child_source(db); | ||
68 | docs_from_ast(&src.value[it.local_id]) | ||
69 | } | ||
70 | AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
71 | AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db.upcast())), | ||
72 | AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
73 | AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
74 | AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
75 | AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value), | ||
76 | AttrDefId::ImplId(_) => None, | ||
77 | } | ||
78 | } | ||
79 | } | 36 | } |
80 | 37 | ||
81 | pub(crate) fn docs_from_ast<N>(node: &N) -> Option<Documentation> | 38 | pub(crate) fn docs_from_ast<N>(node: &N) -> Option<Documentation> |
@@ -94,7 +51,8 @@ fn merge_doc_comments_and_attrs( | |||
94 | ) -> Option<String> { | 51 | ) -> Option<String> { |
95 | match (doc_comment_text, doc_attr_text) { | 52 | match (doc_comment_text, doc_attr_text) { |
96 | (Some(mut comment_text), Some(attr_text)) => { | 53 | (Some(mut comment_text), Some(attr_text)) => { |
97 | comment_text.push_str("\n"); | 54 | comment_text.reserve(attr_text.len() + 1); |
55 | comment_text.push('\n'); | ||
98 | comment_text.push_str(&attr_text); | 56 | comment_text.push_str(&attr_text); |
99 | Some(comment_text) | 57 | Some(comment_text) |
100 | } | 58 | } |