Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions crates/ide-assists/src/handlers/merge_nested_if.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ide_db::syntax_helpers::node_ext::is_pattern_cond;
use syntax::{
T,
ast::{self, AstNode, BinaryOp},
ast::{self, AstNode, BinaryOp, edit::AstNodeEdit},
};

use crate::{
Expand Down Expand Up @@ -67,7 +67,6 @@ pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
}

let nested_if_then_branch = nested_if_to_merge.then_branch()?;
let then_branch_range = then_branch.syntax().text_range();

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

edit.replace(cond_range, replace_cond);
edit.replace(then_branch_range, nested_if_then_branch.syntax().text());
edit.replace_ast(then_branch, nested_if_then_branch.dedent(1.into()));
})
}

Expand All @@ -112,8 +111,20 @@ mod tests {
fn merge_nested_if_test1() {
check_assist(
merge_nested_if,
"fn f() { i$0f x == 3 { if y == 4 { 1 } } }",
"fn f() { if x == 3 && y == 4 { 1 } }",
"
fn f() {
i$0f x == 3 {
if y == 4 {
1
}
}
}",
"
fn f() {
if x == 3 && y == 4 {
1
}
}",
)
}

Expand Down
Loading