Skip to content

Commit 5ae7815

Browse files
committed
Auto merge of rust-lang#15515 - cardoso:flip-binexpr/lhs-binexpr, r=Veykril
Check if lhs is also a binexpr and use its rhs in flip binexpr assist Closes rust-lang#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.
2 parents c27fc0c + 3d92212 commit 5ae7815

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

crates/ide-assists/src/handlers/flip_binexpr.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,19 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
1919
// ```
2020
pub(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());
@@ -114,6 +125,24 @@ mod tests {
114125
)
115126
}
116127

128+
#[test]
129+
fn flip_binexpr_works_for_lhs_arith() {
130+
check_assist(
131+
flip_binexpr,
132+
r"fn f() { let res = 1 + (2 - 3) +$0 4 + 5; }",
133+
r"fn f() { let res = 1 + 4 + (2 - 3) + 5; }",
134+
)
135+
}
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+
117146
#[test]
118147
fn flip_binexpr_works_inside_match() {
119148
check_assist(

0 commit comments

Comments
 (0)