aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr/scope.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-08 11:49:43 +0000
committerAleksey Kladov <[email protected]>2019-02-08 11:49:43 +0000
commit12e3b4c70b5ef23b2fdfc197296d483680e125f9 (patch)
tree71baa0e0a62f9f6b61450501c5f821f67badf9e4 /crates/ra_hir/src/expr/scope.rs
parent5cb1d41a30d25cbe136402644bf5434dd667f1e5 (diff)
reformat the world
Diffstat (limited to 'crates/ra_hir/src/expr/scope.rs')
-rw-r--r--crates/ra_hir/src/expr/scope.rs52
1 files changed, 11 insertions, 41 deletions
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs
index 368994bf7..44d5c2429 100644
--- a/crates/ra_hir/src/expr/scope.rs
+++ b/crates/ra_hir/src/expr/scope.rs
@@ -74,17 +74,11 @@ impl ExprScopes {
74 } 74 }
75 75
76 fn root_scope(&mut self) -> ScopeId { 76 fn root_scope(&mut self) -> ScopeId {
77 self.scopes.alloc(ScopeData { 77 self.scopes.alloc(ScopeData { parent: None, entries: vec![] })
78 parent: None,
79 entries: vec![],
80 })
81 } 78 }
82 79
83 fn new_scope(&mut self, parent: ScopeId) -> ScopeId { 80 fn new_scope(&mut self, parent: ScopeId) -> ScopeId {
84 self.scopes.alloc(ScopeData { 81 self.scopes.alloc(ScopeData { parent: Some(parent), entries: vec![] })
85 parent: Some(parent),
86 entries: vec![],
87 })
88 } 82 }
89 83
90 fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { 84 fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) {
@@ -92,10 +86,7 @@ impl ExprScopes {
92 Pat::Bind { name, .. } => { 86 Pat::Bind { name, .. } => {
93 // bind can have a subpattern, but it's actually not allowed 87 // bind can have a subpattern, but it's actually not allowed
94 // to bind to things in there 88 // to bind to things in there
95 let entry = ScopeEntry { 89 let entry = ScopeEntry { name: name.clone(), pat };
96 name: name.clone(),
97 pat,
98 };
99 self.scopes[scope].entries.push(entry) 90 self.scopes[scope].entries.push(entry)
100 } 91 }
101 p => p.walk_child_pats(|pat| self.add_bindings(body, scope, pat)), 92 p => p.walk_child_pats(|pat| self.add_bindings(body, scope, pat)),
@@ -104,9 +95,7 @@ impl ExprScopes {
104 95
105 fn add_params_bindings(&mut self, scope: ScopeId, params: &[PatId]) { 96 fn add_params_bindings(&mut self, scope: ScopeId, params: &[PatId]) {
106 let body = Arc::clone(&self.body); 97 let body = Arc::clone(&self.body);
107 params 98 params.iter().for_each(|pat| self.add_bindings(&body, scope, *pat));
108 .iter()
109 .for_each(|pat| self.add_bindings(&body, scope, *pat));
110 } 99 }
111 100
112 fn set_scope(&mut self, node: ExprId, scope: ScopeId) { 101 fn set_scope(&mut self, node: ExprId, scope: ScopeId) {
@@ -142,9 +131,7 @@ impl ScopeEntryWithSyntax {
142 131
143impl ScopesWithSyntaxMapping { 132impl ScopesWithSyntaxMapping {
144 fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator<Item = ScopeId> + 'a { 133 fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator<Item = ScopeId> + 'a {
145 generate(self.scope_for(node), move |&scope| { 134 generate(self.scope_for(node), move |&scope| self.scopes.scopes[scope].parent)
146 self.scopes.scopes[scope].parent
147 })
148 } 135 }
149 136
150 pub fn scope_for_offset(&self, offset: TextUnit) -> Option<ScopeId> { 137 pub fn scope_for_offset(&self, offset: TextUnit) -> Option<ScopeId> {
@@ -154,10 +141,7 @@ impl ScopesWithSyntaxMapping {
154 .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) 141 .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope)))
155 // find containing scope 142 // find containing scope
156 .min_by_key(|(ptr, _scope)| { 143 .min_by_key(|(ptr, _scope)| {
157 ( 144 (!(ptr.range().start() <= offset && offset <= ptr.range().end()), ptr.range().len())
158 !(ptr.range().start() <= offset && offset <= ptr.range().end()),
159 ptr.range().len(),
160 )
161 }) 145 })
162 .map(|(ptr, scope)| self.adjust(ptr, *scope, offset)) 146 .map(|(ptr, scope)| self.adjust(ptr, *scope, offset))
163 } 147 }
@@ -251,9 +235,7 @@ fn compute_block_scopes(
251) { 235) {
252 for stmt in statements { 236 for stmt in statements {
253 match stmt { 237 match stmt {
254 Statement::Let { 238 Statement::Let { pat, initializer, .. } => {
255 pat, initializer, ..
256 } => {
257 if let Some(expr) = initializer { 239 if let Some(expr) = initializer {
258 scopes.set_scope(*expr, scope); 240 scopes.set_scope(*expr, scope);
259 compute_expr_scopes(*expr, body, scopes, scope); 241 compute_expr_scopes(*expr, body, scopes, scope);
@@ -278,21 +260,13 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
278 Expr::Block { statements, tail } => { 260 Expr::Block { statements, tail } => {
279 compute_block_scopes(&statements, *tail, body, scopes, scope); 261 compute_block_scopes(&statements, *tail, body, scopes, scope);
280 } 262 }
281 Expr::For { 263 Expr::For { iterable, pat, body: body_expr } => {
282 iterable,
283 pat,
284 body: body_expr,
285 } => {
286 compute_expr_scopes(*iterable, body, scopes, scope); 264 compute_expr_scopes(*iterable, body, scopes, scope);
287 let scope = scopes.new_scope(scope); 265 let scope = scopes.new_scope(scope);
288 scopes.add_bindings(body, scope, *pat); 266 scopes.add_bindings(body, scope, *pat);
289 compute_expr_scopes(*body_expr, body, scopes, scope); 267 compute_expr_scopes(*body_expr, body, scopes, scope);
290 } 268 }
291 Expr::Lambda { 269 Expr::Lambda { args, body: body_expr, .. } => {
292 args,
293 body: body_expr,
294 ..
295 } => {
296 let scope = scopes.new_scope(scope); 270 let scope = scopes.new_scope(scope);
297 scopes.add_params_bindings(scope, &args); 271 scopes.add_params_bindings(scope, &args);
298 compute_expr_scopes(*body_expr, body, scopes, scope); 272 compute_expr_scopes(*body_expr, body, scopes, scope);
@@ -341,9 +315,7 @@ mod tests {
341 let file = SourceFile::parse(&code); 315 let file = SourceFile::parse(&code);
342 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();
343 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();
344 let irrelevant_function = Function { 318 let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) };
345 id: crate::ids::FunctionId::from_raw(0.into()),
346 };
347 let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def); 319 let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def);
348 let scopes = ExprScopes::new(Arc::clone(body_hir.body())); 320 let scopes = ExprScopes::new(Arc::clone(body_hir.body()));
349 let scopes = ScopesWithSyntaxMapping { 321 let scopes = ScopesWithSyntaxMapping {
@@ -444,9 +416,7 @@ mod tests {
444 let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); 416 let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
445 let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); 417 let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();
446 418
447 let irrelevant_function = Function { 419 let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) };
448 id: crate::ids::FunctionId::from_raw(0.into()),
449 };
450 let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def); 420 let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def);
451 let scopes = ExprScopes::new(Arc::clone(body_hir.body())); 421 let scopes = ExprScopes::new(Arc::clone(body_hir.body()));
452 let scopes = ScopesWithSyntaxMapping { 422 let scopes = ScopesWithSyntaxMapping {