diff options
| author | bors <bors@rust-lang.org> | 2023-12-08 10:38:03 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-12-08 10:38:03 +0000 |
| commit | 5ae781562e412114fb72b5be7d26af5bb86c29d5 (patch) | |
| tree | bb8933b23f1c1ff82c95bc9f70845768b5402d80 | |
| parent | c27fc0c945e467177e0b01d25c510d559bdce4f4 (diff) | |
| parent | 3d9221291fb6630c90d1956c2f56d1bbbac55c93 (diff) | |
| download | rust-5ae781562e412114fb72b5be7d26af5bb86c29d5.tar.gz rust-5ae781562e412114fb72b5be7d26af5bb86c29d5.zip | |
Auto merge of #15515 - cardoso:flip-binexpr/lhs-binexpr, r=Veykril
Check if lhs is also a binexpr and use its rhs in flip binexpr assist Closes #15508 From the original PR, flip binexpr assist is not meant to preserve equivalence, so I went with the simplest solution here. I can add some extra checks to keep equivalence, but I think they should go in different specific assists (eg. flip arith op / flip logic op / etc), otherwise this one will get out of hand pretty quickly.
| -rw-r--r-- | crates/ide-assists/src/handlers/flip_binexpr.rs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/crates/ide-assists/src/handlers/flip_binexpr.rs b/crates/ide-assists/src/handlers/flip_binexpr.rs index 2ea6f58fa0f..8b46a23f9a6 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::<BinExpr>()?; - 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()); @@ -115,6 +126,24 @@ mod tests { } #[test] + fn flip_binexpr_works_for_lhs_arith() { + 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_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( flip_binexpr, |
