diff options
author | Aleksey Kladov <[email protected]> | 2019-01-05 14:33:31 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-05 14:33:31 +0000 |
commit | 9f44d4c56d51fdae1ff073df261b8c897b27c824 (patch) | |
tree | f2b248616c5d31d5d4e48c6fe5969cbd7594b477 /crates/ra_analysis/src/hover.rs | |
parent | 3ad0037f907778d20ce6cfd9bf676a467b5734ad (diff) |
fold doc_comment into hover
Diffstat (limited to 'crates/ra_analysis/src/hover.rs')
-rw-r--r-- | crates/ra_analysis/src/hover.rs | 111 |
1 files changed, 108 insertions, 3 deletions
diff --git a/crates/ra_analysis/src/hover.rs b/crates/ra_analysis/src/hover.rs index c3825f6ea..c99d87da6 100644 --- a/crates/ra_analysis/src/hover.rs +++ b/crates/ra_analysis/src/hover.rs | |||
@@ -1,7 +1,11 @@ | |||
1 | use ra_db::{Cancelable, SyntaxDatabase}; | 1 | use ra_db::{Cancelable, SyntaxDatabase}; |
2 | use ra_syntax::{ast, AstNode}; | 2 | use ra_syntax::{ |
3 | AstNode, SyntaxNode, | ||
4 | ast::{self, NameOwner}, | ||
5 | algo::visit::{visitor, Visitor}, | ||
6 | }; | ||
3 | 7 | ||
4 | use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange}; | 8 | use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget}; |
5 | 9 | ||
6 | pub(crate) fn hover( | 10 | pub(crate) fn hover( |
7 | db: &RootDatabase, | 11 | db: &RootDatabase, |
@@ -10,7 +14,7 @@ pub(crate) fn hover( | |||
10 | let mut res = Vec::new(); | 14 | let mut res = Vec::new(); |
11 | let range = if let Some(rr) = db.approximately_resolve_symbol(position)? { | 15 | let range = if let Some(rr) = db.approximately_resolve_symbol(position)? { |
12 | for nav in rr.resolves_to { | 16 | for nav in rr.resolves_to { |
13 | res.extend(db.doc_text_for(nav)?) | 17 | res.extend(doc_text_for(db, nav)?) |
14 | } | 18 | } |
15 | rr.reference_range | 19 | rr.reference_range |
16 | } else { | 20 | } else { |
@@ -33,6 +37,107 @@ pub(crate) fn hover( | |||
33 | Ok(Some(res)) | 37 | Ok(Some(res)) |
34 | } | 38 | } |
35 | 39 | ||
40 | // FIXME: this should not really use navigation target. Rather, approximatelly | ||
41 | // resovled symbol should return a `DefId`. | ||
42 | fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Cancelable<Option<String>> { | ||
43 | let result = match (nav.description(db), nav.docs(db)) { | ||
44 | (Some(desc), Some(docs)) => Some("```rust\n".to_string() + &*desc + "\n```\n\n" + &*docs), | ||
45 | (Some(desc), None) => Some("```rust\n".to_string() + &*desc + "\n```"), | ||
46 | (None, Some(docs)) => Some(docs), | ||
47 | _ => None, | ||
48 | }; | ||
49 | |||
50 | Ok(result) | ||
51 | } | ||
52 | |||
53 | impl NavigationTarget { | ||
54 | fn node(&self, db: &RootDatabase) -> Option<SyntaxNode> { | ||
55 | let source_file = db.source_file(self.file_id); | ||
56 | let source_file = source_file.syntax(); | ||
57 | let node = source_file | ||
58 | .descendants() | ||
59 | .find(|node| node.kind() == self.kind && node.range() == self.range)? | ||
60 | .owned(); | ||
61 | Some(node) | ||
62 | } | ||
63 | |||
64 | fn docs(&self, db: &RootDatabase) -> Option<String> { | ||
65 | let node = self.node(db)?; | ||
66 | let node = node.borrowed(); | ||
67 | fn doc_comments<'a, N: ast::DocCommentsOwner<'a>>(node: N) -> Option<String> { | ||
68 | let comments = node.doc_comment_text(); | ||
69 | if comments.is_empty() { | ||
70 | None | ||
71 | } else { | ||
72 | Some(comments) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | visitor() | ||
77 | .visit(doc_comments::<ast::FnDef>) | ||
78 | .visit(doc_comments::<ast::StructDef>) | ||
79 | .visit(doc_comments::<ast::EnumDef>) | ||
80 | .visit(doc_comments::<ast::TraitDef>) | ||
81 | .visit(doc_comments::<ast::Module>) | ||
82 | .visit(doc_comments::<ast::TypeDef>) | ||
83 | .visit(doc_comments::<ast::ConstDef>) | ||
84 | .visit(doc_comments::<ast::StaticDef>) | ||
85 | .accept(node)? | ||
86 | } | ||
87 | |||
88 | /// Get a description of this node. | ||
89 | /// | ||
90 | /// e.g. `struct Name`, `enum Name`, `fn Name` | ||
91 | fn description(&self, db: &RootDatabase) -> Option<String> { | ||
92 | // TODO: After type inference is done, add type information to improve the output | ||
93 | let node = self.node(db)?; | ||
94 | let node = node.borrowed(); | ||
95 | // TODO: Refactor to be have less repetition | ||
96 | visitor() | ||
97 | .visit(|node: ast::FnDef| { | ||
98 | let mut string = "fn ".to_string(); | ||
99 | node.name()?.syntax().text().push_to(&mut string); | ||
100 | Some(string) | ||
101 | }) | ||
102 | .visit(|node: ast::StructDef| { | ||
103 | let mut string = "struct ".to_string(); | ||
104 | node.name()?.syntax().text().push_to(&mut string); | ||
105 | Some(string) | ||
106 | }) | ||
107 | .visit(|node: ast::EnumDef| { | ||
108 | let mut string = "enum ".to_string(); | ||
109 | node.name()?.syntax().text().push_to(&mut string); | ||
110 | Some(string) | ||
111 | }) | ||
112 | .visit(|node: ast::TraitDef| { | ||
113 | let mut string = "trait ".to_string(); | ||
114 | node.name()?.syntax().text().push_to(&mut string); | ||
115 | Some(string) | ||
116 | }) | ||
117 | .visit(|node: ast::Module| { | ||
118 | let mut string = "mod ".to_string(); | ||
119 | node.name()?.syntax().text().push_to(&mut string); | ||
120 | Some(string) | ||
121 | }) | ||
122 | .visit(|node: ast::TypeDef| { | ||
123 | let mut string = "type ".to_string(); | ||
124 | node.name()?.syntax().text().push_to(&mut string); | ||
125 | Some(string) | ||
126 | }) | ||
127 | .visit(|node: ast::ConstDef| { | ||
128 | let mut string = "const ".to_string(); | ||
129 | node.name()?.syntax().text().push_to(&mut string); | ||
130 | Some(string) | ||
131 | }) | ||
132 | .visit(|node: ast::StaticDef| { | ||
133 | let mut string = "static ".to_string(); | ||
134 | node.name()?.syntax().text().push_to(&mut string); | ||
135 | Some(string) | ||
136 | }) | ||
137 | .accept(node)? | ||
138 | } | ||
139 | } | ||
140 | |||
36 | #[cfg(test)] | 141 | #[cfg(test)] |
37 | mod tests { | 142 | mod tests { |
38 | use ra_syntax::TextRange; | 143 | use ra_syntax::TextRange; |