From e18b89452efc8e036ad0f95ff00d8e9e333a5c1a Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Fri, 25 Aug 2023 15:47:27 -0300 Subject: [PATCH 1/2] Flip binexpr works for lhs binexpr --- .../ide-assists/src/handlers/flip_binexpr.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/ide-assists/src/handlers/flip_binexpr.rs b/crates/ide-assists/src/handlers/flip_binexpr.rs index 2ea6f58fa0..4b1e416596 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", @@ -114,6 +123,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( From 3d9221291fb6630c90d1956c2f56d1bbbac55c93 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Fri, 25 Aug 2023 20:14:35 -0300 Subject: [PATCH 2/2] flip binexpr works for lhs cmp --- .../ide-assists/src/handlers/flip_binexpr.rs | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/crates/ide-assists/src/handlers/flip_binexpr.rs b/crates/ide-assists/src/handlers/flip_binexpr.rs index 4b1e416596..8b46a23f9a 100644 --- a/crates/ide-assists/src/handlers/flip_binexpr.rs +++ b/crates/ide-assists/src/handlers/flip_binexpr.rs @@ -19,8 +19,19 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // ``` pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let expr = ctx.find_node_at_offset::()?; - let lhs = expr.lhs()?.syntax().clone(); let rhs = expr.rhs()?.syntax().clone(); + let lhs = expr.lhs()?.syntax().clone(); + + let lhs = if let Some(bin_expr) = BinExpr::cast(lhs.clone()) { + if bin_expr.op_kind() == expr.op_kind() { + bin_expr.rhs()?.syntax().clone() + } else { + lhs + } + } else { + lhs + }; + let op_range = expr.op_token()?.text_range(); // The assist should be applied only if the cursor is on the operator 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 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", @@ -124,7 +126,7 @@ mod tests { } #[test] - fn flip_binexpr_works_for_lhs_binexpr() { + fn flip_binexpr_works_for_lhs_arith() { check_assist( flip_binexpr, r"fn f() { let res = 1 + (2 - 3) +$0 4 + 5; }", @@ -132,6 +134,15 @@ mod tests { ) } + #[test] + fn flip_binexpr_works_for_lhs_cmp() { + check_assist( + flip_binexpr, + r"fn f() { let res = 1 + (2 - 3) >$0 4 + 5; }", + r"fn f() { let res = 4 + 5 < 1 + (2 - 3); }", + ) + } + #[test] fn flip_binexpr_works_inside_match() { check_assist(