diff options
author | Florian Diebold <[email protected]> | 2019-01-07 23:30:49 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-01-10 20:32:54 +0000 |
commit | a6590ce2318676210b6b5a197b76b5861a3407c9 (patch) | |
tree | ec9a223b23f7675a8e0eb1d12c454adc9551baf2 /crates/ra_ide_api | |
parent | dc2a8d5acc53054c86ad17260b69d4bf4f14dbc6 (diff) |
Use name resolution for goto definition
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_path.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 43 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 64 |
3 files changed, 109 insertions, 4 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 4860db629..a25ad3f13 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs | |||
@@ -15,11 +15,11 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C | |||
15 | match def_id.resolve(ctx.db)? { | 15 | match def_id.resolve(ctx.db)? { |
16 | hir::Def::Module(module) => { | 16 | hir::Def::Module(module) => { |
17 | let module_scope = module.scope(ctx.db)?; | 17 | let module_scope = module.scope(ctx.db)?; |
18 | module_scope.entries().for_each(|(name, res)| { | 18 | for (name, res) in module_scope.entries() { |
19 | CompletionItem::new(CompletionKind::Reference, name.to_string()) | 19 | CompletionItem::new(CompletionKind::Reference, name.to_string()) |
20 | .from_resolution(ctx, res) | 20 | .from_resolution(ctx, res) |
21 | .add_to(acc) | 21 | .add_to(acc); |
22 | }); | 22 | } |
23 | } | 23 | } |
24 | hir::Def::Enum(e) => { | 24 | hir::Def::Enum(e) => { |
25 | e.variants(ctx.db)? | 25 | e.variants(ctx.db)? |
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 0d524b6f1..eaddd5083 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -42,6 +42,24 @@ pub(crate) fn reference_definition( | |||
42 | return Ok(vec![nav]); | 42 | return Ok(vec![nav]); |
43 | }; | 43 | }; |
44 | } | 44 | } |
45 | // Then try module name resolution | ||
46 | if let Some(module) = | ||
47 | hir::source_binder::module_from_child_node(db, file_id, name_ref.syntax())? | ||
48 | { | ||
49 | if let Some(path) = name_ref | ||
50 | .syntax() | ||
51 | .ancestors() | ||
52 | .find_map(ast::Path::cast) | ||
53 | .and_then(hir::Path::from_ast) | ||
54 | { | ||
55 | let resolved = module.resolve_path(db, &path)?; | ||
56 | if let Some(def_id) = resolved.take_types().or(resolved.take_values()) { | ||
57 | if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? { | ||
58 | return Ok(vec![target]); | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | } | ||
45 | // If that fails try the index based approach. | 63 | // If that fails try the index based approach. |
46 | let navs = db | 64 | let navs = db |
47 | .index_resolve(name_ref)? | 65 | .index_resolve(name_ref)? |
@@ -105,6 +123,31 @@ mod tests { | |||
105 | } | 123 | } |
106 | 124 | ||
107 | #[test] | 125 | #[test] |
126 | fn goto_definition_resolves_correct_name() { | ||
127 | let (analysis, pos) = analysis_and_position( | ||
128 | " | ||
129 | //- /lib.rs | ||
130 | use a::Foo; | ||
131 | mod a; | ||
132 | mod b; | ||
133 | enum E { X(Foo<|>) } | ||
134 | //- /a.rs | ||
135 | struct Foo; | ||
136 | //- /b.rs | ||
137 | struct Foo; | ||
138 | ", | ||
139 | ); | ||
140 | |||
141 | let symbols = analysis.goto_definition(pos).unwrap().unwrap(); | ||
142 | assert_eq_dbg( | ||
143 | r#"[NavigationTarget { file_id: FileId(2), name: "Foo", | ||
144 | kind: STRUCT_DEF, range: [0; 11), | ||
145 | ptr: Some(LocalSyntaxPtr { range: [0; 11), kind: STRUCT_DEF }) }]"#, | ||
146 | &symbols, | ||
147 | ); | ||
148 | } | ||
149 | |||
150 | #[test] | ||
108 | fn goto_definition_works_for_module_declaration() { | 151 | fn goto_definition_works_for_module_declaration() { |
109 | let (analysis, pos) = analysis_and_position( | 152 | let (analysis, pos) = analysis_and_position( |
110 | " | 153 | " |
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index f505959ce..65d21d899 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -33,7 +33,8 @@ mod syntax_highlighting; | |||
33 | 33 | ||
34 | use std::{fmt, sync::Arc}; | 34 | use std::{fmt, sync::Arc}; |
35 | 35 | ||
36 | use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, TextRange, TextUnit}; | 36 | use hir::{Def, ModuleSource, Name}; |
37 | use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, SyntaxNode, TextRange, TextUnit, AstNode}; | ||
37 | use ra_text_edit::TextEdit; | 38 | use ra_text_edit::TextEdit; |
38 | use ra_db::{SyntaxDatabase, FilesDatabase, LocalSyntaxPtr, BaseDatabase}; | 39 | use ra_db::{SyntaxDatabase, FilesDatabase, LocalSyntaxPtr, BaseDatabase}; |
39 | use rayon::prelude::*; | 40 | use rayon::prelude::*; |
@@ -268,6 +269,67 @@ impl NavigationTarget { | |||
268 | } | 269 | } |
269 | } | 270 | } |
270 | 271 | ||
272 | fn from_syntax(name: Option<Name>, file_id: FileId, node: &SyntaxNode) -> NavigationTarget { | ||
273 | NavigationTarget { | ||
274 | file_id, | ||
275 | name: name.map(|n| n.to_string().into()).unwrap_or("".into()), | ||
276 | kind: node.kind(), | ||
277 | range: node.range(), | ||
278 | ptr: Some(LocalSyntaxPtr::new(node)), | ||
279 | } | ||
280 | } | ||
281 | // TODO once Def::Item is gone, this should be able to always return a NavigationTarget | ||
282 | fn from_def(db: &db::RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> { | ||
283 | Ok(match def { | ||
284 | Def::Struct(s) => { | ||
285 | let (file_id, node) = s.source(db)?; | ||
286 | Some(NavigationTarget::from_syntax( | ||
287 | s.name(db)?, | ||
288 | file_id.original_file(db), | ||
289 | node.syntax(), | ||
290 | )) | ||
291 | } | ||
292 | Def::Enum(e) => { | ||
293 | let (file_id, node) = e.source(db)?; | ||
294 | Some(NavigationTarget::from_syntax( | ||
295 | e.name(db)?, | ||
296 | file_id.original_file(db), | ||
297 | node.syntax(), | ||
298 | )) | ||
299 | } | ||
300 | Def::EnumVariant(ev) => { | ||
301 | let (file_id, node) = ev.source(db)?; | ||
302 | Some(NavigationTarget::from_syntax( | ||
303 | ev.name(db)?, | ||
304 | file_id.original_file(db), | ||
305 | node.syntax(), | ||
306 | )) | ||
307 | } | ||
308 | Def::Function(f) => { | ||
309 | let (file_id, node) = f.source(db)?; | ||
310 | let name = f.signature(db).name().clone(); | ||
311 | Some(NavigationTarget::from_syntax( | ||
312 | Some(name), | ||
313 | file_id.original_file(db), | ||
314 | node.syntax(), | ||
315 | )) | ||
316 | } | ||
317 | Def::Module(m) => { | ||
318 | let (file_id, source) = m.definition_source(db)?; | ||
319 | let name = m.name(db)?; | ||
320 | match source { | ||
321 | ModuleSource::SourceFile(node) => { | ||
322 | Some(NavigationTarget::from_syntax(name, file_id, node.syntax())) | ||
323 | } | ||
324 | ModuleSource::Module(node) => { | ||
325 | Some(NavigationTarget::from_syntax(name, file_id, node.syntax())) | ||
326 | } | ||
327 | } | ||
328 | } | ||
329 | Def::Item => None, | ||
330 | }) | ||
331 | } | ||
332 | |||
271 | pub fn name(&self) -> &SmolStr { | 333 | pub fn name(&self) -> &SmolStr { |
272 | &self.name | 334 | &self.name |
273 | } | 335 | } |