diff options
author | Edwin Cheng <[email protected]> | 2019-06-09 16:59:59 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-06-09 16:59:59 +0100 |
commit | aacc8941342d9b04fe65164018c07c994ff981b7 (patch) | |
tree | 437afc068961faadbe6ad30f93d82eb88f5bc1c0 /crates/ra_ide_api/src | |
parent | 2acf1e16fc89c51725a1bd6aafe8dfe4b9d19b1f (diff) |
Add display::Description
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/display.rs | 3 | ||||
-rw-r--r-- | crates/ra_ide_api/src/display/description.rs | 92 | ||||
-rw-r--r-- | crates/ra_ide_api/src/display/navigation_target.rs | 110 | ||||
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 25 |
4 files changed, 163 insertions, 67 deletions
diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index 0eef11464..1d0e8ba12 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs | |||
@@ -4,6 +4,7 @@ | |||
4 | mod function_signature; | 4 | mod function_signature; |
5 | mod navigation_target; | 5 | mod navigation_target; |
6 | mod structure; | 6 | mod structure; |
7 | mod description; | ||
7 | 8 | ||
8 | use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; | 9 | use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; |
9 | 10 | ||
@@ -11,6 +12,8 @@ pub use navigation_target::NavigationTarget; | |||
11 | pub use structure::{StructureNode, file_structure}; | 12 | pub use structure::{StructureNode, file_structure}; |
12 | pub use function_signature::FunctionSignature; | 13 | pub use function_signature::FunctionSignature; |
13 | 14 | ||
15 | pub(crate) use description::Description; | ||
16 | |||
14 | pub(crate) fn function_label(node: &ast::FnDef) -> String { | 17 | pub(crate) fn function_label(node: &ast::FnDef) -> String { |
15 | FunctionSignature::from(node).to_string() | 18 | FunctionSignature::from(node).to_string() |
16 | } | 19 | } |
diff --git a/crates/ra_ide_api/src/display/description.rs b/crates/ra_ide_api/src/display/description.rs new file mode 100644 index 000000000..fd0f564de --- /dev/null +++ b/crates/ra_ide_api/src/display/description.rs | |||
@@ -0,0 +1,92 @@ | |||
1 | use ra_syntax::{ | ||
2 | ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, AstNode}, | ||
3 | }; | ||
4 | |||
5 | pub(crate) trait Description { | ||
6 | fn description(&self) -> Option<String>; | ||
7 | } | ||
8 | |||
9 | impl Description for ast::FnDef { | ||
10 | fn description(&self) -> Option<String> { | ||
11 | Some(crate::display::function_label(self)) | ||
12 | } | ||
13 | } | ||
14 | |||
15 | impl Description for ast::StructDef { | ||
16 | fn description(&self) -> Option<String> { | ||
17 | visit_node(self, "struct ") | ||
18 | } | ||
19 | } | ||
20 | |||
21 | impl Description for ast::EnumDef { | ||
22 | fn description(&self) -> Option<String> { | ||
23 | visit_node(self, "enum ") | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl Description for ast::TraitDef { | ||
28 | fn description(&self) -> Option<String> { | ||
29 | visit_node(self, "trait ") | ||
30 | } | ||
31 | } | ||
32 | |||
33 | impl Description for ast::Module { | ||
34 | fn description(&self) -> Option<String> { | ||
35 | visit_node(self, "mod ") | ||
36 | } | ||
37 | } | ||
38 | |||
39 | impl Description for ast::TypeAliasDef { | ||
40 | fn description(&self) -> Option<String> { | ||
41 | visit_node(self, "type ") | ||
42 | } | ||
43 | } | ||
44 | |||
45 | impl Description for ast::ConstDef { | ||
46 | fn description(&self) -> Option<String> { | ||
47 | visit_ascribed_node(self, "const ") | ||
48 | } | ||
49 | } | ||
50 | |||
51 | impl Description for ast::StaticDef { | ||
52 | fn description(&self) -> Option<String> { | ||
53 | visit_ascribed_node(self, "static ") | ||
54 | } | ||
55 | } | ||
56 | |||
57 | impl Description for ast::NamedFieldDef { | ||
58 | fn description(&self) -> Option<String> { | ||
59 | visit_ascribed_node(self, "") | ||
60 | } | ||
61 | } | ||
62 | |||
63 | impl Description for ast::EnumVariant { | ||
64 | fn description(&self) -> Option<String> { | ||
65 | Some(self.name()?.text().to_string()) | ||
66 | } | ||
67 | } | ||
68 | |||
69 | fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String> | ||
70 | where | ||
71 | T: NameOwner + VisibilityOwner + TypeAscriptionOwner, | ||
72 | { | ||
73 | let mut string = visit_node(node, prefix)?; | ||
74 | |||
75 | if let Some(type_ref) = node.ascribed_type() { | ||
76 | string.push_str(": "); | ||
77 | type_ref.syntax().text().push_to(&mut string); | ||
78 | } | ||
79 | |||
80 | Some(string) | ||
81 | } | ||
82 | |||
83 | fn visit_node<T>(node: &T, label: &str) -> Option<String> | ||
84 | where | ||
85 | T: NameOwner + VisibilityOwner, | ||
86 | { | ||
87 | let mut string = | ||
88 | node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default(); | ||
89 | string.push_str(label); | ||
90 | string.push_str(node.name()?.text().as_str()); | ||
91 | Some(string) | ||
92 | } | ||
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index b2a925e2a..f60a07f1d 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs | |||
@@ -2,12 +2,13 @@ use ra_db::{FileId, SourceDatabase}; | |||
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, | 3 | SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, |
4 | SyntaxKind::{self, NAME}, | 4 | SyntaxKind::{self, NAME}, |
5 | ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, DocCommentsOwner}, | 5 | ast::{self, DocCommentsOwner}, |
6 | algo::visit::{visitor, Visitor}, | 6 | algo::visit::{visitor, Visitor}, |
7 | }; | 7 | }; |
8 | use hir::{ModuleSource, FieldSource, ImplItem}; | 8 | use hir::{ModuleSource, FieldSource, ImplItem}; |
9 | 9 | ||
10 | use crate::{FileSymbol, db::RootDatabase}; | 10 | use crate::{FileSymbol, db::RootDatabase}; |
11 | use super::description::Description; | ||
11 | 12 | ||
12 | /// `NavigationTarget` represents and element in the editor's UI which you can | 13 | /// `NavigationTarget` represents and element in the editor's UI which you can |
13 | /// click on to navigate to a particular piece of code. | 14 | /// click on to navigate to a particular piece of code. |
@@ -63,7 +64,7 @@ impl NavigationTarget { | |||
63 | } | 64 | } |
64 | 65 | ||
65 | pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget { | 66 | pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget { |
66 | NavigationTarget::from_named(file_id, pat, None) | 67 | NavigationTarget::from_named(file_id, pat, None, None) |
67 | } | 68 | } |
68 | 69 | ||
69 | pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget { | 70 | pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget { |
@@ -77,7 +78,7 @@ impl NavigationTarget { | |||
77 | full_range: symbol.ptr.range(), | 78 | full_range: symbol.ptr.range(), |
78 | focus_range: symbol.name_range, | 79 | focus_range: symbol.name_range, |
79 | container_name: symbol.container_name.clone(), | 80 | container_name: symbol.container_name.clone(), |
80 | description: description_inner(&node), | 81 | description: description_from_symbol(&node), |
81 | docs: docs_from_symbol(db, &symbol), | 82 | docs: docs_from_symbol(db, &symbol), |
82 | } | 83 | } |
83 | } | 84 | } |
@@ -92,7 +93,6 @@ impl NavigationTarget { | |||
92 | ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat), | 93 | ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat), |
93 | _ => ("_".into(), pat.syntax_node_ptr().range()), | 94 | _ => ("_".into(), pat.syntax_node_ptr().range()), |
94 | }; | 95 | }; |
95 | let node = pat.to_node(file.syntax()).syntax().to_owned(); | ||
96 | 96 | ||
97 | NavigationTarget { | 97 | NavigationTarget { |
98 | file_id, | 98 | file_id, |
@@ -101,20 +101,16 @@ impl NavigationTarget { | |||
101 | focus_range: None, | 101 | focus_range: None, |
102 | kind: NAME, | 102 | kind: NAME, |
103 | container_name: None, | 103 | container_name: None, |
104 | description: description_inner(&node), | 104 | description: None, //< No documentation for Description |
105 | docs: None, //< No documentation for Pattern | 105 | docs: None, //< No documentation for Pattern |
106 | } | 106 | } |
107 | } | 107 | } |
108 | 108 | ||
109 | pub(crate) fn from_self_param( | 109 | pub(crate) fn from_self_param( |
110 | db: &RootDatabase, | ||
111 | file_id: FileId, | 110 | file_id: FileId, |
112 | par: AstPtr<ast::SelfParam>, | 111 | par: AstPtr<ast::SelfParam>, |
113 | ) -> NavigationTarget { | 112 | ) -> NavigationTarget { |
114 | let (name, full_range) = ("self".into(), par.syntax_node_ptr().range()); | 113 | let (name, full_range) = ("self".into(), par.syntax_node_ptr().range()); |
115 | let file = db.parse(file_id).tree; | ||
116 | let ast = par.to_node(file.syntax()); | ||
117 | let node = ast.syntax().to_owned(); | ||
118 | 114 | ||
119 | NavigationTarget { | 115 | NavigationTarget { |
120 | file_id, | 116 | file_id, |
@@ -123,8 +119,8 @@ impl NavigationTarget { | |||
123 | focus_range: None, | 119 | focus_range: None, |
124 | kind: NAME, | 120 | kind: NAME, |
125 | container_name: None, | 121 | container_name: None, |
126 | description: description_inner(&node), | 122 | description: None, //< No document node for SelfParam |
127 | docs: None, //< No document node for SelfParam | 123 | docs: None, //< No document node for SelfParam |
128 | } | 124 | } |
129 | } | 125 | } |
130 | 126 | ||
@@ -134,7 +130,7 @@ impl NavigationTarget { | |||
134 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); | 130 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); |
135 | match source { | 131 | match source { |
136 | ModuleSource::SourceFile(node) => { | 132 | ModuleSource::SourceFile(node) => { |
137 | NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None) | 133 | NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None) |
138 | } | 134 | } |
139 | ModuleSource::Module(node) => NavigationTarget::from_syntax( | 135 | ModuleSource::Module(node) => NavigationTarget::from_syntax( |
140 | file_id, | 136 | file_id, |
@@ -142,6 +138,7 @@ impl NavigationTarget { | |||
142 | None, | 138 | None, |
143 | node.syntax(), | 139 | node.syntax(), |
144 | node.doc_comment_text(), | 140 | node.doc_comment_text(), |
141 | node.description(), | ||
145 | ), | 142 | ), |
146 | } | 143 | } |
147 | } | 144 | } |
@@ -156,6 +153,7 @@ impl NavigationTarget { | |||
156 | None, | 153 | None, |
157 | source.syntax(), | 154 | source.syntax(), |
158 | source.doc_comment_text(), | 155 | source.doc_comment_text(), |
156 | source.description(), | ||
159 | ); | 157 | ); |
160 | } | 158 | } |
161 | NavigationTarget::from_module(db, module) | 159 | NavigationTarget::from_module(db, module) |
@@ -163,7 +161,12 @@ impl NavigationTarget { | |||
163 | 161 | ||
164 | pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget { | 162 | pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget { |
165 | let (file_id, fn_def) = func.source(db); | 163 | let (file_id, fn_def) = func.source(db); |
166 | NavigationTarget::from_named(file_id.original_file(db), &*fn_def, fn_def.doc_comment_text()) | 164 | NavigationTarget::from_named( |
165 | file_id.original_file(db), | ||
166 | &*fn_def, | ||
167 | fn_def.doc_comment_text(), | ||
168 | fn_def.description(), | ||
169 | ) | ||
167 | } | 170 | } |
168 | 171 | ||
169 | pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { | 172 | pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { |
@@ -171,10 +174,10 @@ impl NavigationTarget { | |||
171 | let file_id = file_id.original_file(db); | 174 | let file_id = file_id.original_file(db); |
172 | match field { | 175 | match field { |
173 | FieldSource::Named(it) => { | 176 | FieldSource::Named(it) => { |
174 | NavigationTarget::from_named(file_id, &*it, it.doc_comment_text()) | 177 | NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.description()) |
175 | } | 178 | } |
176 | FieldSource::Pos(it) => { | 179 | FieldSource::Pos(it) => { |
177 | NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None) | 180 | NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None, None) |
178 | } | 181 | } |
179 | } | 182 | } |
180 | } | 183 | } |
@@ -187,6 +190,7 @@ impl NavigationTarget { | |||
187 | file_id.original_file(db), | 190 | file_id.original_file(db), |
188 | &*node, | 191 | &*node, |
189 | node.doc_comment_text(), | 192 | node.doc_comment_text(), |
193 | node.description(), | ||
190 | ) | 194 | ) |
191 | } | 195 | } |
192 | hir::AdtDef::Union(s) => { | 196 | hir::AdtDef::Union(s) => { |
@@ -195,6 +199,7 @@ impl NavigationTarget { | |||
195 | file_id.original_file(db), | 199 | file_id.original_file(db), |
196 | &*node, | 200 | &*node, |
197 | node.doc_comment_text(), | 201 | node.doc_comment_text(), |
202 | node.description(), | ||
198 | ) | 203 | ) |
199 | } | 204 | } |
200 | hir::AdtDef::Enum(s) => { | 205 | hir::AdtDef::Enum(s) => { |
@@ -203,6 +208,7 @@ impl NavigationTarget { | |||
203 | file_id.original_file(db), | 208 | file_id.original_file(db), |
204 | &*node, | 209 | &*node, |
205 | node.doc_comment_text(), | 210 | node.doc_comment_text(), |
211 | node.description(), | ||
206 | ) | 212 | ) |
207 | } | 213 | } |
208 | } | 214 | } |
@@ -221,6 +227,7 @@ impl NavigationTarget { | |||
221 | file_id.original_file(db), | 227 | file_id.original_file(db), |
222 | &*node, | 228 | &*node, |
223 | node.doc_comment_text(), | 229 | node.doc_comment_text(), |
230 | node.description(), | ||
224 | ) | 231 | ) |
225 | } | 232 | } |
226 | hir::ModuleDef::Union(s) => { | 233 | hir::ModuleDef::Union(s) => { |
@@ -229,6 +236,7 @@ impl NavigationTarget { | |||
229 | file_id.original_file(db), | 236 | file_id.original_file(db), |
230 | &*node, | 237 | &*node, |
231 | node.doc_comment_text(), | 238 | node.doc_comment_text(), |
239 | node.description(), | ||
232 | ) | 240 | ) |
233 | } | 241 | } |
234 | hir::ModuleDef::Const(s) => { | 242 | hir::ModuleDef::Const(s) => { |
@@ -237,6 +245,7 @@ impl NavigationTarget { | |||
237 | file_id.original_file(db), | 245 | file_id.original_file(db), |
238 | &*node, | 246 | &*node, |
239 | node.doc_comment_text(), | 247 | node.doc_comment_text(), |
248 | node.description(), | ||
240 | ) | 249 | ) |
241 | } | 250 | } |
242 | hir::ModuleDef::Static(s) => { | 251 | hir::ModuleDef::Static(s) => { |
@@ -245,6 +254,7 @@ impl NavigationTarget { | |||
245 | file_id.original_file(db), | 254 | file_id.original_file(db), |
246 | &*node, | 255 | &*node, |
247 | node.doc_comment_text(), | 256 | node.doc_comment_text(), |
257 | node.description(), | ||
248 | ) | 258 | ) |
249 | } | 259 | } |
250 | hir::ModuleDef::Enum(e) => { | 260 | hir::ModuleDef::Enum(e) => { |
@@ -253,6 +263,7 @@ impl NavigationTarget { | |||
253 | file_id.original_file(db), | 263 | file_id.original_file(db), |
254 | &*node, | 264 | &*node, |
255 | node.doc_comment_text(), | 265 | node.doc_comment_text(), |
266 | node.description(), | ||
256 | ) | 267 | ) |
257 | } | 268 | } |
258 | hir::ModuleDef::EnumVariant(var) => { | 269 | hir::ModuleDef::EnumVariant(var) => { |
@@ -261,6 +272,7 @@ impl NavigationTarget { | |||
261 | file_id.original_file(db), | 272 | file_id.original_file(db), |
262 | &*node, | 273 | &*node, |
263 | node.doc_comment_text(), | 274 | node.doc_comment_text(), |
275 | node.description(), | ||
264 | ) | 276 | ) |
265 | } | 277 | } |
266 | hir::ModuleDef::Trait(e) => { | 278 | hir::ModuleDef::Trait(e) => { |
@@ -269,6 +281,7 @@ impl NavigationTarget { | |||
269 | file_id.original_file(db), | 281 | file_id.original_file(db), |
270 | &*node, | 282 | &*node, |
271 | node.doc_comment_text(), | 283 | node.doc_comment_text(), |
284 | node.description(), | ||
272 | ) | 285 | ) |
273 | } | 286 | } |
274 | hir::ModuleDef::TypeAlias(e) => { | 287 | hir::ModuleDef::TypeAlias(e) => { |
@@ -277,6 +290,7 @@ impl NavigationTarget { | |||
277 | file_id.original_file(db), | 290 | file_id.original_file(db), |
278 | &*node, | 291 | &*node, |
279 | node.doc_comment_text(), | 292 | node.doc_comment_text(), |
293 | node.description(), | ||
280 | ) | 294 | ) |
281 | } | 295 | } |
282 | hir::ModuleDef::BuiltinType(..) => { | 296 | hir::ModuleDef::BuiltinType(..) => { |
@@ -297,6 +311,7 @@ impl NavigationTarget { | |||
297 | None, | 311 | None, |
298 | node.syntax(), | 312 | node.syntax(), |
299 | None, | 313 | None, |
314 | None, | ||
300 | ) | 315 | ) |
301 | } | 316 | } |
302 | 317 | ||
@@ -309,6 +324,7 @@ impl NavigationTarget { | |||
309 | file_id.original_file(db), | 324 | file_id.original_file(db), |
310 | &*node, | 325 | &*node, |
311 | node.doc_comment_text(), | 326 | node.doc_comment_text(), |
327 | node.description(), | ||
312 | ) | 328 | ) |
313 | } | 329 | } |
314 | ImplItem::TypeAlias(a) => { | 330 | ImplItem::TypeAlias(a) => { |
@@ -317,6 +333,7 @@ impl NavigationTarget { | |||
317 | file_id.original_file(db), | 333 | file_id.original_file(db), |
318 | &*node, | 334 | &*node, |
319 | node.doc_comment_text(), | 335 | node.doc_comment_text(), |
336 | node.description(), | ||
320 | ) | 337 | ) |
321 | } | 338 | } |
322 | } | 339 | } |
@@ -325,7 +342,12 @@ impl NavigationTarget { | |||
325 | pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { | 342 | pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { |
326 | let (file_id, node) = macro_call.source(db); | 343 | let (file_id, node) = macro_call.source(db); |
327 | log::debug!("nav target {}", node.syntax().debug_dump()); | 344 | log::debug!("nav target {}", node.syntax().debug_dump()); |
328 | NavigationTarget::from_named(file_id.original_file(db), &*node, node.doc_comment_text()) | 345 | NavigationTarget::from_named( |
346 | file_id.original_file(db), | ||
347 | &*node, | ||
348 | node.doc_comment_text(), | ||
349 | None, | ||
350 | ) | ||
329 | } | 351 | } |
330 | 352 | ||
331 | #[cfg(test)] | 353 | #[cfg(test)] |
@@ -357,11 +379,12 @@ impl NavigationTarget { | |||
357 | file_id: FileId, | 379 | file_id: FileId, |
358 | node: &impl ast::NameOwner, | 380 | node: &impl ast::NameOwner, |
359 | docs: Option<String>, | 381 | docs: Option<String>, |
382 | description: Option<String>, | ||
360 | ) -> NavigationTarget { | 383 | ) -> NavigationTarget { |
361 | //FIXME: use `_` instead of empty string | 384 | //FIXME: use `_` instead of empty string |
362 | let name = node.name().map(|it| it.text().clone()).unwrap_or_default(); | 385 | let name = node.name().map(|it| it.text().clone()).unwrap_or_default(); |
363 | let focus_range = node.name().map(|it| it.syntax().range()); | 386 | let focus_range = node.name().map(|it| it.syntax().range()); |
364 | NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax(), docs) | 387 | NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax(), docs, description) |
365 | } | 388 | } |
366 | 389 | ||
367 | fn from_syntax( | 390 | fn from_syntax( |
@@ -370,6 +393,7 @@ impl NavigationTarget { | |||
370 | focus_range: Option<TextRange>, | 393 | focus_range: Option<TextRange>, |
371 | node: &SyntaxNode, | 394 | node: &SyntaxNode, |
372 | docs: Option<String>, | 395 | docs: Option<String>, |
396 | description: Option<String>, | ||
373 | ) -> NavigationTarget { | 397 | ) -> NavigationTarget { |
374 | NavigationTarget { | 398 | NavigationTarget { |
375 | file_id, | 399 | file_id, |
@@ -379,7 +403,7 @@ impl NavigationTarget { | |||
379 | focus_range, | 403 | focus_range, |
380 | // ptr: Some(LocalSyntaxPtr::new(node)), | 404 | // ptr: Some(LocalSyntaxPtr::new(node)), |
381 | container_name: None, | 405 | container_name: None, |
382 | description: description_inner(node), | 406 | description, |
383 | docs, | 407 | docs, |
384 | } | 408 | } |
385 | } | 409 | } |
@@ -408,46 +432,22 @@ fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { | |||
408 | .accept(&node)? | 432 | .accept(&node)? |
409 | } | 433 | } |
410 | 434 | ||
411 | /// Get a description of a node. | 435 | /// Get a description of a symbol. |
412 | /// | 436 | /// |
413 | /// e.g. `struct Name`, `enum Name`, `fn Name` | 437 | /// e.g. `struct Name`, `enum Name`, `fn Name` |
414 | fn description_inner(node: &SyntaxNode) -> Option<String> { | 438 | fn description_from_symbol(node: &SyntaxNode) -> Option<String> { |
415 | // FIXME: After type inference is done, add type information to improve the output | 439 | // FIXME: After type inference is done, add type information to improve the output |
416 | fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String> | ||
417 | where | ||
418 | T: NameOwner + VisibilityOwner + TypeAscriptionOwner, | ||
419 | { | ||
420 | let mut string = visit_node(node, prefix)?; | ||
421 | |||
422 | if let Some(type_ref) = node.ascribed_type() { | ||
423 | string.push_str(": "); | ||
424 | type_ref.syntax().text().push_to(&mut string); | ||
425 | } | ||
426 | |||
427 | Some(string) | ||
428 | } | ||
429 | |||
430 | fn visit_node<T>(node: &T, label: &str) -> Option<String> | ||
431 | where | ||
432 | T: NameOwner + VisibilityOwner, | ||
433 | { | ||
434 | let mut string = | ||
435 | node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default(); | ||
436 | string.push_str(label); | ||
437 | string.push_str(node.name()?.text().as_str()); | ||
438 | Some(string) | ||
439 | } | ||
440 | 440 | ||
441 | visitor() | 441 | visitor() |
442 | .visit(|node: &ast::FnDef| Some(crate::display::function_label(node))) | 442 | .visit(|node: &ast::FnDef| node.description()) |
443 | .visit(|node: &ast::StructDef| visit_node(node, "struct ")) | 443 | .visit(|node: &ast::StructDef| node.description()) |
444 | .visit(|node: &ast::EnumDef| visit_node(node, "enum ")) | 444 | .visit(|node: &ast::EnumDef| node.description()) |
445 | .visit(|node: &ast::TraitDef| visit_node(node, "trait ")) | 445 | .visit(|node: &ast::TraitDef| node.description()) |
446 | .visit(|node: &ast::Module| visit_node(node, "mod ")) | 446 | .visit(|node: &ast::Module| node.description()) |
447 | .visit(|node: &ast::TypeAliasDef| visit_node(node, "type ")) | 447 | .visit(|node: &ast::TypeAliasDef| node.description()) |
448 | .visit(|node: &ast::ConstDef| visit_ascribed_node(node, "const ")) | 448 | .visit(|node: &ast::ConstDef| node.description()) |
449 | .visit(|node: &ast::StaticDef| visit_ascribed_node(node, "static ")) | 449 | .visit(|node: &ast::StaticDef| node.description()) |
450 | .visit(|node: &ast::NamedFieldDef| visit_ascribed_node(node, "")) | 450 | .visit(|node: &ast::NamedFieldDef| node.description()) |
451 | .visit(|node: &ast::EnumVariant| Some(node.name()?.text().to_string())) | 451 | .visit(|node: &ast::EnumVariant| node.description()) |
452 | .accept(&node)? | 452 | .accept(&node)? |
453 | } | 453 | } |
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index fbd881bfe..359fc2da1 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -13,6 +13,7 @@ use crate::{ | |||
13 | db::RootDatabase, | 13 | db::RootDatabase, |
14 | RangeInfo, | 14 | RangeInfo, |
15 | name_ref_kind::{NameRefKind::*, classify_name_ref}, | 15 | name_ref_kind::{NameRefKind::*, classify_name_ref}, |
16 | display::Description, | ||
16 | }; | 17 | }; |
17 | 18 | ||
18 | pub(crate) fn goto_definition( | 19 | pub(crate) fn goto_definition( |
@@ -72,7 +73,7 @@ pub(crate) fn reference_definition( | |||
72 | } | 73 | } |
73 | } | 74 | } |
74 | Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), | 75 | Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), |
75 | Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(db, file_id, par)), | 76 | Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)), |
76 | Some(GenericParam(_)) => { | 77 | Some(GenericParam(_)) => { |
77 | // FIXME: go to the generic param def | 78 | // FIXME: go to the generic param def |
78 | } | 79 | } |
@@ -115,37 +116,37 @@ pub(crate) fn name_definition( | |||
115 | fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> { | 116 | fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> { |
116 | visitor() | 117 | visitor() |
117 | .visit(|node: &ast::StructDef| { | 118 | .visit(|node: &ast::StructDef| { |
118 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 119 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
119 | }) | 120 | }) |
120 | .visit(|node: &ast::EnumDef| { | 121 | .visit(|node: &ast::EnumDef| { |
121 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 122 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
122 | }) | 123 | }) |
123 | .visit(|node: &ast::EnumVariant| { | 124 | .visit(|node: &ast::EnumVariant| { |
124 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 125 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
125 | }) | 126 | }) |
126 | .visit(|node: &ast::FnDef| { | 127 | .visit(|node: &ast::FnDef| { |
127 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 128 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
128 | }) | 129 | }) |
129 | .visit(|node: &ast::TypeAliasDef| { | 130 | .visit(|node: &ast::TypeAliasDef| { |
130 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 131 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
131 | }) | 132 | }) |
132 | .visit(|node: &ast::ConstDef| { | 133 | .visit(|node: &ast::ConstDef| { |
133 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 134 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
134 | }) | 135 | }) |
135 | .visit(|node: &ast::StaticDef| { | 136 | .visit(|node: &ast::StaticDef| { |
136 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 137 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
137 | }) | 138 | }) |
138 | .visit(|node: &ast::TraitDef| { | 139 | .visit(|node: &ast::TraitDef| { |
139 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 140 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
140 | }) | 141 | }) |
141 | .visit(|node: &ast::NamedFieldDef| { | 142 | .visit(|node: &ast::NamedFieldDef| { |
142 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 143 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
143 | }) | 144 | }) |
144 | .visit(|node: &ast::Module| { | 145 | .visit(|node: &ast::Module| { |
145 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 146 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) |
146 | }) | 147 | }) |
147 | .visit(|node: &ast::MacroCall| { | 148 | .visit(|node: &ast::MacroCall| { |
148 | NavigationTarget::from_named(file_id, node, node.doc_comment_text()) | 149 | NavigationTarget::from_named(file_id, node, node.doc_comment_text(), None) |
149 | }) | 150 | }) |
150 | .accept(node) | 151 | .accept(node) |
151 | } | 152 | } |