aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model_api.rs4
-rw-r--r--crates/ra_hir/src/expr/scope.rs26
2 files changed, 13 insertions, 17 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index c77b8acc7..7fc14634b 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -497,8 +497,8 @@ impl Function {
497 497
498 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping { 498 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping {
499 let scopes = db.expr_scopes(*self); 499 let scopes = db.expr_scopes(*self);
500 let syntax_mapping = db.body_with_source_map(*self).1; 500 let source_map = db.body_with_source_map(*self).1;
501 ScopesWithSyntaxMapping { scopes, syntax_mapping } 501 ScopesWithSyntaxMapping { scopes, source_map }
502 } 502 }
503 503
504 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> { 504 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs
index d5824eb81..578b10c65 100644
--- a/crates/ra_hir/src/expr/scope.rs
+++ b/crates/ra_hir/src/expr/scope.rs
@@ -109,7 +109,7 @@ impl ExprScopes {
109 109
110#[derive(Debug, Clone, PartialEq, Eq)] 110#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct ScopesWithSyntaxMapping { 111pub struct ScopesWithSyntaxMapping {
112 pub syntax_mapping: Arc<BodySourceMap>, 112 pub source_map: Arc<BodySourceMap>,
113 pub scopes: Arc<ExprScopes>, 113 pub scopes: Arc<ExprScopes>,
114} 114}
115 115
@@ -138,7 +138,7 @@ impl ScopesWithSyntaxMapping {
138 self.scopes 138 self.scopes
139 .scope_for 139 .scope_for
140 .iter() 140 .iter()
141 .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) 141 .filter_map(|(id, scope)| Some((self.source_map.expr_syntax(*id)?, scope)))
142 // find containing scope 142 // find containing scope
143 .min_by_key(|(ptr, _scope)| { 143 .min_by_key(|(ptr, _scope)| {
144 (!(ptr.range().start() <= offset && offset <= ptr.range().end()), ptr.range().len()) 144 (!(ptr.range().start() <= offset && offset <= ptr.range().end()), ptr.range().len())
@@ -155,7 +155,7 @@ impl ScopesWithSyntaxMapping {
155 .scopes 155 .scopes
156 .scope_for 156 .scope_for
157 .iter() 157 .iter()
158 .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) 158 .filter_map(|(id, scope)| Some((self.source_map.expr_syntax(*id)?, scope)))
159 .map(|(ptr, scope)| (ptr.range(), scope)) 159 .map(|(ptr, scope)| (ptr.range(), scope))
160 .filter(|(range, _)| range.start() <= offset && range.is_subrange(&r) && *range != r); 160 .filter(|(range, _)| range.start() <= offset && range.is_subrange(&r) && *range != r);
161 161
@@ -185,7 +185,7 @@ impl ScopesWithSyntaxMapping {
185 ret.and_then(|entry| { 185 ret.and_then(|entry| {
186 Some(ScopeEntryWithSyntax { 186 Some(ScopeEntryWithSyntax {
187 name: entry.name().clone(), 187 name: entry.name().clone(),
188 ptr: self.syntax_mapping.pat_syntax(entry.pat())?, 188 ptr: self.source_map.pat_syntax(entry.pat())?,
189 }) 189 })
190 }) 190 })
191 } 191 }
@@ -211,7 +211,7 @@ impl ScopesWithSyntaxMapping {
211 pub fn scope_for(&self, node: &SyntaxNode) -> Option<ScopeId> { 211 pub fn scope_for(&self, node: &SyntaxNode) -> Option<ScopeId> {
212 node.ancestors() 212 node.ancestors()
213 .map(SyntaxNodePtr::new) 213 .map(SyntaxNodePtr::new)
214 .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) 214 .filter_map(|ptr| self.source_map.syntax_expr(ptr))
215 .find_map(|it| self.scopes.scope_for(it)) 215 .find_map(|it| self.scopes.scope_for(it))
216 } 216 }
217} 217}
@@ -316,12 +316,10 @@ mod tests {
316 let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); 316 let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
317 let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); 317 let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
318 let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; 318 let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) };
319 let (body, syntax_mapping) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); 319 let (body, source_map) = expr::collect_fn_body_syntax(irrelevant_function, fn_def);
320 let scopes = ExprScopes::new(Arc::new(body)); 320 let scopes = ExprScopes::new(Arc::new(body));
321 let scopes = ScopesWithSyntaxMapping { 321 let scopes =
322 scopes: Arc::new(scopes), 322 ScopesWithSyntaxMapping { scopes: Arc::new(scopes), source_map: Arc::new(source_map) };
323 syntax_mapping: Arc::new(syntax_mapping),
324 };
325 let actual = scopes 323 let actual = scopes
326 .scope_chain(marker.syntax()) 324 .scope_chain(marker.syntax())
327 .flat_map(|scope| scopes.scopes.entries(scope)) 325 .flat_map(|scope| scopes.scopes.entries(scope))
@@ -417,12 +415,10 @@ mod tests {
417 let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); 415 let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();
418 416
419 let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; 417 let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) };
420 let (body, syntax_mapping) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); 418 let (body, source_map) = expr::collect_fn_body_syntax(irrelevant_function, fn_def);
421 let scopes = ExprScopes::new(Arc::new(body)); 419 let scopes = ExprScopes::new(Arc::new(body));
422 let scopes = ScopesWithSyntaxMapping { 420 let scopes =
423 scopes: Arc::new(scopes), 421 ScopesWithSyntaxMapping { scopes: Arc::new(scopes), source_map: Arc::new(source_map) };
424 syntax_mapping: Arc::new(syntax_mapping),
425 };
426 let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); 422 let local_name_entry = scopes.resolve_local_name(name_ref).unwrap();
427 let local_name = local_name_entry.ptr(); 423 let local_name = local_name_entry.ptr();
428 assert_eq!(local_name.range(), expected_name.syntax().range()); 424 assert_eq!(local_name.range(), expected_name.syntax().range());