Skip to content

Commit d6a1e31

Browse files
committed
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), } } } } ```
1 parent ccb2ffe commit d6a1e31

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ide_db::famous_defs::FamousDefs;
33
use stdx::format_to;
44
use syntax::{
55
AstNode,
6-
ast::{self, HasArgList, HasLoopBody, edit_in_place::Indent, syntax_factory::SyntaxFactory},
6+
ast::{self, HasArgList, HasLoopBody, edit::AstNodeEdit, syntax_factory::SyntaxFactory},
77
};
88

99
use crate::{AssistContext, AssistId, Assists};
@@ -62,10 +62,10 @@ pub(crate) fn convert_iter_for_each_to_for(
6262
stmt.as_ref().map_or_else(|| method.indent_level(), ast::ExprStmt::indent_level);
6363

6464
let block = match body {
65-
ast::Expr::BlockExpr(block) => block.clone_for_update(),
66-
_ => make.block_expr(Vec::new(), Some(body)),
67-
};
68-
block.reindent_to(indent);
65+
ast::Expr::BlockExpr(block) => block.reset_indent(),
66+
_ => make.block_expr(Vec::new(), Some(body.reset_indent().indent(1.into()))),
67+
}
68+
.indent(indent);
6969

7070
let expr_for_loop = make.expr_for_loop(param, receiver, block);
7171

@@ -285,15 +285,23 @@ fn main() {
285285
r#"
286286
//- minicore: iterators
287287
fn main() {
288-
let it = core::iter::repeat(92);
289-
it.$0for_each(|(x, y)| println!("x: {}, y: {}", x, y));
288+
{
289+
let it = core::iter::repeat(92);
290+
it.$0for_each(|param| match param {
291+
(x, y) => println!("x: {}, y: {}", x, y),
292+
});
293+
}
290294
}
291295
"#,
292296
r#"
293297
fn main() {
294-
let it = core::iter::repeat(92);
295-
for (x, y) in it {
296-
println!("x: {}, y: {}", x, y)
298+
{
299+
let it = core::iter::repeat(92);
300+
for param in it {
301+
match param {
302+
(x, y) => println!("x: {}, y: {}", x, y),
303+
}
304+
}
297305
}
298306
}
299307
"#,

0 commit comments

Comments
 (0)