diff options
author | Aleksey Kladov <[email protected]> | 2019-02-03 18:26:35 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-06 14:00:00 +0000 |
commit | 0c5fd8f7cbf04eda763e55bc9a38dad5f7ec917d (patch) | |
tree | 4af15c8906b85de01a15c717bc1fac388952cd3d /crates/ra_ide_api_light/src/assists/introduce_variable.rs | |
parent | 736a55c97e69f95e6ff4a0c3dafb2018e8ea05f9 (diff) |
move assists to a separate crate
Diffstat (limited to 'crates/ra_ide_api_light/src/assists/introduce_variable.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/assists/introduce_variable.rs | 431 |
1 files changed, 0 insertions, 431 deletions
diff --git a/crates/ra_ide_api_light/src/assists/introduce_variable.rs b/crates/ra_ide_api_light/src/assists/introduce_variable.rs deleted file mode 100644 index ed13bddc4..000000000 --- a/crates/ra_ide_api_light/src/assists/introduce_variable.rs +++ /dev/null | |||
@@ -1,431 +0,0 @@ | |||
1 | use ra_syntax::{ | ||
2 | ast::{self, AstNode}, | ||
3 | SyntaxKind::{ | ||
4 | WHITESPACE, MATCH_ARM, LAMBDA_EXPR, PATH_EXPR, BREAK_EXPR, LOOP_EXPR, RETURN_EXPR, COMMENT | ||
5 | }, SyntaxNode, TextUnit, | ||
6 | }; | ||
7 | |||
8 | use crate::assists::{AssistCtx, Assist}; | ||
9 | |||
10 | pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option<Assist> { | ||
11 | let node = ctx.covering_node(); | ||
12 | if !valid_covering_node(node) { | ||
13 | return None; | ||
14 | } | ||
15 | let expr = node.ancestors().filter_map(valid_target_expr).next()?; | ||
16 | let (anchor_stmt, wrap_in_block) = anchor_stmt(expr)?; | ||
17 | let indent = anchor_stmt.prev_sibling()?; | ||
18 | if indent.kind() != WHITESPACE { | ||
19 | return None; | ||
20 | } | ||
21 | ctx.build("introduce variable", move |edit| { | ||
22 | let mut buf = String::new(); | ||
23 | |||
24 | let cursor_offset = if wrap_in_block { | ||
25 | buf.push_str("{ let var_name = "); | ||
26 | TextUnit::of_str("{ let ") | ||
27 | } else { | ||
28 | buf.push_str("let var_name = "); | ||
29 | TextUnit::of_str("let ") | ||
30 | }; | ||
31 | |||
32 | expr.syntax().text().push_to(&mut buf); | ||
33 | let full_stmt = ast::ExprStmt::cast(anchor_stmt); | ||
34 | let is_full_stmt = if let Some(expr_stmt) = full_stmt { | ||
35 | Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax()) | ||
36 | } else { | ||
37 | false | ||
38 | }; | ||
39 | if is_full_stmt { | ||
40 | if !full_stmt.unwrap().has_semi() { | ||
41 | buf.push_str(";"); | ||
42 | } | ||
43 | edit.replace(expr.syntax().range(), buf); | ||
44 | } else { | ||
45 | buf.push_str(";"); | ||
46 | indent.text().push_to(&mut buf); | ||
47 | edit.replace(expr.syntax().range(), "var_name".to_string()); | ||
48 | edit.insert(anchor_stmt.range().start(), buf); | ||
49 | if wrap_in_block { | ||
50 | edit.insert(anchor_stmt.range().end(), " }"); | ||
51 | } | ||
52 | } | ||
53 | edit.set_cursor(anchor_stmt.range().start() + cursor_offset); | ||
54 | }) | ||
55 | } | ||
56 | |||
57 | fn valid_covering_node(node: &SyntaxNode) -> bool { | ||
58 | node.kind() != COMMENT | ||
59 | } | ||
60 | /// Check wether the node is a valid expression which can be extracted to a variable. | ||
61 | /// In general that's true for any expression, but in some cases that would produce invalid code. | ||
62 | fn valid_target_expr(node: &SyntaxNode) -> Option<&ast::Expr> { | ||
63 | return match node.kind() { | ||
64 | PATH_EXPR => None, | ||
65 | BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()), | ||
66 | RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()), | ||
67 | LOOP_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()), | ||
68 | _ => ast::Expr::cast(node), | ||
69 | }; | ||
70 | } | ||
71 | |||
72 | /// Returns the syntax node which will follow the freshly introduced var | ||
73 | /// and a boolean indicating whether we have to wrap it within a { } block | ||
74 | /// to produce correct code. | ||
75 | /// It can be a statement, the last in a block expression or a wanna be block | ||
76 | /// expression like a lamba or match arm. | ||
77 | fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> { | ||
78 | expr.syntax().ancestors().find_map(|node| { | ||
79 | if ast::Stmt::cast(node).is_some() { | ||
80 | return Some((node, false)); | ||
81 | } | ||
82 | |||
83 | if let Some(expr) = node | ||
84 | .parent() | ||
85 | .and_then(ast::Block::cast) | ||
86 | .and_then(|it| it.expr()) | ||
87 | { | ||
88 | if expr.syntax() == node { | ||
89 | return Some((node, false)); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | if let Some(parent) = node.parent() { | ||
94 | if parent.kind() == MATCH_ARM || parent.kind() == LAMBDA_EXPR { | ||
95 | return Some((node, true)); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | None | ||
100 | }) | ||
101 | } | ||
102 | |||
103 | #[cfg(test)] | ||
104 | mod tests { | ||
105 | use super::*; | ||
106 | use crate::assists::{ check_assist, check_assist_not_applicable, check_assist_range }; | ||
107 | |||
108 | #[test] | ||
109 | fn test_introduce_var_simple() { | ||
110 | check_assist_range( | ||
111 | introduce_variable, | ||
112 | " | ||
113 | fn foo() { | ||
114 | foo(<|>1 + 1<|>); | ||
115 | }", | ||
116 | " | ||
117 | fn foo() { | ||
118 | let <|>var_name = 1 + 1; | ||
119 | foo(var_name); | ||
120 | }", | ||
121 | ); | ||
122 | } | ||
123 | |||
124 | #[test] | ||
125 | fn test_introduce_var_expr_stmt() { | ||
126 | check_assist_range( | ||
127 | introduce_variable, | ||
128 | " | ||
129 | fn foo() { | ||
130 | <|>1 + 1<|>; | ||
131 | }", | ||
132 | " | ||
133 | fn foo() { | ||
134 | let <|>var_name = 1 + 1; | ||
135 | }", | ||
136 | ); | ||
137 | } | ||
138 | |||
139 | #[test] | ||
140 | fn test_introduce_var_part_of_expr_stmt() { | ||
141 | check_assist_range( | ||
142 | introduce_variable, | ||
143 | " | ||
144 | fn foo() { | ||
145 | <|>1<|> + 1; | ||
146 | }", | ||
147 | " | ||
148 | fn foo() { | ||
149 | let <|>var_name = 1; | ||
150 | var_name + 1; | ||
151 | }", | ||
152 | ); | ||
153 | } | ||
154 | |||
155 | #[test] | ||
156 | fn test_introduce_var_last_expr() { | ||
157 | check_assist_range( | ||
158 | introduce_variable, | ||
159 | " | ||
160 | fn foo() { | ||
161 | bar(<|>1 + 1<|>) | ||
162 | }", | ||
163 | " | ||
164 | fn foo() { | ||
165 | let <|>var_name = 1 + 1; | ||
166 | bar(var_name) | ||
167 | }", | ||
168 | ); | ||
169 | } | ||
170 | |||
171 | #[test] | ||
172 | fn test_introduce_var_last_full_expr() { | ||
173 | check_assist_range( | ||
174 | introduce_variable, | ||
175 | " | ||
176 | fn foo() { | ||
177 | <|>bar(1 + 1)<|> | ||
178 | }", | ||
179 | " | ||
180 | fn foo() { | ||
181 | let <|>var_name = bar(1 + 1); | ||
182 | var_name | ||
183 | }", | ||
184 | ); | ||
185 | } | ||
186 | |||
187 | #[test] | ||
188 | fn test_introduce_var_block_expr_second_to_last() { | ||
189 | check_assist_range( | ||
190 | introduce_variable, | ||
191 | " | ||
192 | fn foo() { | ||
193 | <|>{ let x = 0; x }<|> | ||
194 | something_else(); | ||
195 | }", | ||
196 | " | ||
197 | fn foo() { | ||
198 | let <|>var_name = { let x = 0; x }; | ||
199 | something_else(); | ||
200 | }", | ||
201 | ); | ||
202 | } | ||
203 | |||
204 | #[test] | ||
205 | fn test_introduce_var_in_match_arm_no_block() { | ||
206 | check_assist_range( | ||
207 | introduce_variable, | ||
208 | " | ||
209 | fn main() { | ||
210 | let x = true; | ||
211 | let tuple = match x { | ||
212 | true => (<|>2 + 2<|>, true) | ||
213 | _ => (0, false) | ||
214 | }; | ||
215 | } | ||
216 | ", | ||
217 | " | ||
218 | fn main() { | ||
219 | let x = true; | ||
220 | let tuple = match x { | ||
221 | true => { let <|>var_name = 2 + 2; (var_name, true) } | ||
222 | _ => (0, false) | ||
223 | }; | ||
224 | } | ||
225 | ", | ||
226 | ); | ||
227 | } | ||
228 | |||
229 | #[test] | ||
230 | fn test_introduce_var_in_match_arm_with_block() { | ||
231 | check_assist_range( | ||
232 | introduce_variable, | ||
233 | " | ||
234 | fn main() { | ||
235 | let x = true; | ||
236 | let tuple = match x { | ||
237 | true => { | ||
238 | let y = 1; | ||
239 | (<|>2 + y<|>, true) | ||
240 | } | ||
241 | _ => (0, false) | ||
242 | }; | ||
243 | } | ||
244 | ", | ||
245 | " | ||
246 | fn main() { | ||
247 | let x = true; | ||
248 | let tuple = match x { | ||
249 | true => { | ||
250 | let y = 1; | ||
251 | let <|>var_name = 2 + y; | ||
252 | (var_name, true) | ||
253 | } | ||
254 | _ => (0, false) | ||
255 | }; | ||
256 | } | ||
257 | ", | ||
258 | ); | ||
259 | } | ||
260 | |||
261 | #[test] | ||
262 | fn test_introduce_var_in_closure_no_block() { | ||
263 | check_assist_range( | ||
264 | introduce_variable, | ||
265 | " | ||
266 | fn main() { | ||
267 | let lambda = |x: u32| <|>x * 2<|>; | ||
268 | } | ||
269 | ", | ||
270 | " | ||
271 | fn main() { | ||
272 | let lambda = |x: u32| { let <|>var_name = x * 2; var_name }; | ||
273 | } | ||
274 | ", | ||
275 | ); | ||
276 | } | ||
277 | |||
278 | #[test] | ||
279 | fn test_introduce_var_in_closure_with_block() { | ||
280 | check_assist_range( | ||
281 | introduce_variable, | ||
282 | " | ||
283 | fn main() { | ||
284 | let lambda = |x: u32| { <|>x * 2<|> }; | ||
285 | } | ||
286 | ", | ||
287 | " | ||
288 | fn main() { | ||
289 | let lambda = |x: u32| { let <|>var_name = x * 2; var_name }; | ||
290 | } | ||
291 | ", | ||
292 | ); | ||
293 | } | ||
294 | |||
295 | #[test] | ||
296 | fn test_introduce_var_path_simple() { | ||
297 | check_assist( | ||
298 | introduce_variable, | ||
299 | " | ||
300 | fn main() { | ||
301 | let o = S<|>ome(true); | ||
302 | } | ||
303 | ", | ||
304 | " | ||
305 | fn main() { | ||
306 | let <|>var_name = Some(true); | ||
307 | let o = var_name; | ||
308 | } | ||
309 | ", | ||
310 | ); | ||
311 | } | ||
312 | |||
313 | #[test] | ||
314 | fn test_introduce_var_path_method() { | ||
315 | check_assist( | ||
316 | introduce_variable, | ||
317 | " | ||
318 | fn main() { | ||
319 | let v = b<|>ar.foo(); | ||
320 | } | ||
321 | ", | ||
322 | " | ||
323 | fn main() { | ||
324 | let <|>var_name = bar.foo(); | ||
325 | let v = var_name; | ||
326 | } | ||
327 | ", | ||
328 | ); | ||
329 | } | ||
330 | |||
331 | #[test] | ||
332 | fn test_introduce_var_return() { | ||
333 | check_assist( | ||
334 | introduce_variable, | ||
335 | " | ||
336 | fn foo() -> u32 { | ||
337 | r<|>eturn 2 + 2; | ||
338 | } | ||
339 | ", | ||
340 | " | ||
341 | fn foo() -> u32 { | ||
342 | let <|>var_name = 2 + 2; | ||
343 | return var_name; | ||
344 | } | ||
345 | ", | ||
346 | ); | ||
347 | } | ||
348 | |||
349 | #[test] | ||
350 | fn test_introduce_var_break() { | ||
351 | check_assist( | ||
352 | introduce_variable, | ||
353 | " | ||
354 | fn main() { | ||
355 | let result = loop { | ||
356 | b<|>reak 2 + 2; | ||
357 | }; | ||
358 | } | ||
359 | ", | ||
360 | " | ||
361 | fn main() { | ||
362 | let result = loop { | ||
363 | let <|>var_name = 2 + 2; | ||
364 | break var_name; | ||
365 | }; | ||
366 | } | ||
367 | ", | ||
368 | ); | ||
369 | } | ||
370 | |||
371 | #[test] | ||
372 | fn test_introduce_var_for_cast() { | ||
373 | check_assist( | ||
374 | introduce_variable, | ||
375 | " | ||
376 | fn main() { | ||
377 | let v = 0f32 a<|>s u32; | ||
378 | } | ||
379 | ", | ||
380 | " | ||
381 | fn main() { | ||
382 | let <|>var_name = 0f32 as u32; | ||
383 | let v = var_name; | ||
384 | } | ||
385 | ", | ||
386 | ); | ||
387 | } | ||
388 | |||
389 | #[test] | ||
390 | fn test_introduce_var_for_return_not_applicable() { | ||
391 | check_assist_not_applicable( | ||
392 | introduce_variable, | ||
393 | " | ||
394 | fn foo() { | ||
395 | r<|>eturn; | ||
396 | } | ||
397 | ", | ||
398 | ); | ||
399 | } | ||
400 | |||
401 | #[test] | ||
402 | fn test_introduce_var_for_break_not_applicable() { | ||
403 | check_assist_not_applicable( | ||
404 | introduce_variable, | ||
405 | " | ||
406 | fn main() { | ||
407 | loop { | ||
408 | b<|>reak; | ||
409 | }; | ||
410 | } | ||
411 | ", | ||
412 | ); | ||
413 | } | ||
414 | |||
415 | #[test] | ||
416 | fn test_introduce_var_in_comment_not_applicable() { | ||
417 | check_assist_not_applicable( | ||
418 | introduce_variable, | ||
419 | " | ||
420 | fn main() { | ||
421 | let x = true; | ||
422 | let tuple = match x { | ||
423 | // c<|>omment | ||
424 | true => (2 + 2, true) | ||
425 | _ => (0, false) | ||
426 | }; | ||
427 | } | ||
428 | ", | ||
429 | ); | ||
430 | } | ||
431 | } | ||