aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-01-29 19:49:31 +0000
committerFlorian Diebold <[email protected]>2019-02-01 21:45:43 +0000
commitafce8e442639fa9ed954b3659a2d1eccb7d80113 (patch)
tree38015aa82986dc69f5b60cfb21fe6062a37d8006 /crates/ra_ide_api
parent33ff7b56ff353410e7bcb7aed27004d4f0a57d8e (diff)
Use the new Resolver API for goto def
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs49
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs11
2 files changed, 36 insertions, 24 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.
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs
index 5ccb5cc2e..617908aed 100644
--- a/crates/ra_ide_api/src/navigation_target.rs
+++ b/crates/ra_ide_api/src/navigation_target.rs
@@ -1,9 +1,9 @@
1use ra_db::FileId; 1use ra_db::FileId;
2use ra_syntax::{ 2use ra_syntax::{
3 SyntaxNode, AstNode, SmolStr, TextRange, ast, 3 SyntaxNode, SyntaxNodePtr, AstNode, SmolStr, TextRange, ast,
4 SyntaxKind::{self, NAME}, 4 SyntaxKind::{self, NAME},
5}; 5};
6use hir::{ModuleSource, FieldSource}; 6use hir::{ModuleSource, FieldSource, Name};
7 7
8use crate::{FileSymbol, db::RootDatabase}; 8use crate::{FileSymbol, db::RootDatabase};
9 9
@@ -58,12 +58,13 @@ impl NavigationTarget {
58 58
59 pub(crate) fn from_scope_entry( 59 pub(crate) fn from_scope_entry(
60 file_id: FileId, 60 file_id: FileId,
61 entry: &hir::ScopeEntryWithSyntax, 61 name: Name,
62 ptr: SyntaxNodePtr,
62 ) -> NavigationTarget { 63 ) -> NavigationTarget {
63 NavigationTarget { 64 NavigationTarget {
64 file_id, 65 file_id,
65 name: entry.name().to_string().into(), 66 name: name.to_string().into(),
66 full_range: entry.ptr().range(), 67 full_range: ptr.range(),
67 focus_range: None, 68 focus_range: None,
68 kind: NAME, 69 kind: NAME,
69 } 70 }