diff options
author | Aleksey Kladov <[email protected]> | 2020-08-13 16:33:38 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-13 16:33:38 +0100 |
commit | fc34403018079ea053f26d0a31b7517053c7dd8c (patch) | |
tree | 500d7c2ec2179309be12a063634cb6a77c9af845 /crates/ra_assists/src/handlers/early_return.rs | |
parent | ae3abd6e575940eb1221acf26c09e96352f052fa (diff) |
Rename ra_assists -> assists
Diffstat (limited to 'crates/ra_assists/src/handlers/early_return.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/early_return.rs | 515 |
1 files changed, 0 insertions, 515 deletions
diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs deleted file mode 100644 index 7fd78e9d4..000000000 --- a/crates/ra_assists/src/handlers/early_return.rs +++ /dev/null | |||
@@ -1,515 +0,0 @@ | |||
1 | use std::{iter::once, ops::RangeInclusive}; | ||
2 | |||
3 | use syntax::{ | ||
4 | algo::replace_children, | ||
5 | ast::{ | ||
6 | self, | ||
7 | edit::{AstNodeEdit, IndentLevel}, | ||
8 | make, | ||
9 | }, | ||
10 | AstNode, | ||
11 | SyntaxKind::{FN, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE}, | ||
12 | SyntaxNode, | ||
13 | }; | ||
14 | |||
15 | use crate::{ | ||
16 | assist_context::{AssistContext, Assists}, | ||
17 | utils::invert_boolean_expression, | ||
18 | AssistId, AssistKind, | ||
19 | }; | ||
20 | |||
21 | // Assist: convert_to_guarded_return | ||
22 | // | ||
23 | // Replace a large conditional with a guarded return. | ||
24 | // | ||
25 | // ``` | ||
26 | // fn main() { | ||
27 | // <|>if cond { | ||
28 | // foo(); | ||
29 | // bar(); | ||
30 | // } | ||
31 | // } | ||
32 | // ``` | ||
33 | // -> | ||
34 | // ``` | ||
35 | // fn main() { | ||
36 | // if !cond { | ||
37 | // return; | ||
38 | // } | ||
39 | // foo(); | ||
40 | // bar(); | ||
41 | // } | ||
42 | // ``` | ||
43 | pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
44 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; | ||
45 | if if_expr.else_branch().is_some() { | ||
46 | return None; | ||
47 | } | ||
48 | |||
49 | let cond = if_expr.condition()?; | ||
50 | |||
51 | // Check if there is an IfLet that we can handle. | ||
52 | let if_let_pat = match cond.pat() { | ||
53 | None => None, // No IfLet, supported. | ||
54 | Some(ast::Pat::TupleStructPat(pat)) if pat.fields().count() == 1 => { | ||
55 | let path = pat.path()?; | ||
56 | match path.qualifier() { | ||
57 | None => { | ||
58 | let bound_ident = pat.fields().next().unwrap(); | ||
59 | Some((path, bound_ident)) | ||
60 | } | ||
61 | Some(_) => return None, | ||
62 | } | ||
63 | } | ||
64 | Some(_) => return None, // Unsupported IfLet. | ||
65 | }; | ||
66 | |||
67 | let cond_expr = cond.expr()?; | ||
68 | let then_block = if_expr.then_branch()?; | ||
69 | |||
70 | let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::BlockExpr::cast)?; | ||
71 | |||
72 | if parent_block.expr()? != if_expr.clone().into() { | ||
73 | return None; | ||
74 | } | ||
75 | |||
76 | // check for early return and continue | ||
77 | let first_in_then_block = then_block.syntax().first_child()?; | ||
78 | if ast::ReturnExpr::can_cast(first_in_then_block.kind()) | ||
79 | || ast::ContinueExpr::can_cast(first_in_then_block.kind()) | ||
80 | || first_in_then_block | ||
81 | .children() | ||
82 | .any(|x| ast::ReturnExpr::can_cast(x.kind()) || ast::ContinueExpr::can_cast(x.kind())) | ||
83 | { | ||
84 | return None; | ||
85 | } | ||
86 | |||
87 | let parent_container = parent_block.syntax().parent()?; | ||
88 | |||
89 | let early_expression: ast::Expr = match parent_container.kind() { | ||
90 | WHILE_EXPR | LOOP_EXPR => make::expr_continue(), | ||
91 | FN => make::expr_return(), | ||
92 | _ => return None, | ||
93 | }; | ||
94 | |||
95 | if then_block.syntax().first_child_or_token().map(|t| t.kind() == L_CURLY).is_none() { | ||
96 | return None; | ||
97 | } | ||
98 | |||
99 | then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?; | ||
100 | |||
101 | let target = if_expr.syntax().text_range(); | ||
102 | acc.add( | ||
103 | AssistId("convert_to_guarded_return", AssistKind::RefactorRewrite), | ||
104 | "Convert to guarded return", | ||
105 | target, | ||
106 | |edit| { | ||
107 | let if_indent_level = IndentLevel::from_node(&if_expr.syntax()); | ||
108 | let new_block = match if_let_pat { | ||
109 | None => { | ||
110 | // If. | ||
111 | let new_expr = { | ||
112 | let then_branch = | ||
113 | make::block_expr(once(make::expr_stmt(early_expression).into()), None); | ||
114 | let cond = invert_boolean_expression(cond_expr); | ||
115 | make::expr_if(make::condition(cond, None), then_branch) | ||
116 | .indent(if_indent_level) | ||
117 | }; | ||
118 | replace(new_expr.syntax(), &then_block, &parent_block, &if_expr) | ||
119 | } | ||
120 | Some((path, bound_ident)) => { | ||
121 | // If-let. | ||
122 | let match_expr = { | ||
123 | let happy_arm = { | ||
124 | let pat = make::tuple_struct_pat( | ||
125 | path, | ||
126 | once(make::ident_pat(make::name("it")).into()), | ||
127 | ); | ||
128 | let expr = { | ||
129 | let name_ref = make::name_ref("it"); | ||
130 | let segment = make::path_segment(name_ref); | ||
131 | let path = make::path_unqualified(segment); | ||
132 | make::expr_path(path) | ||
133 | }; | ||
134 | make::match_arm(once(pat.into()), expr) | ||
135 | }; | ||
136 | |||
137 | let sad_arm = make::match_arm( | ||
138 | // FIXME: would be cool to use `None` or `Err(_)` if appropriate | ||
139 | once(make::wildcard_pat().into()), | ||
140 | early_expression, | ||
141 | ); | ||
142 | |||
143 | make::expr_match(cond_expr, make::match_arm_list(vec![happy_arm, sad_arm])) | ||
144 | }; | ||
145 | |||
146 | let let_stmt = make::let_stmt( | ||
147 | make::ident_pat(make::name(&bound_ident.syntax().to_string())).into(), | ||
148 | Some(match_expr), | ||
149 | ); | ||
150 | let let_stmt = let_stmt.indent(if_indent_level); | ||
151 | replace(let_stmt.syntax(), &then_block, &parent_block, &if_expr) | ||
152 | } | ||
153 | }; | ||
154 | edit.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap()); | ||
155 | |||
156 | fn replace( | ||
157 | new_expr: &SyntaxNode, | ||
158 | then_block: &ast::BlockExpr, | ||
159 | parent_block: &ast::BlockExpr, | ||
160 | if_expr: &ast::IfExpr, | ||
161 | ) -> SyntaxNode { | ||
162 | let then_block_items = then_block.dedent(IndentLevel(1)); | ||
163 | let end_of_then = then_block_items.syntax().last_child_or_token().unwrap(); | ||
164 | let end_of_then = | ||
165 | if end_of_then.prev_sibling_or_token().map(|n| n.kind()) == Some(WHITESPACE) { | ||
166 | end_of_then.prev_sibling_or_token().unwrap() | ||
167 | } else { | ||
168 | end_of_then | ||
169 | }; | ||
170 | let mut then_statements = new_expr.children_with_tokens().chain( | ||
171 | then_block_items | ||
172 | .syntax() | ||
173 | .children_with_tokens() | ||
174 | .skip(1) | ||
175 | .take_while(|i| *i != end_of_then), | ||
176 | ); | ||
177 | replace_children( | ||
178 | &parent_block.syntax(), | ||
179 | RangeInclusive::new( | ||
180 | if_expr.clone().syntax().clone().into(), | ||
181 | if_expr.syntax().clone().into(), | ||
182 | ), | ||
183 | &mut then_statements, | ||
184 | ) | ||
185 | } | ||
186 | }, | ||
187 | ) | ||
188 | } | ||
189 | |||
190 | #[cfg(test)] | ||
191 | mod tests { | ||
192 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
193 | |||
194 | use super::*; | ||
195 | |||
196 | #[test] | ||
197 | fn convert_inside_fn() { | ||
198 | check_assist( | ||
199 | convert_to_guarded_return, | ||
200 | r#" | ||
201 | fn main() { | ||
202 | bar(); | ||
203 | if<|> true { | ||
204 | foo(); | ||
205 | |||
206 | //comment | ||
207 | bar(); | ||
208 | } | ||
209 | } | ||
210 | "#, | ||
211 | r#" | ||
212 | fn main() { | ||
213 | bar(); | ||
214 | if !true { | ||
215 | return; | ||
216 | } | ||
217 | foo(); | ||
218 | |||
219 | //comment | ||
220 | bar(); | ||
221 | } | ||
222 | "#, | ||
223 | ); | ||
224 | } | ||
225 | |||
226 | #[test] | ||
227 | fn convert_let_inside_fn() { | ||
228 | check_assist( | ||
229 | convert_to_guarded_return, | ||
230 | r#" | ||
231 | fn main(n: Option<String>) { | ||
232 | bar(); | ||
233 | if<|> let Some(n) = n { | ||
234 | foo(n); | ||
235 | |||
236 | //comment | ||
237 | bar(); | ||
238 | } | ||
239 | } | ||
240 | "#, | ||
241 | r#" | ||
242 | fn main(n: Option<String>) { | ||
243 | bar(); | ||
244 | let n = match n { | ||
245 | Some(it) => it, | ||
246 | _ => return, | ||
247 | }; | ||
248 | foo(n); | ||
249 | |||
250 | //comment | ||
251 | bar(); | ||
252 | } | ||
253 | "#, | ||
254 | ); | ||
255 | } | ||
256 | |||
257 | #[test] | ||
258 | fn convert_if_let_result() { | ||
259 | check_assist( | ||
260 | convert_to_guarded_return, | ||
261 | r#" | ||
262 | fn main() { | ||
263 | if<|> let Ok(x) = Err(92) { | ||
264 | foo(x); | ||
265 | } | ||
266 | } | ||
267 | "#, | ||
268 | r#" | ||
269 | fn main() { | ||
270 | let x = match Err(92) { | ||
271 | Ok(it) => it, | ||
272 | _ => return, | ||
273 | }; | ||
274 | foo(x); | ||
275 | } | ||
276 | "#, | ||
277 | ); | ||
278 | } | ||
279 | |||
280 | #[test] | ||
281 | fn convert_let_ok_inside_fn() { | ||
282 | check_assist( | ||
283 | convert_to_guarded_return, | ||
284 | r#" | ||
285 | fn main(n: Option<String>) { | ||
286 | bar(); | ||
287 | if<|> let Ok(n) = n { | ||
288 | foo(n); | ||
289 | |||
290 | //comment | ||
291 | bar(); | ||
292 | } | ||
293 | } | ||
294 | "#, | ||
295 | r#" | ||
296 | fn main(n: Option<String>) { | ||
297 | bar(); | ||
298 | let n = match n { | ||
299 | Ok(it) => it, | ||
300 | _ => return, | ||
301 | }; | ||
302 | foo(n); | ||
303 | |||
304 | //comment | ||
305 | bar(); | ||
306 | } | ||
307 | "#, | ||
308 | ); | ||
309 | } | ||
310 | |||
311 | #[test] | ||
312 | fn convert_inside_while() { | ||
313 | check_assist( | ||
314 | convert_to_guarded_return, | ||
315 | r#" | ||
316 | fn main() { | ||
317 | while true { | ||
318 | if<|> true { | ||
319 | foo(); | ||
320 | bar(); | ||
321 | } | ||
322 | } | ||
323 | } | ||
324 | "#, | ||
325 | r#" | ||
326 | fn main() { | ||
327 | while true { | ||
328 | if !true { | ||
329 | continue; | ||
330 | } | ||
331 | foo(); | ||
332 | bar(); | ||
333 | } | ||
334 | } | ||
335 | "#, | ||
336 | ); | ||
337 | } | ||
338 | |||
339 | #[test] | ||
340 | fn convert_let_inside_while() { | ||
341 | check_assist( | ||
342 | convert_to_guarded_return, | ||
343 | r#" | ||
344 | fn main() { | ||
345 | while true { | ||
346 | if<|> let Some(n) = n { | ||
347 | foo(n); | ||
348 | bar(); | ||
349 | } | ||
350 | } | ||
351 | } | ||
352 | "#, | ||
353 | r#" | ||
354 | fn main() { | ||
355 | while true { | ||
356 | let n = match n { | ||
357 | Some(it) => it, | ||
358 | _ => continue, | ||
359 | }; | ||
360 | foo(n); | ||
361 | bar(); | ||
362 | } | ||
363 | } | ||
364 | "#, | ||
365 | ); | ||
366 | } | ||
367 | |||
368 | #[test] | ||
369 | fn convert_inside_loop() { | ||
370 | check_assist( | ||
371 | convert_to_guarded_return, | ||
372 | r#" | ||
373 | fn main() { | ||
374 | loop { | ||
375 | if<|> true { | ||
376 | foo(); | ||
377 | bar(); | ||
378 | } | ||
379 | } | ||
380 | } | ||
381 | "#, | ||
382 | r#" | ||
383 | fn main() { | ||
384 | loop { | ||
385 | if !true { | ||
386 | continue; | ||
387 | } | ||
388 | foo(); | ||
389 | bar(); | ||
390 | } | ||
391 | } | ||
392 | "#, | ||
393 | ); | ||
394 | } | ||
395 | |||
396 | #[test] | ||
397 | fn convert_let_inside_loop() { | ||
398 | check_assist( | ||
399 | convert_to_guarded_return, | ||
400 | r#" | ||
401 | fn main() { | ||
402 | loop { | ||
403 | if<|> let Some(n) = n { | ||
404 | foo(n); | ||
405 | bar(); | ||
406 | } | ||
407 | } | ||
408 | } | ||
409 | "#, | ||
410 | r#" | ||
411 | fn main() { | ||
412 | loop { | ||
413 | let n = match n { | ||
414 | Some(it) => it, | ||
415 | _ => continue, | ||
416 | }; | ||
417 | foo(n); | ||
418 | bar(); | ||
419 | } | ||
420 | } | ||
421 | "#, | ||
422 | ); | ||
423 | } | ||
424 | |||
425 | #[test] | ||
426 | fn ignore_already_converted_if() { | ||
427 | check_assist_not_applicable( | ||
428 | convert_to_guarded_return, | ||
429 | r#" | ||
430 | fn main() { | ||
431 | if<|> true { | ||
432 | return; | ||
433 | } | ||
434 | } | ||
435 | "#, | ||
436 | ); | ||
437 | } | ||
438 | |||
439 | #[test] | ||
440 | fn ignore_already_converted_loop() { | ||
441 | check_assist_not_applicable( | ||
442 | convert_to_guarded_return, | ||
443 | r#" | ||
444 | fn main() { | ||
445 | loop { | ||
446 | if<|> true { | ||
447 | continue; | ||
448 | } | ||
449 | } | ||
450 | } | ||
451 | "#, | ||
452 | ); | ||
453 | } | ||
454 | |||
455 | #[test] | ||
456 | fn ignore_return() { | ||
457 | check_assist_not_applicable( | ||
458 | convert_to_guarded_return, | ||
459 | r#" | ||
460 | fn main() { | ||
461 | if<|> true { | ||
462 | return | ||
463 | } | ||
464 | } | ||
465 | "#, | ||
466 | ); | ||
467 | } | ||
468 | |||
469 | #[test] | ||
470 | fn ignore_else_branch() { | ||
471 | check_assist_not_applicable( | ||
472 | convert_to_guarded_return, | ||
473 | r#" | ||
474 | fn main() { | ||
475 | if<|> true { | ||
476 | foo(); | ||
477 | } else { | ||
478 | bar() | ||
479 | } | ||
480 | } | ||
481 | "#, | ||
482 | ); | ||
483 | } | ||
484 | |||
485 | #[test] | ||
486 | fn ignore_statements_aftert_if() { | ||
487 | check_assist_not_applicable( | ||
488 | convert_to_guarded_return, | ||
489 | r#" | ||
490 | fn main() { | ||
491 | if<|> true { | ||
492 | foo(); | ||
493 | } | ||
494 | bar(); | ||
495 | } | ||
496 | "#, | ||
497 | ); | ||
498 | } | ||
499 | |||
500 | #[test] | ||
501 | fn ignore_statements_inside_if() { | ||
502 | check_assist_not_applicable( | ||
503 | convert_to_guarded_return, | ||
504 | r#" | ||
505 | fn main() { | ||
506 | if false { | ||
507 | if<|> true { | ||
508 | foo(); | ||
509 | } | ||
510 | } | ||
511 | } | ||
512 | "#, | ||
513 | ); | ||
514 | } | ||
515 | } | ||