Unnamed repository; edit this file 'description' to name the repository.
Fix indent for convert_iter_for_each_to_for
Example --- ```rust fn main() { { let it = core::iter::repeat(92); it.$0for_each(|param| match param { (x, y) => println!("x: {}, y: {}", x, y), }); } } ``` **Before this PR**: ```rust fn main() { { let it = core::iter::repeat(92); for param in it { match param { (x, y) => println!("x: {}, y: {}", x, y), } } } } ``` **After this PR**: ```rust fn main() { { let it = core::iter::repeat(92); for param in it { match param { (x, y) => println!("x: {}, y: {}", x, y), } } } } ```
A4-Tacks 4 months ago
parent ccb2ffe · commit d6a1e31
-rw-r--r--crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs28
1 files changed, 18 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 2eea4f71ed..63b1a0193b 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
@@ -3,7 +3,7 @@ use ide_db::famous_defs::FamousDefs;
use stdx::format_to;
use syntax::{
AstNode,
- ast::{self, HasArgList, HasLoopBody, edit_in_place::Indent, syntax_factory::SyntaxFactory},
+ ast::{self, HasArgList, HasLoopBody, edit::AstNodeEdit, syntax_factory::SyntaxFactory},
};
use crate::{AssistContext, AssistId, Assists};
@@ -62,10 +62,10 @@ pub(crate) fn convert_iter_for_each_to_for(
stmt.as_ref().map_or_else(|| method.indent_level(), ast::ExprStmt::indent_level);
let block = match body {
- ast::Expr::BlockExpr(block) => block.clone_for_update(),
- _ => make.block_expr(Vec::new(), Some(body)),
- };
- block.reindent_to(indent);
+ ast::Expr::BlockExpr(block) => block.reset_indent(),
+ _ => make.block_expr(Vec::new(), Some(body.reset_indent().indent(1.into()))),
+ }
+ .indent(indent);
let expr_for_loop = make.expr_for_loop(param, receiver, block);
@@ -285,15 +285,23 @@ fn main() {
r#"
//- minicore: iterators
fn main() {
- let it = core::iter::repeat(92);
- it.$0for_each(|(x, y)| println!("x: {}, y: {}", x, y));
+ {
+ let it = core::iter::repeat(92);
+ it.$0for_each(|param| match param {
+ (x, y) => println!("x: {}, y: {}", x, y),
+ });
+ }
}
"#,
r#"
fn main() {
- let it = core::iter::repeat(92);
- for (x, y) in it {
- println!("x: {}, y: {}", x, y)
+ {
+ let it = core::iter::repeat(92);
+ for param in it {
+ match param {
+ (x, y) => println!("x: {}, y: {}", x, y),
+ }
+ }
}
}
"#,