Skip to content

Commit 47037ad

Browse files
authored
Merge pull request #20577 from A4-Tacks/nested-if-indent
Fix indent for merge_nested_if
2 parents ee97838 + 51908e1 commit 47037ad

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use syntax::{
22
T,
3-
ast::{self, AstNode, BinaryOp},
3+
ast::{self, AstNode, BinaryOp, edit::AstNodeEdit},
44
};
55

66
use crate::{
@@ -59,7 +59,6 @@ pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
5959
let nested_if_cond = nested_if_to_merge.condition()?;
6060

6161
let nested_if_then_branch = nested_if_to_merge.then_branch()?;
62-
let then_branch_range = then_branch.syntax().text_range();
6362

6463
acc.add(AssistId::refactor_rewrite("merge_nested_if"), "Merge nested if", if_range, |edit| {
6564
let cond_text = if has_logic_op_or(&cond) {
@@ -77,7 +76,7 @@ pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
7776
let replace_cond = format!("{cond_text} && {nested_if_cond_text}");
7877

7978
edit.replace(cond_range, replace_cond);
80-
edit.replace(then_branch_range, nested_if_then_branch.syntax().text());
79+
edit.replace_ast(then_branch, nested_if_then_branch.dedent(1.into()));
8180
})
8281
}
8382

@@ -104,8 +103,20 @@ mod tests {
104103
fn merge_nested_if_test1() {
105104
check_assist(
106105
merge_nested_if,
107-
"fn f() { i$0f x == 3 { if y == 4 { 1 } } }",
108-
"fn f() { if x == 3 && y == 4 { 1 } }",
106+
"
107+
fn f() {
108+
i$0f x == 3 {
109+
if y == 4 {
110+
1
111+
}
112+
}
113+
}",
114+
"
115+
fn f() {
116+
if x == 3 && y == 4 {
117+
1
118+
}
119+
}",
109120
)
110121
}
111122

0 commit comments

Comments
 (0)