diff options
Diffstat (limited to 'crates/ra_ide_api/src/display/navigation_target.rs')
-rw-r--r-- | crates/ra_ide_api/src/display/navigation_target.rs | 110 |
1 files changed, 55 insertions, 55 deletions
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 | } |