diff options
Diffstat (limited to 'crates/ra_assists/src/flip_eq_operands.rs')
-rw-r--r-- | crates/ra_assists/src/flip_eq_operands.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/crates/ra_assists/src/flip_eq_operands.rs b/crates/ra_assists/src/flip_eq_operands.rs new file mode 100644 index 000000000..df0bb689d --- /dev/null +++ b/crates/ra_assists/src/flip_eq_operands.rs | |||
@@ -0,0 +1,86 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::ast::{AstNode, BinExpr, BinOp}; | ||
3 | |||
4 | use crate::{AssistCtx, Assist, AssistId}; | ||
5 | |||
6 | pub(crate) fn flip_eq_operands(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | ||
7 | let expr = ctx.node_at_offset::<BinExpr>()?; | ||
8 | let lhs = expr.lhs()?.syntax(); | ||
9 | let rhs = expr.rhs()?.syntax(); | ||
10 | let op_range = expr.op()?.range(); | ||
11 | let cursor_in_range = ctx.frange.range.is_subrange(&op_range); | ||
12 | let allowed_ops = [BinOp::EqualityTest, BinOp::NegatedEqualityTest]; | ||
13 | let expr_op = expr.op_kind()?; | ||
14 | if !cursor_in_range || !allowed_ops.iter().any(|o| *o == expr_op) { | ||
15 | return None; | ||
16 | } | ||
17 | ctx.add_action(AssistId("flip_eq_operands"), "flip equality operands", |edit| { | ||
18 | edit.target(op_range); | ||
19 | edit.replace(lhs.range(), rhs.text()); | ||
20 | edit.replace(rhs.range(), lhs.text()); | ||
21 | }); | ||
22 | |||
23 | ctx.build() | ||
24 | } | ||
25 | |||
26 | #[cfg(test)] | ||
27 | mod tests { | ||
28 | use super::*; | ||
29 | |||
30 | use crate::helpers::{check_assist, check_assist_target}; | ||
31 | |||
32 | #[test] | ||
33 | fn flip_eq_operands_for_simple_stmt() { | ||
34 | check_assist( | ||
35 | flip_eq_operands, | ||
36 | "fn f() { let res = 1 ==<|> 2; }", | ||
37 | "fn f() { let res = 2 ==<|> 1; }", | ||
38 | ) | ||
39 | } | ||
40 | |||
41 | #[test] | ||
42 | fn flip_neq_operands_for_simple_stmt() { | ||
43 | check_assist( | ||
44 | flip_eq_operands, | ||
45 | "fn f() { let res = 1 !=<|> 2; }", | ||
46 | "fn f() { let res = 2 !=<|> 1; }", | ||
47 | ) | ||
48 | } | ||
49 | |||
50 | #[test] | ||
51 | fn flip_eq_operands_for_complex_stmt() { | ||
52 | check_assist( | ||
53 | flip_eq_operands, | ||
54 | "fn f() { let res = (1 + 1) ==<|> (2 + 2); }", | ||
55 | "fn f() { let res = (2 + 2) ==<|> (1 + 1); }", | ||
56 | ) | ||
57 | } | ||
58 | |||
59 | #[test] | ||
60 | fn flip_eq_operands_in_match_expr() { | ||
61 | check_assist( | ||
62 | flip_eq_operands, | ||
63 | r#" | ||
64 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { | ||
65 | match other.downcast_ref::<Self>() { | ||
66 | None => false, | ||
67 | Some(it) => it ==<|> self, | ||
68 | } | ||
69 | } | ||
70 | "#, | ||
71 | r#" | ||
72 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { | ||
73 | match other.downcast_ref::<Self>() { | ||
74 | None => false, | ||
75 | Some(it) => self ==<|> it, | ||
76 | } | ||
77 | } | ||
78 | "#, | ||
79 | ) | ||
80 | } | ||
81 | |||
82 | #[test] | ||
83 | fn flip_eq_operands_target() { | ||
84 | check_assist_target(flip_eq_operands, "fn f() { let res = 1 ==<|> 2; }", "==") | ||
85 | } | ||
86 | } | ||