aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/hir/function
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/hir/function')
-rw-r--r--crates/ra_analysis/src/hir/function/mod.rs2
-rw-r--r--crates/ra_analysis/src/hir/function/scope.rs29
2 files changed, 15 insertions, 16 deletions
diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs
index 5de9806e3..8161a604f 100644
--- a/crates/ra_analysis/src/hir/function/mod.rs
+++ b/crates/ra_analysis/src/hir/function/mod.rs
@@ -15,7 +15,7 @@ use crate::{
15 syntax_ptr::SyntaxPtr, FileId, 15 syntax_ptr::SyntaxPtr, FileId,
16}; 16};
17 17
18pub(crate) use self::scope::{resolve_local_name, FnScopes}; 18pub(crate) use self::scope::FnScopes;
19pub(crate) use crate::loc2id::FnId; 19pub(crate) use crate::loc2id::FnId;
20 20
21impl FnId { 21impl FnId {
diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs
index 5307a0a8e..b8bdebe47 100644
--- a/crates/ra_analysis/src/hir/function/scope.rs
+++ b/crates/ra_analysis/src/hir/function/scope.rs
@@ -57,6 +57,19 @@ impl FnScopes {
57 self.scopes[scope].parent 57 self.scopes[scope].parent
58 }) 58 })
59 } 59 }
60 pub(crate) fn resolve_local_name<'a>(
61 &'a self,
62 name_ref: ast::NameRef,
63 ) -> Option<&'a ScopeEntry> {
64 let mut shadowed = FxHashSet::default();
65 let ret = self
66 .scope_chain(name_ref.syntax())
67 .flat_map(|scope| self.entries(scope).iter())
68 .filter(|entry| shadowed.insert(entry.name()))
69 .filter(|entry| entry.name() == &name_ref.text())
70 .nth(0);
71 ret
72 }
60 fn root_scope(&mut self) -> ScopeId { 73 fn root_scope(&mut self) -> ScopeId {
61 self.scopes.alloc(ScopeData { 74 self.scopes.alloc(ScopeData {
62 parent: None, 75 parent: None,
@@ -249,20 +262,6 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) {
249 } 262 }
250} 263}
251 264
252pub fn resolve_local_name<'a>(
253 name_ref: ast::NameRef,
254 scopes: &'a FnScopes,
255) -> Option<&'a ScopeEntry> {
256 let mut shadowed = FxHashSet::default();
257 let ret = scopes
258 .scope_chain(name_ref.syntax())
259 .flat_map(|scope| scopes.entries(scope).iter())
260 .filter(|entry| shadowed.insert(entry.name()))
261 .filter(|entry| entry.name() == &name_ref.text())
262 .nth(0);
263 ret
264}
265
266#[cfg(test)] 265#[cfg(test)]
267mod tests { 266mod tests {
268 use ra_editor::find_node_at_offset; 267 use ra_editor::find_node_at_offset;
@@ -376,7 +375,7 @@ mod tests {
376 375
377 let scopes = FnScopes::new(fn_def); 376 let scopes = FnScopes::new(fn_def);
378 377
379 let local_name_entry = resolve_local_name(name_ref, &scopes).unwrap(); 378 let local_name_entry = scopes.resolve_local_name(name_ref).unwrap();
380 let local_name = local_name_entry.ptr().resolve(&file); 379 let local_name = local_name_entry.ptr().resolve(&file);
381 let expected_name = 380 let expected_name =
382 find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap(); 381 find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap();