aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/scope/fn_scope.rs
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/ra_editor/src/scope/fn_scope.rs
parentba0bfeee12e19da40b5eabc8d0408639af10e96f (diff)
rename all things
Diffstat (limited to 'crates/ra_editor/src/scope/fn_scope.rs')
-rw-r--r--crates/ra_editor/src/scope/fn_scope.rs329
1 files changed, 329 insertions, 0 deletions
diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs
new file mode 100644
index 000000000..3ae5276a2
--- /dev/null
+++ b/crates/ra_editor/src/scope/fn_scope.rs
@@ -0,0 +1,329 @@
1use std::{
2 fmt,
3 collections::HashMap,
4};
5
6use ra_syntax::{
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 ra_syntax::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}