diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-13 16:59:35 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-13 16:59:35 +0000 |
commit | 4209022e5b8dc185b098d76fd6701cd046b7c37d (patch) | |
tree | 98a579f8c0fd7a0738862937ec32a7dc62172e1a /crates/ra_ide_api/src | |
parent | 8b985b427c4e67d6833745258bfdf1c4e9eb62ca (diff) | |
parent | eedc08300c427b854db56f8fe1f1866ed398d5ee (diff) |
Merge #527
527: goto defenition works for type-inferred methods r=flodiebold a=matklad
This uses type inference results for `goto method` functionality.
This is achieved by adding another map to `InferenceResult`. I wonder how we should handle this long-term... The pattern seems to be "we are doing some analysis, and we produce some stuff as a by-product, and IDE would like to use the stuff". Ideally, adding an additional bit of info shouldn't require threading it through all data structures.
I kinda like how Kotlin deals with this problem. They have this [`BindingContext`](https://github.com/JetBrains/kotlin/blob/72e351a0e3610051fe4222dca4e1eeedf7ae45da/compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContext.java#L122) thing, which is basically an [`AnyMap`](https://github.com/JetBrains/kotlin/blob/72e351a0e3610051fe4222dca4e1eeedf7ae45da/compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContext.java#L122) of HashMaps.
Deep in the compiler guts, they [record the info](https://github.com/JetBrains/kotlin/blob/ba6da7c40a6cc502508faf6e04fa105b96bc7777/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TracingStrategyForInvoke.java#L70-L75) into the map, using a type key, a value key and a value.
Then the IDE [reads this map](https://github.com/JetBrains/kotlin/blob/ba6da7c40a6cc502508faf6e04fa105b96bc7777/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantNotNullExtensionReceiverOfInlineInspection.kt#L64) (via a [helper](https://github.com/JetBrains/kotlin/blob/ba6da7c40a6cc502508faf6e04fa105b96bc7777/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/callUtil.kt#L178-L180)). The stuff in between does not know that this type-key exists, unless it inspects it.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index e2537758d..332a2fb8d 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -47,15 +47,34 @@ pub(crate) fn reference_definition( | |||
47 | name_ref: &ast::NameRef, | 47 | name_ref: &ast::NameRef, |
48 | ) -> Cancelable<ReferenceResult> { | 48 | ) -> Cancelable<ReferenceResult> { |
49 | use self::ReferenceResult::*; | 49 | use self::ReferenceResult::*; |
50 | if let Some(fn_descr) = | 50 | if let Some(function) = |
51 | hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax())? | 51 | hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax())? |
52 | { | 52 | { |
53 | let scope = fn_descr.scopes(db)?; | 53 | let scope = function.scopes(db)?; |
54 | // First try to resolve the symbol locally | 54 | // First try to resolve the symbol locally |
55 | if let Some(entry) = scope.resolve_local_name(name_ref) { | 55 | if let Some(entry) = scope.resolve_local_name(name_ref) { |
56 | let nav = NavigationTarget::from_scope_entry(file_id, &entry); | 56 | let nav = NavigationTarget::from_scope_entry(file_id, &entry); |
57 | return Ok(Exact(nav)); | 57 | return Ok(Exact(nav)); |
58 | }; | 58 | }; |
59 | |||
60 | // Next check if it is a method | ||
61 | if let Some(method_call) = name_ref | ||
62 | .syntax() | ||
63 | .parent() | ||
64 | .and_then(ast::MethodCallExpr::cast) | ||
65 | { | ||
66 | let infer_result = function.infer(db)?; | ||
67 | let syntax_mapping = function.body_syntax_mapping(db)?; | ||
68 | let expr = ast::Expr::cast(method_call.syntax()).unwrap(); | ||
69 | if let Some(def_id) = syntax_mapping | ||
70 | .node_expr(expr) | ||
71 | .and_then(|it| infer_result.method_resolution(it)) | ||
72 | { | ||
73 | if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? { | ||
74 | return Ok(Exact(target)); | ||
75 | } | ||
76 | }; | ||
77 | } | ||
59 | } | 78 | } |
60 | // Then try module name resolution | 79 | // Then try module name resolution |
61 | if let Some(module) = | 80 | if let Some(module) = |
@@ -167,4 +186,32 @@ mod tests { | |||
167 | "foo SOURCE_FILE FileId(2) [0; 10)", | 186 | "foo SOURCE_FILE FileId(2) [0; 10)", |
168 | ); | 187 | ); |
169 | } | 188 | } |
189 | |||
190 | #[test] | ||
191 | fn goto_definition_works_for_methods() { | ||
192 | check_goto( | ||
193 | " | ||
194 | //- /lib.rs | ||
195 | struct Foo; | ||
196 | impl Foo { | ||
197 | fn frobnicate(&self) { } | ||
198 | } | ||
199 | |||
200 | fn bar(foo: &Foo) { | ||
201 | foo.frobnicate<|>(); | ||
202 | } | ||
203 | ", | ||
204 | "frobnicate FN_DEF FileId(1) [27; 52) [30; 40)", | ||
205 | ); | ||
206 | |||
207 | check_goto( | ||
208 | " | ||
209 | //- /lib.rs | ||
210 | mod <|>foo; | ||
211 | //- /foo/mod.rs | ||
212 | // empty | ||
213 | ", | ||
214 | "foo SOURCE_FILE FileId(2) [0; 10)", | ||
215 | ); | ||
216 | } | ||
170 | } | 217 | } |