diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs b/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs index 7903a18fa..73ef44685 100644 --- a/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs +++ b/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs | |||
@@ -1,5 +1,8 @@ | |||
1 | use ide_db::helpers::FamousDefs; | 1 | use ide_db::helpers::FamousDefs; |
2 | use syntax::{AstNode, ast::{self, make, ArgListOwner, edit::AstNodeEdit}}; | 2 | use syntax::{ |
3 | ast::{self, edit::AstNodeEdit, make, ArgListOwner}, | ||
4 | AstNode, | ||
5 | }; | ||
3 | 6 | ||
4 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 7 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
5 | 8 | ||
@@ -31,16 +34,20 @@ pub(crate) fn convert_iter_for_each_to_for(acc: &mut Assists, ctx: &AssistContex | |||
31 | ast::Expr::MethodCallExpr(expr) => { | 34 | ast::Expr::MethodCallExpr(expr) => { |
32 | closure = match expr.arg_list()?.args().next()? { | 35 | closure = match expr.arg_list()?.args().next()? { |
33 | ast::Expr::ClosureExpr(expr) => expr, | 36 | ast::Expr::ClosureExpr(expr) => expr, |
34 | _ => { return None; } | 37 | _ => { |
38 | return None; | ||
39 | } | ||
35 | }; | 40 | }; |
36 | 41 | ||
37 | expr | 42 | expr |
38 | }, | 43 | } |
39 | ast::Expr::ClosureExpr(expr) => { | 44 | ast::Expr::ClosureExpr(expr) => { |
40 | closure = expr; | 45 | closure = expr; |
41 | ast::MethodCallExpr::cast(closure.syntax().ancestors().nth(2)?)? | 46 | ast::MethodCallExpr::cast(closure.syntax().ancestors().nth(2)?)? |
42 | }, | 47 | } |
43 | _ => { return None; } | 48 | _ => { |
49 | return None; | ||
50 | } | ||
44 | }; | 51 | }; |
45 | 52 | ||
46 | let (total_expr, parent) = validate_method_call_expr(&ctx.sema, total_expr)?; | 53 | let (total_expr, parent) = validate_method_call_expr(&ctx.sema, total_expr)?; |
@@ -58,8 +65,10 @@ pub(crate) fn convert_iter_for_each_to_for(acc: &mut Assists, ctx: &AssistContex | |||
58 | 65 | ||
59 | let block = match body { | 66 | let block = match body { |
60 | ast::Expr::BlockExpr(block) => block, | 67 | ast::Expr::BlockExpr(block) => block, |
61 | _ => make::block_expr(Vec::new(), Some(body)) | 68 | _ => make::block_expr(Vec::new(), Some(body)), |
62 | }.reset_indent().indent(original_indentation); | 69 | } |
70 | .reset_indent() | ||
71 | .indent(original_indentation); | ||
63 | 72 | ||
64 | let expr_for_loop = make::expr_for_loop(param, parent, block); | 73 | let expr_for_loop = make::expr_for_loop(param, parent, block); |
65 | builder.replace_ast(total_expr, expr_for_loop) | 74 | builder.replace_ast(total_expr, expr_for_loop) |
@@ -187,6 +196,7 @@ fn main() { | |||
187 | r#" | 196 | r#" |
188 | fn main() { | 197 | fn main() { |
189 | value.$0for_each(|x| println!("{}", x)); | 198 | value.$0for_each(|x| println!("{}", x)); |
190 | }"#) | 199 | }"#, |
200 | ) | ||
191 | } | 201 | } |
192 | } \ No newline at end of file | 202 | } |