aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr/scope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/expr/scope.rs')
-rw-r--r--crates/ra_hir/src/expr/scope.rs38
1 files changed, 9 insertions, 29 deletions
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs
index 887ad8dd8..23f1c5e7f 100644
--- a/crates/ra_hir/src/expr/scope.rs
+++ b/crates/ra_hir/src/expr/scope.rs
@@ -62,25 +62,11 @@ impl ExprScopes {
62 &self.scopes[scope].entries 62 &self.scopes[scope].entries
63 } 63 }
64 64
65 pub fn scope_chain_for<'a>(&'a self, expr: ExprId) -> impl Iterator<Item = ScopeId> + 'a { 65 pub fn scope_chain_for<'a>(
66 generate(self.scope_for(expr), move |&scope| {
67 self.scopes[scope].parent
68 })
69 }
70
71 pub fn resolve_local_name<'a>(
72 &'a self, 66 &'a self,
73 context_expr: ExprId, 67 scope: Option<ScopeId>,
74 name: Name, 68 ) -> impl Iterator<Item = ScopeId> + 'a {
75 ) -> Option<&'a ScopeEntry> { 69 generate(scope, move |&scope| self.scopes[scope].parent)
76 // TODO replace by Resolver::resolve_name
77 let mut shadowed = FxHashSet::default();
78 let ret = self
79 .scope_chain_for(context_expr)
80 .flat_map(|scope| self.entries(scope).iter())
81 .filter(|entry| shadowed.insert(entry.name()))
82 .find(|entry| entry.name() == &name);
83 ret
84 } 70 }
85 71
86 fn root_scope(&mut self) -> ScopeId { 72 fn root_scope(&mut self) -> ScopeId {
@@ -123,7 +109,7 @@ impl ExprScopes {
123 self.scope_for.insert(node, scope); 109 self.scope_for.insert(node, scope);
124 } 110 }
125 111
126 fn scope_for(&self, expr: ExprId) -> Option<ScopeId> { 112 pub fn scope_for(&self, expr: ExprId) -> Option<ScopeId> {
127 self.scope_for.get(&expr).map(|&scope| scope) 113 self.scope_for.get(&expr).map(|&scope| scope)
128 } 114 }
129} 115}
@@ -151,18 +137,14 @@ impl ScopeEntryWithSyntax {
151} 137}
152 138
153impl ScopesWithSyntaxMapping { 139impl ScopesWithSyntaxMapping {
154 pub fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator<Item = ScopeId> + 'a { 140 fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator<Item = ScopeId> + 'a {
155 generate(self.scope_for(node), move |&scope| { 141 generate(self.scope_for(node), move |&scope| {
156 self.scopes.scopes[scope].parent 142 self.scopes.scopes[scope].parent
157 }) 143 })
158 } 144 }
159 145
160 pub fn scope_chain_for_offset<'a>( 146 pub fn scope_for_offset<'a>(&'a self, offset: TextUnit) -> Option<ScopeId> {
161 &'a self, 147 self.scopes
162 offset: TextUnit,
163 ) -> impl Iterator<Item = ScopeId> + 'a {
164 let scope = self
165 .scopes
166 .scope_for 148 .scope_for
167 .iter() 149 .iter()
168 .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) 150 .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope)))
@@ -173,9 +155,7 @@ impl ScopesWithSyntaxMapping {
173 ptr.range().len(), 155 ptr.range().len(),
174 ) 156 )
175 }) 157 })
176 .map(|(ptr, scope)| self.adjust(ptr, *scope, offset)); 158 .map(|(ptr, scope)| self.adjust(ptr, *scope, offset))
177
178 generate(scope, move |&scope| self.scopes.scopes[scope].parent)
179 } 159 }
180 160
181 // XXX: during completion, cursor might be outside of any particular 161 // XXX: during completion, cursor might be outside of any particular