about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatheus Cardoso <matheus@cardo.so>2023-08-25 15:47:27 -0300
committerLukas Wirth <lukastw97@gmail.com>2023-12-08 11:36:30 +0100
commite18b89452efc8e036ad0f95ff00d8e9e333a5c1a (patch)
tree402f7f8a31211b6f559b29228df36f93d73d9aa6
parentbc9c952b6d02bf889fff84ba508423b1daca381e (diff)
downloadrust-e18b89452efc8e036ad0f95ff00d8e9e333a5c1a.tar.gz
rust-e18b89452efc8e036ad0f95ff00d8e9e333a5c1a.zip
Flip binexpr works for lhs binexpr
-rw-r--r--crates/ide-assists/src/handlers/flip_binexpr.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/flip_binexpr.rs b/crates/ide-assists/src/handlers/flip_binexpr.rs
index 2ea6f58fa0f..4b1e4165965 100644
--- a/crates/ide-assists/src/handlers/flip_binexpr.rs
+++ b/crates/ide-assists/src/handlers/flip_binexpr.rs
@@ -33,6 +33,15 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
         return None;
     }
 
+    // If the lhs is a binary expression we check if its rhs can be used as the lhs of the current expression
+    let lhs = match BinExpr::cast(lhs.clone()) {
+        Some(lhs) => match lhs.rhs() {
+            Some(lhs) => lhs,
+            None => lhs,
+        },
+        None => lhs,
+    };
+
     acc.add(
         AssistId("flip_binexpr", AssistKind::RefactorRewrite),
         "Flip binary expression",
@@ -115,6 +124,15 @@ mod tests {
     }
 
     #[test]
+    fn flip_binexpr_works_for_lhs_binexpr() {
+        check_assist(
+            flip_binexpr,
+            r"fn f() { let res = 1 + (2 - 3) +$0 4 + 5; }",
+            r"fn f() { let res = 1 + 4 + (2 - 3) + 5; }",
+        )
+    }
+
+    #[test]
     fn flip_binexpr_works_inside_match() {
         check_assist(
             flip_binexpr,