aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/scope.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-27 19:02:47 +0100
committerAleksey Kladov <[email protected]>2018-08-27 19:02:47 +0100
commit7f4b07a9076a38b2bd0fa0518ba090311dcaf880 (patch)
treeb7482d9a3f1b32cf6017493e43f8b1078e601d39 /crates/libeditor/src/scope.rs
parentaaca7d003bd969785be53d8f312b67bfa26f6272 (diff)
Refactor
Diffstat (limited to 'crates/libeditor/src/scope.rs')
-rw-r--r--crates/libeditor/src/scope.rs32
1 files changed, 14 insertions, 18 deletions
diff --git a/crates/libeditor/src/scope.rs b/crates/libeditor/src/scope.rs
index 1fec0b24e..76104b2cf 100644
--- a/crates/libeditor/src/scope.rs
+++ b/crates/libeditor/src/scope.rs
@@ -9,20 +9,6 @@ use libsyntax2::{
9 algo::{ancestors, generate, walk::preorder} 9 algo::{ancestors, generate, walk::preorder}
10}; 10};
11 11
12pub fn compute_scopes(fn_def: ast::FnDef) -> FnScopes {
13 let mut scopes = FnScopes::new();
14 let root = scopes.root_scope();
15 fn_def.param_list().into_iter()
16 .flat_map(|it| it.params())
17 .filter_map(|it| it.pat())
18 .for_each(|it| scopes.add_bindings(root, it));
19
20 if let Some(body) = fn_def.body() {
21 compute_block_scopes(body, &mut scopes, root)
22 }
23 scopes
24}
25
26fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) { 12fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) {
27 for stmt in block.statements() { 13 for stmt in block.statements() {
28 match stmt { 14 match stmt {
@@ -106,11 +92,21 @@ pub struct FnScopes {
106} 92}
107 93
108impl FnScopes { 94impl FnScopes {
109 fn new() -> FnScopes { 95 pub fn new(fn_def: ast::FnDef) -> FnScopes {
110 FnScopes { 96 let mut scopes = FnScopes {
111 scopes: vec![], 97 scopes: Vec::new(),
112 scope_for: HashMap::new(), 98 scope_for: HashMap::new()
99 };
100 let root = scopes.root_scope();
101 fn_def.param_list().into_iter()
102 .flat_map(|it| it.params())
103 .filter_map(|it| it.pat())
104 .for_each(|it| scopes.add_bindings(root, it));
105
106 if let Some(body) = fn_def.body() {
107 compute_block_scopes(body, &mut scopes, root)
113 } 108 }
109 scopes
114 } 110 }
115 pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { 111 pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
116 &self.scopes[scope].entries 112 &self.scopes[scope].entries