aboutsummaryrefslogtreecommitdiff
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
parentaaca7d003bd969785be53d8f312b67bfa26f6272 (diff)
Refactor
-rw-r--r--crates/libeditor/src/completion.rs4
-rw-r--r--crates/libeditor/src/scope.rs32
2 files changed, 16 insertions, 20 deletions
diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs
index 242a3a434..cea2d14d1 100644
--- a/crates/libeditor/src/completion.rs
+++ b/crates/libeditor/src/completion.rs
@@ -8,7 +8,7 @@ use libsyntax2::{
8 8
9use { 9use {
10 AtomEdit, find_node_at_offset, 10 AtomEdit, find_node_at_offset,
11 scope::{FnScopes, compute_scopes}, 11 scope::FnScopes,
12}; 12};
13 13
14#[derive(Debug)] 14#[derive(Debug)]
@@ -25,7 +25,7 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI
25 }; 25 };
26 let name_ref = find_node_at_offset::<ast::NameRef>(file.syntax(), offset)?; 26 let name_ref = find_node_at_offset::<ast::NameRef>(file.syntax(), offset)?;
27 let fn_def = ancestors(name_ref.syntax()).filter_map(ast::FnDef::cast).next()?; 27 let fn_def = ancestors(name_ref.syntax()).filter_map(ast::FnDef::cast).next()?;
28 let scopes = compute_scopes(fn_def); 28 let scopes = FnScopes::new(fn_def);
29 Some(complete(name_ref, &scopes)) 29 Some(complete(name_ref, &scopes))
30} 30}
31 31
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