aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/scope
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-09-16 10:54:24 +0100
committerAleksey Kladov <[email protected]>2018-09-16 11:07:39 +0100
commitb5021411a84822cb3f1e3aeffad9550dd15bdeb6 (patch)
tree9dca564f8e51b298dced01c4ce669c756dce3142 /crates/libeditor/src/scope
parentba0bfeee12e19da40b5eabc8d0408639af10e96f (diff)
rename all things
Diffstat (limited to 'crates/libeditor/src/scope')
-rw-r--r--crates/libeditor/src/scope/fn_scope.rs329
-rw-r--r--crates/libeditor/src/scope/mod.rs8
-rw-r--r--crates/libeditor/src/scope/mod_scope.rs115
3 files changed, 0 insertions, 452 deletions
diff --git a/crates/libeditor/src/scope/fn_scope.rs b/crates/libeditor/src/scope/fn_scope.rs
deleted file mode 100644
index 60b8ce919..000000000
--- a/crates/libeditor/src/scope/fn_scope.rs
+++ /dev/null
@@ -1,329 +0,0 @@
1use std::{
2 fmt,
3 collections::HashMap,
4};
5
6use libsyntax2::{
7 SyntaxNodeRef, SyntaxNode, SmolStr, AstNode,
8 ast::{self, NameOwner, LoopBodyOwner, ArgListOwner},
9 algo::{ancestors, generate, walk::preorder}
10};
11
12type ScopeId = usize;
13
14#[derive(Debug)]
15pub struct FnScopes {
16 pub self_param: Option<SyntaxNode>,
17 scopes: Vec<ScopeData>,
18 scope_for: HashMap<SyntaxNode, ScopeId>,
19}
20
21impl FnScopes {
22 pub fn new(fn_def: ast::FnDef) -> FnScopes {
23 let mut scopes = FnScopes {
24 self_param: fn_def.param_list()
25 .and_then(|it| it.self_param())
26 .map(|it| it.syntax().owned()),
27 scopes: Vec::new(),
28 scope_for: HashMap::new()
29 };
30 let root = scopes.root_scope();
31 scopes.add_params_bindings(root, fn_def.param_list());
32 if let Some(body) = fn_def.body() {
33 compute_block_scopes(body, &mut scopes, root)
34 }
35 scopes
36 }
37 pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
38 &self.scopes[scope].entries
39 }
40 pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator<Item=ScopeId> + 'a {
41 generate(self.scope_for(node), move |&scope| self.scopes[scope].parent)
42 }
43 fn root_scope(&mut self) -> ScopeId {
44 let res = self.scopes.len();
45 self.scopes.push(ScopeData { parent: None, entries: vec![] });
46 res
47 }
48 fn new_scope(&mut self, parent: ScopeId) -> ScopeId {
49 let res = self.scopes.len();
50 self.scopes.push(ScopeData { parent: Some(parent), entries: vec![] });
51 res
52 }
53 fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) {
54 let entries = preorder(pat.syntax())
55 .filter_map(ast::BindPat::cast)
56 .filter_map(ScopeEntry::new);
57 self.scopes[scope].entries.extend(entries);
58 }
59 fn add_params_bindings(&mut self, scope: ScopeId, params: Option<ast::ParamList>) {
60 params.into_iter()
61 .flat_map(|it| it.params())
62 .filter_map(|it| it.pat())
63 .for_each(|it| self.add_bindings(scope, it));
64 }
65 fn set_scope(&mut self, node: SyntaxNodeRef, scope: ScopeId) {
66 self.scope_for.insert(node.owned(), scope);
67 }
68 fn scope_for(&self, node: SyntaxNodeRef) -> Option<ScopeId> {
69 ancestors(node)
70 .filter_map(|it| self.scope_for.get(&it.owned()).map(|&scope| scope))
71 .next()
72 }
73}
74
75pub struct ScopeEntry {
76 syntax: SyntaxNode
77}
78
79impl ScopeEntry {
80 fn new(pat: ast::BindPat) -> Option<ScopeEntry> {
81 if pat.name().is_some() {
82 Some(ScopeEntry { syntax: pat.syntax().owned() })
83 } else {
84 None
85 }
86 }
87 pub fn name(&self) -> SmolStr {
88 self.ast().name()
89 .unwrap()
90 .text()
91 }
92 fn ast(&self) -> ast::BindPat {
93 ast::BindPat::cast(self.syntax.borrowed())
94 .unwrap()
95 }
96}
97
98impl fmt::Debug for ScopeEntry {
99 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100 f.debug_struct("ScopeEntry")
101 .field("name", &self.name())
102 .field("syntax", &self.syntax)
103 .finish()
104 }
105}
106
107fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) {
108 for stmt in block.statements() {
109 match stmt {
110 ast::Stmt::LetStmt(stmt) => {
111 if let Some(expr) = stmt.initializer() {
112 scopes.set_scope(expr.syntax(), scope);
113 compute_expr_scopes(expr, scopes, scope);
114 }
115 scope = scopes.new_scope(scope);
116 if let Some(pat) = stmt.pat() {
117 scopes.add_bindings(scope, pat);
118 }
119 }
120 ast::Stmt::ExprStmt(expr_stmt) => {
121 if let Some(expr) = expr_stmt.expr() {
122 scopes.set_scope(expr.syntax(), scope);
123 compute_expr_scopes(expr, scopes, scope);
124 }
125 }
126 }
127 }
128 if let Some(expr) = block.expr() {
129 scopes.set_scope(expr.syntax(), scope);
130 compute_expr_scopes(expr, scopes, scope);
131 }
132}
133
134fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) {
135 match expr {
136 ast::Expr::IfExpr(e) => {
137 let cond_scope = e.condition().and_then(|cond| {
138 compute_cond_scopes(cond, scopes, scope)
139 });
140 if let Some(block) = e.then_branch() {
141 compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope));
142 }
143 if let Some(block) = e.else_branch() {
144 compute_block_scopes(block, scopes, scope);
145 }
146 },
147 ast::Expr::BlockExpr(e) => {
148 if let Some(block) = e.block() {
149 compute_block_scopes(block, scopes, scope);
150 }
151 }
152 ast::Expr::LoopExpr(e) => {
153 if let Some(block) = e.loop_body() {
154 compute_block_scopes(block, scopes, scope);
155 }
156 }
157 ast::Expr::WhileExpr(e) => {
158 let cond_scope = e.condition().and_then(|cond| {
159 compute_cond_scopes(cond, scopes, scope)
160 });
161 if let Some(block) = e.loop_body() {
162 compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope));
163 }
164 }
165 ast::Expr::ForExpr(e) => {
166 if let Some(expr) = e.iterable() {
167 compute_expr_scopes(expr, scopes, scope);
168 }
169 let mut scope = scope;
170 if let Some(pat) = e.pat() {
171 scope = scopes.new_scope(scope);
172 scopes.add_bindings(scope, pat);
173 }
174 if let Some(block) = e.loop_body() {
175 compute_block_scopes(block, scopes, scope);
176 }
177 }
178 ast::Expr::LambdaExpr(e) => {
179 let mut scope = scopes.new_scope(scope);
180 scopes.add_params_bindings(scope, e.param_list());
181 if let Some(body) = e.body() {
182 scopes.set_scope(body.syntax(), scope);
183 compute_expr_scopes(body, scopes, scope);
184 }
185 }
186 ast::Expr::CallExpr(e) => {
187 compute_call_scopes(e.expr(), e.arg_list(), scopes, scope);
188 }
189 ast::Expr::MethodCallExpr(e) => {
190 compute_call_scopes(e.expr(), e.arg_list(), scopes, scope);
191 }
192 ast::Expr::MatchExpr(e) => {
193 if let Some(expr) = e.expr() {
194 compute_expr_scopes(expr, scopes, scope);
195 }
196 for arm in e.match_arm_list().into_iter().flat_map(|it| it.arms()) {
197 let scope = scopes.new_scope(scope);
198 for pat in arm.pats() {
199 scopes.add_bindings(scope, pat);
200 }
201 if let Some(expr) = arm.expr() {
202 compute_expr_scopes(expr, scopes, scope);
203 }
204 }
205 }
206 _ => {
207 expr.syntax().children()
208 .filter_map(ast::Expr::cast)
209 .for_each(|expr| compute_expr_scopes(expr, scopes, scope))
210 }
211 };
212
213 fn compute_call_scopes(
214 receiver: Option<ast::Expr>,
215 arg_list: Option<ast::ArgList>,
216 scopes: &mut FnScopes, scope: ScopeId,
217 ) {
218 arg_list.into_iter()
219 .flat_map(|it| it.args())
220 .chain(receiver)
221 .for_each(|expr| compute_expr_scopes(expr, scopes, scope));
222 }
223
224 fn compute_cond_scopes(cond: ast::Condition, scopes: &mut FnScopes, scope: ScopeId) -> Option<ScopeId> {
225 if let Some(expr) = cond.expr() {
226 compute_expr_scopes(expr, scopes, scope);
227 }
228 if let Some(pat) = cond.pat() {
229 let s = scopes.new_scope(scope);
230 scopes.add_bindings(s, pat);
231 Some(s)
232 } else {
233 None
234 }
235 }
236}
237
238#[derive(Debug)]
239struct ScopeData {
240 parent: Option<ScopeId>,
241 entries: Vec<ScopeEntry>
242}
243
244#[cfg(test)]
245mod tests {
246 use super::*;
247 use libsyntax2::File;
248 use {find_node_at_offset, test_utils::extract_offset};
249
250 fn do_check(code: &str, expected: &[&str]) {
251 let (off, code) = extract_offset(code);
252 let code = {
253 let mut buf = String::new();
254 let off = u32::from(off) as usize;
255 buf.push_str(&code[..off]);
256 buf.push_str("marker");
257 buf.push_str(&code[off..]);
258 buf
259 };
260 let file = File::parse(&code);
261 let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
262 let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
263 let scopes = FnScopes::new(fn_def);
264 let actual = scopes.scope_chain(marker.syntax())
265 .flat_map(|scope| scopes.entries(scope))
266 .map(|it| it.name())
267 .collect::<Vec<_>>();
268 assert_eq!(expected, actual.as_slice());
269 }
270
271 #[test]
272 fn test_lambda_scope() {
273 do_check(r"
274 fn quux(foo: i32) {
275 let f = |bar, baz: i32| {
276 <|>
277 };
278 }",
279 &["bar", "baz", "foo"],
280 );
281 }
282
283 #[test]
284 fn test_call_scope() {
285 do_check(r"
286 fn quux() {
287 f(|x| <|> );
288 }",
289 &["x"],
290 );
291 }
292
293 #[test]
294 fn test_metod_call_scope() {
295 do_check(r"
296 fn quux() {
297 z.f(|x| <|> );
298 }",
299 &["x"],
300 );
301 }
302
303 #[test]
304 fn test_loop_scope() {
305 do_check(r"
306 fn quux() {
307 loop {
308 let x = ();
309 <|>
310 };
311 }",
312 &["x"],
313 );
314 }
315
316 #[test]
317 fn test_match() {
318 do_check(r"
319 fn quux() {
320 match () {
321 Some(x) => {
322 <|>
323 }
324 };
325 }",
326 &["x"],
327 );
328 }
329}
diff --git a/crates/libeditor/src/scope/mod.rs b/crates/libeditor/src/scope/mod.rs
deleted file mode 100644
index 2f25230f8..000000000
--- a/crates/libeditor/src/scope/mod.rs
+++ /dev/null
@@ -1,8 +0,0 @@
1mod fn_scope;
2mod mod_scope;
3
4pub use self::{
5 fn_scope::FnScopes,
6 mod_scope::ModuleScope,
7};
8
diff --git a/crates/libeditor/src/scope/mod_scope.rs b/crates/libeditor/src/scope/mod_scope.rs
deleted file mode 100644
index 0ec56a206..000000000
--- a/crates/libeditor/src/scope/mod_scope.rs
+++ /dev/null
@@ -1,115 +0,0 @@
1use libsyntax2::{
2 AstNode, SyntaxNode, SyntaxNodeRef, SmolStr,
3 ast::{self, AstChildren},
4};
5
6pub struct ModuleScope {
7 entries: Vec<Entry>,
8}
9
10pub struct Entry {
11 node: SyntaxNode,
12 kind: EntryKind,
13}
14
15enum EntryKind {
16 Item, Import,
17}
18
19impl ModuleScope {
20 pub fn new(items: AstChildren<ast::ModuleItem>) -> ModuleScope {
21 let mut entries = Vec::new();
22 for item in items {
23 let entry = match item {
24 ast::ModuleItem::StructDef(item) => Entry::new(item),
25 ast::ModuleItem::EnumDef(item) => Entry::new(item),
26 ast::ModuleItem::FnDef(item) => Entry::new(item),
27 ast::ModuleItem::ConstDef(item) => Entry::new(item),
28 ast::ModuleItem::StaticDef(item) => Entry::new(item),
29 ast::ModuleItem::TraitDef(item) => Entry::new(item),
30 ast::ModuleItem::TypeDef(item) => Entry::new(item),
31 ast::ModuleItem::Module(item) => Entry::new(item),
32 ast::ModuleItem::UseItem(item) => {
33 if let Some(tree) = item.use_tree() {
34 collect_imports(tree, &mut entries);
35 }
36 continue;
37 },
38 ast::ModuleItem::ExternCrateItem(_) |
39 ast::ModuleItem::ImplItem(_) => continue,
40 };
41 entries.extend(entry)
42 }
43
44 ModuleScope { entries }
45 }
46
47 pub fn entries(&self) -> &[Entry] {
48 self.entries.as_slice()
49 }
50}
51
52impl Entry {
53 fn new<'a>(item: impl ast::NameOwner<'a>) -> Option<Entry> {
54 let name = item.name()?;
55 Some(Entry { node: name.syntax().owned(), kind: EntryKind::Item })
56 }
57 fn new_import(path: ast::Path) -> Option<Entry> {
58 let name_ref = path.segment()?.name_ref()?;
59 Some(Entry { node: name_ref.syntax().owned(), kind: EntryKind::Import })
60 }
61 pub fn name(&self) -> SmolStr {
62 match self.kind {
63 EntryKind::Item =>
64 ast::Name::cast(self.node.borrowed()).unwrap()
65 .text(),
66 EntryKind::Import =>
67 ast::NameRef::cast(self.node.borrowed()).unwrap()
68 .text(),
69 }
70 }
71 pub fn syntax(&self) -> SyntaxNodeRef {
72 self.node.borrowed()
73 }
74}
75
76fn collect_imports(tree: ast::UseTree, acc: &mut Vec<Entry>) {
77 if let Some(use_tree_list) = tree.use_tree_list() {
78 return use_tree_list.use_trees().for_each(|it| collect_imports(it, acc));
79 }
80 if let Some(path) = tree.path() {
81 acc.extend(Entry::new_import(path));
82 }
83}
84
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89 use libsyntax2::{File, ast::ModuleItemOwner};
90
91 fn do_check(code: &str, expected: &[&str]) {
92 let file = File::parse(&code);
93 let scope = ModuleScope::new(file.ast().items());
94 let actual = scope.entries
95 .iter()
96 .map(|it| it.name())
97 .collect::<Vec<_>>();
98 assert_eq!(expected, actual.as_slice());
99 }
100
101 #[test]
102 fn test_module_scope() {
103 do_check("
104 struct Foo;
105 enum Bar {}
106 mod baz {}
107 fn quux() {}
108 use x::{
109 y::z,
110 t,
111 };
112 type T = ();
113 ", &["Foo", "Bar", "baz", "quux", "z", "t", "T"])
114 }
115}