diff options
Diffstat (limited to 'crates/ra_ide_api/src/goto_definition.rs')
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 43 |
1 files changed, 43 insertions, 0 deletions
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 | " |