@@ -19,8 +19,19 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
19
19
// ```
20
20
pub ( crate ) fn flip_binexpr ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
21
21
let expr = ctx. find_node_at_offset :: < BinExpr > ( ) ?;
22
- let lhs = expr. lhs ( ) ?. syntax ( ) . clone ( ) ;
23
22
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
+
24
35
let op_range = expr. op_token ( ) ?. text_range ( ) ;
25
36
// The assist should be applied only if the cursor is on the operator
26
37
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
33
44
return None ;
34
45
}
35
46
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
-
45
47
acc. add (
46
48
AssistId ( "flip_binexpr" , AssistKind :: RefactorRewrite ) ,
47
49
"Flip binary expression" ,
@@ -124,14 +126,23 @@ mod tests {
124
126
}
125
127
126
128
#[ test]
127
- fn flip_binexpr_works_for_lhs_binexpr ( ) {
129
+ fn flip_binexpr_works_for_lhs_arith ( ) {
128
130
check_assist (
129
131
flip_binexpr,
130
132
r"fn f() { let res = 1 + (2 - 3) +$0 4 + 5; }" ,
131
133
r"fn f() { let res = 1 + 4 + (2 - 3) + 5; }" ,
132
134
)
133
135
}
134
136
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
+
135
146
#[ test]
136
147
fn flip_binexpr_works_inside_match ( ) {
137
148
check_assist (
0 commit comments