aboutsummaryrefslogtreecommitdiff
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
parent33ff7b56ff353410e7bcb7aed27004d4f0a57d8e (diff)
Use the new Resolver API for goto def
-rw-r--r--crates/ra_hir/src/expr.rs4
-rw-r--r--crates/ra_hir/src/expr/scope.rs6
-rw-r--r--crates/ra_hir/src/resolve.rs26
-rw-r--r--crates/ra_hir/src/source_binder.rs28
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs49
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs11
6 files changed, 89 insertions, 35 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 6c294bf10..e78ba889e 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -69,6 +69,10 @@ impl Body {
69 pub fn owner(&self) -> Function { 69 pub fn owner(&self) -> Function {
70 self.owner 70 self.owner
71 } 71 }
72
73 pub fn syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> {
74 db.body_syntax_mapping(self.owner)
75 }
72} 76}
73 77
74// needs arbitrary_self_types to be a method... or maybe move to the def? 78// needs arbitrary_self_types to be a method... or maybe move to the def?
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs
index 23f1c5e7f..9202e3671 100644
--- a/crates/ra_hir/src/expr/scope.rs
+++ b/crates/ra_hir/src/expr/scope.rs
@@ -58,6 +58,10 @@ impl ExprScopes {
58 scopes 58 scopes
59 } 59 }
60 60
61 pub fn body(&self) -> Arc<Body> {
62 self.body.clone()
63 }
64
61 pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { 65 pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
62 &self.scopes[scope].entries 66 &self.scopes[scope].entries
63 } 67 }
@@ -220,7 +224,7 @@ impl ScopesWithSyntaxMapping {
220 .collect() 224 .collect()
221 } 225 }
222 226
223 fn scope_for(&self, node: &SyntaxNode) -> Option<ScopeId> { 227 pub fn scope_for(&self, node: &SyntaxNode) -> Option<ScopeId> {
224 node.ancestors() 228 node.ancestors()
225 .map(SyntaxNodePtr::new) 229 .map(SyntaxNodePtr::new)
226 .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) 230 .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr))
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 30cf9c69e..871f7d8f7 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -9,7 +9,7 @@ use crate::{
9 name::{Name, KnownName}, 9 name::{Name, KnownName},
10 nameres::{PerNs, ItemMap}, 10 nameres::{PerNs, ItemMap},
11 generics::GenericParams, 11 generics::GenericParams,
12 expr::{scope::{ExprScopes, ScopeId}, PatId}, 12 expr::{scope::{ExprScopes, ScopeId}, PatId, Body},
13 impl_block::ImplBlock, 13 impl_block::ImplBlock,
14 path::Path, 14 path::Path,
15}; 15};
@@ -106,15 +106,21 @@ impl Resolver {
106 } 106 }
107 107
108 fn module(&self) -> Option<(&ItemMap, Module)> { 108 fn module(&self) -> Option<(&ItemMap, Module)> {
109 for scope in self.scopes.iter().rev() { 109 self.scopes.iter().rev().find_map(|scope| match scope {
110 match scope { 110 Scope::ModuleScope(m) => Some((&*m.item_map, m.module.clone())),
111 Scope::ModuleScope(m) => { 111
112 return Some((&m.item_map, m.module.clone())); 112 Scope::ModuleScopeRef(m) => Some((m.item_map, m.module.clone())),
113 } 113
114 _ => {} 114 _ => None,
115 } 115 })
116 } 116 }
117 None 117
118 /// The body from which any `LocalBinding` resolutions in this resolver come.
119 pub fn body(&self) -> Option<Arc<Body>> {
120 self.scopes.iter().rev().find_map(|scope| match scope {
121 Scope::ExprScope(expr_scope) => Some(expr_scope.expr_scopes.body()),
122 _ => None,
123 })
118 } 124 }
119} 125}
120 126
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 998158b3e..621215bfb 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -229,3 +229,31 @@ pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> R
229 }) 229 })
230 .unwrap_or_default() 230 .unwrap_or_default()
231} 231}
232
233pub fn resolver_for_node(
234 db: &impl HirDatabase,
235 file_id: FileId,
236 node: &SyntaxNode,
237) -> Resolver<'static> {
238 node.ancestors()
239 .find_map(|node| {
240 if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() {
241 if let Some(func) = function_from_child_node(db, file_id, node) {
242 let scopes = func.scopes(db);
243 let scope = scopes.scope_for(&node);
244 Some(expr::resolver_for_scope(func.body(db), db, scope))
245 } else {
246 // TODO const/static/array length
247 None
248 }
249 } else if let Some(module) = ast::Module::cast(node) {
250 Some(module_from_declaration(db, file_id, module)?.resolver(db))
251 } else if let Some(_) = ast::SourceFile::cast(node) {
252 Some(module_from_source(db, file_id.into(), None)?.resolver(db))
253 } else {
254 // TODO add missing cases
255 None
256 }
257 })
258 .unwrap_or_default()
259}
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 }