mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-23 08:48:08 +00:00
Merge pull request #20577 from A4-Tacks/nested-if-indent
Fix indent for merge_nested_if
This commit is contained in:
commit
47037adcac
1 changed files with 16 additions and 5 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use syntax::{
|
||||
T,
|
||||
ast::{self, AstNode, BinaryOp},
|
||||
ast::{self, AstNode, BinaryOp, edit::AstNodeEdit},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
|
@ -59,7 +59,6 @@ pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
|
|||
let nested_if_cond = nested_if_to_merge.condition()?;
|
||||
|
||||
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) {
|
||||
|
|
@ -77,7 +76,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()));
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -104,8 +103,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
|
||||
}
|
||||
}",
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue