diff options
Diffstat (limited to 'crates/ra_ide_api/src/goto_definition.rs')
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 2a20c20ee..f7dd28c7c 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -48,14 +48,7 @@ pub(crate) fn reference_definition( | |||
48 | if let Some(function) = | 48 | if let Some(function) = |
49 | hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax()) | 49 | hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax()) |
50 | { | 50 | { |
51 | let scope = function.scopes(db); | 51 | // Check if it is a method |
52 | // First try to resolve the symbol locally | ||
53 | if let Some(entry) = scope.resolve_local_name(name_ref) { | ||
54 | let nav = NavigationTarget::from_scope_entry(file_id, &entry); | ||
55 | return Exact(nav); | ||
56 | }; | ||
57 | |||
58 | // Next check if it is a method | ||
59 | if let Some(method_call) = name_ref | 52 | if let Some(method_call) = name_ref |
60 | .syntax() | 53 | .syntax() |
61 | .parent() | 54 | .parent() |
@@ -86,19 +79,37 @@ pub(crate) fn reference_definition( | |||
86 | }; | 79 | }; |
87 | } | 80 | } |
88 | } | 81 | } |
89 | // Then try module name resolution | 82 | // Try name resolution |
90 | if let Some(module) = hir::source_binder::module_from_child_node(db, file_id, name_ref.syntax()) | 83 | let resolver = hir::source_binder::resolver_for_node(db, file_id, name_ref.syntax()); |
84 | if let Some(path) = name_ref | ||
85 | .syntax() | ||
86 | .ancestors() | ||
87 | .find_map(ast::Path::cast) | ||
88 | .and_then(hir::Path::from_ast) | ||
91 | { | 89 | { |
92 | if let Some(path) = name_ref | 90 | let resolved = resolver.resolve_path(db, &path); |
93 | .syntax() | 91 | match resolved.clone().take_types().or(resolved.take_values()) { |
94 | .ancestors() | 92 | Some(Resolution::Def { def }) => return Exact(NavigationTarget::from_def(db, def)), |
95 | .find_map(ast::Path::cast) | 93 | Some(Resolution::LocalBinding { pat }) => { |
96 | .and_then(hir::Path::from_ast) | 94 | let body = resolver.body().expect("no body for local binding"); |
97 | { | 95 | let syntax_mapping = body.syntax_mapping(db); |
98 | let resolved = module.resolve_path(db, &path); | 96 | let ptr = syntax_mapping |
99 | if let Some(def_id) = resolved.take_types().or(resolved.take_values()) { | 97 | .pat_syntax(pat) |
100 | return Exact(NavigationTarget::from_def(db, def_id)); | 98 | .expect("pattern not found in syntax mapping"); |
99 | let name = path | ||
100 | .as_ident() | ||
101 | .cloned() | ||
102 | .expect("local binding from a multi-segment path"); | ||
103 | let nav = NavigationTarget::from_scope_entry(file_id, name, ptr); | ||
104 | return Exact(nav); | ||
105 | } | ||
106 | Some(Resolution::GenericParam { .. }) => { | ||
107 | // TODO go to the generic param def | ||
108 | } | ||
109 | Some(Resolution::SelfType(_impl_block)) => { | ||
110 | // TODO go to the implemented type | ||
101 | } | 111 | } |
112 | None => {} | ||
102 | } | 113 | } |
103 | } | 114 | } |
104 | // If that fails try the index based approach. | 115 | // If that fails try the index based approach. |