aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-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
4 files changed, 53 insertions, 11 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}