diff options
Diffstat (limited to 'crates/ra_ide_api/src/goto_definition.rs')
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 77 |
1 files changed, 64 insertions, 13 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index c1ce54bea..afa59cbe3 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -101,19 +101,20 @@ pub(crate) fn name_definition( | |||
101 | } | 101 | } |
102 | } | 102 | } |
103 | 103 | ||
104 | if let Some(nav) = named_target(file_id, &parent) { | 104 | if let Some(nav) = named_target(db, file_id, &parent) { |
105 | return Some(vec![nav]); | 105 | return Some(vec![nav]); |
106 | } | 106 | } |
107 | 107 | ||
108 | None | 108 | None |
109 | } | 109 | } |
110 | 110 | ||
111 | fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> { | 111 | fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> { |
112 | match_ast! { | 112 | match_ast! { |
113 | match node { | 113 | match node { |
114 | ast::StructDef(it) => { | 114 | ast::StructDef(it) => { |
115 | Some(NavigationTarget::from_named( | 115 | Some(NavigationTarget::from_named( |
116 | file_id, | 116 | db, |
117 | file_id.into(), | ||
117 | &it, | 118 | &it, |
118 | it.doc_comment_text(), | 119 | it.doc_comment_text(), |
119 | it.short_label(), | 120 | it.short_label(), |
@@ -121,7 +122,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
121 | }, | 122 | }, |
122 | ast::EnumDef(it) => { | 123 | ast::EnumDef(it) => { |
123 | Some(NavigationTarget::from_named( | 124 | Some(NavigationTarget::from_named( |
124 | file_id, | 125 | db, |
126 | file_id.into(), | ||
125 | &it, | 127 | &it, |
126 | it.doc_comment_text(), | 128 | it.doc_comment_text(), |
127 | it.short_label(), | 129 | it.short_label(), |
@@ -129,7 +131,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
129 | }, | 131 | }, |
130 | ast::EnumVariant(it) => { | 132 | ast::EnumVariant(it) => { |
131 | Some(NavigationTarget::from_named( | 133 | Some(NavigationTarget::from_named( |
132 | file_id, | 134 | db, |
135 | file_id.into(), | ||
133 | &it, | 136 | &it, |
134 | it.doc_comment_text(), | 137 | it.doc_comment_text(), |
135 | it.short_label(), | 138 | it.short_label(), |
@@ -137,7 +140,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
137 | }, | 140 | }, |
138 | ast::FnDef(it) => { | 141 | ast::FnDef(it) => { |
139 | Some(NavigationTarget::from_named( | 142 | Some(NavigationTarget::from_named( |
140 | file_id, | 143 | db, |
144 | file_id.into(), | ||
141 | &it, | 145 | &it, |
142 | it.doc_comment_text(), | 146 | it.doc_comment_text(), |
143 | it.short_label(), | 147 | it.short_label(), |
@@ -145,7 +149,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
145 | }, | 149 | }, |
146 | ast::TypeAliasDef(it) => { | 150 | ast::TypeAliasDef(it) => { |
147 | Some(NavigationTarget::from_named( | 151 | Some(NavigationTarget::from_named( |
148 | file_id, | 152 | db, |
153 | file_id.into(), | ||
149 | &it, | 154 | &it, |
150 | it.doc_comment_text(), | 155 | it.doc_comment_text(), |
151 | it.short_label(), | 156 | it.short_label(), |
@@ -153,7 +158,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
153 | }, | 158 | }, |
154 | ast::ConstDef(it) => { | 159 | ast::ConstDef(it) => { |
155 | Some(NavigationTarget::from_named( | 160 | Some(NavigationTarget::from_named( |
156 | file_id, | 161 | db, |
162 | file_id.into(), | ||
157 | &it, | 163 | &it, |
158 | it.doc_comment_text(), | 164 | it.doc_comment_text(), |
159 | it.short_label(), | 165 | it.short_label(), |
@@ -161,7 +167,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
161 | }, | 167 | }, |
162 | ast::StaticDef(it) => { | 168 | ast::StaticDef(it) => { |
163 | Some(NavigationTarget::from_named( | 169 | Some(NavigationTarget::from_named( |
164 | file_id, | 170 | db, |
171 | file_id.into(), | ||
165 | &it, | 172 | &it, |
166 | it.doc_comment_text(), | 173 | it.doc_comment_text(), |
167 | it.short_label(), | 174 | it.short_label(), |
@@ -169,7 +176,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
169 | }, | 176 | }, |
170 | ast::TraitDef(it) => { | 177 | ast::TraitDef(it) => { |
171 | Some(NavigationTarget::from_named( | 178 | Some(NavigationTarget::from_named( |
172 | file_id, | 179 | db, |
180 | file_id.into(), | ||
173 | &it, | 181 | &it, |
174 | it.doc_comment_text(), | 182 | it.doc_comment_text(), |
175 | it.short_label(), | 183 | it.short_label(), |
@@ -177,7 +185,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
177 | }, | 185 | }, |
178 | ast::RecordFieldDef(it) => { | 186 | ast::RecordFieldDef(it) => { |
179 | Some(NavigationTarget::from_named( | 187 | Some(NavigationTarget::from_named( |
180 | file_id, | 188 | db, |
189 | file_id.into(), | ||
181 | &it, | 190 | &it, |
182 | it.doc_comment_text(), | 191 | it.doc_comment_text(), |
183 | it.short_label(), | 192 | it.short_label(), |
@@ -185,7 +194,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
185 | }, | 194 | }, |
186 | ast::Module(it) => { | 195 | ast::Module(it) => { |
187 | Some(NavigationTarget::from_named( | 196 | Some(NavigationTarget::from_named( |
188 | file_id, | 197 | db, |
198 | file_id.into(), | ||
189 | &it, | 199 | &it, |
190 | it.doc_comment_text(), | 200 | it.doc_comment_text(), |
191 | it.short_label(), | 201 | it.short_label(), |
@@ -193,7 +203,8 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> | |||
193 | }, | 203 | }, |
194 | ast::MacroCall(it) => { | 204 | ast::MacroCall(it) => { |
195 | Some(NavigationTarget::from_named( | 205 | Some(NavigationTarget::from_named( |
196 | file_id, | 206 | db, |
207 | file_id.into(), | ||
197 | &it, | 208 | &it, |
198 | it.doc_comment_text(), | 209 | it.doc_comment_text(), |
199 | None, | 210 | None, |
@@ -335,6 +346,46 @@ mod tests { | |||
335 | } | 346 | } |
336 | 347 | ||
337 | #[test] | 348 | #[test] |
349 | fn goto_definition_works_for_macro_defined_fn_with_arg() { | ||
350 | check_goto( | ||
351 | " | ||
352 | //- /lib.rs | ||
353 | macro_rules! define_fn { | ||
354 | ($name:ident) => (fn $name() {}) | ||
355 | } | ||
356 | |||
357 | define_fn!( | ||
358 | foo | ||
359 | ) | ||
360 | |||
361 | fn bar() { | ||
362 | <|>foo(); | ||
363 | } | ||
364 | ", | ||
365 | "foo FN_DEF FileId(1) [80; 83) [80; 83)", | ||
366 | ); | ||
367 | } | ||
368 | |||
369 | #[test] | ||
370 | fn goto_definition_works_for_macro_defined_fn_no_arg() { | ||
371 | check_goto( | ||
372 | " | ||
373 | //- /lib.rs | ||
374 | macro_rules! define_fn { | ||
375 | () => (fn foo() {}) | ||
376 | } | ||
377 | |||
378 | define_fn!(); | ||
379 | |||
380 | fn bar() { | ||
381 | <|>foo(); | ||
382 | } | ||
383 | ", | ||
384 | "foo FN_DEF FileId(1) [39; 42) [39; 42)", | ||
385 | ); | ||
386 | } | ||
387 | |||
388 | #[test] | ||
338 | fn goto_definition_works_for_methods() { | 389 | fn goto_definition_works_for_methods() { |
339 | covers!(goto_definition_works_for_methods); | 390 | covers!(goto_definition_works_for_methods); |
340 | check_goto( | 391 | check_goto( |