@@ -19,8 +19,19 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
1919// ```
2020pub ( crate ) fn flip_binexpr ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
2121 let expr = ctx. find_node_at_offset :: < BinExpr > ( ) ?;
22- let lhs = expr. lhs ( ) ?. syntax ( ) . clone ( ) ;
2322 let rhs = expr. rhs ( ) ?. syntax ( ) . clone ( ) ;
23+ let lhs = expr. lhs ( ) ?. syntax ( ) . clone ( ) ;
24+
25+ let lhs = if let Some ( bin_expr) = BinExpr :: cast ( lhs. clone ( ) ) {
26+ if bin_expr. op_kind ( ) == expr. op_kind ( ) {
27+ bin_expr. rhs ( ) ?. syntax ( ) . clone ( )
28+ } else {
29+ lhs
30+ }
31+ } else {
32+ lhs
33+ } ;
34+
2435 let op_range = expr. op_token ( ) ?. text_range ( ) ;
2536 // The assist should be applied only if the cursor is on the operator
2637 let cursor_in_range = op_range. contains_range ( ctx. selection_trimmed ( ) ) ;
@@ -33,15 +44,6 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
3344 return None ;
3445 }
3546
36- // If the lhs is a binary expression we check if its rhs can be used as the lhs of the current expression
37- let lhs = match BinExpr :: cast ( lhs. clone ( ) ) {
38- Some ( lhs) => match lhs. rhs ( ) {
39- Some ( lhs) => lhs,
40- None => lhs,
41- } ,
42- None => lhs,
43- } ;
44-
4547 acc. add (
4648 AssistId ( "flip_binexpr" , AssistKind :: RefactorRewrite ) ,
4749 "Flip binary expression" ,
@@ -124,14 +126,23 @@ mod tests {
124126 }
125127
126128 #[ test]
127- fn flip_binexpr_works_for_lhs_binexpr ( ) {
129+ fn flip_binexpr_works_for_lhs_arith ( ) {
128130 check_assist (
129131 flip_binexpr,
130132 r"fn f() { let res = 1 + (2 - 3) +$0 4 + 5; }" ,
131133 r"fn f() { let res = 1 + 4 + (2 - 3) + 5; }" ,
132134 )
133135 }
134136
137+ #[ test]
138+ fn flip_binexpr_works_for_lhs_cmp ( ) {
139+ check_assist (
140+ flip_binexpr,
141+ r"fn f() { let res = 1 + (2 - 3) >$0 4 + 5; }" ,
142+ r"fn f() { let res = 4 + 5 < 1 + (2 - 3); }" ,
143+ )
144+ }
145+
135146 #[ test]
136147 fn flip_binexpr_works_inside_match ( ) {
137148 check_assist (
0 commit comments