11use syntax:: {
22 T ,
3- ast:: { self , AstNode , BinaryOp } ,
3+ ast:: { self , AstNode , BinaryOp , edit :: AstNodeEdit } ,
44} ;
55
66use 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