aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/flip_binexpr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/flip_binexpr.rs')
-rw-r--r--crates/ra_assists/src/flip_binexpr.rs149
1 files changed, 149 insertions, 0 deletions
diff --git a/crates/ra_assists/src/flip_binexpr.rs b/crates/ra_assists/src/flip_binexpr.rs
new file mode 100644
index 000000000..8a0737b55
--- /dev/null
+++ b/crates/ra_assists/src/flip_binexpr.rs
@@ -0,0 +1,149 @@
1use hir::db::HirDatabase;
2use ra_syntax::ast::{AstNode, BinExpr, BinOp};
3
4use crate::{AssistCtx, Assist, AssistId};
5
6/// Flip binary comparison expressions (==, !=, >, >=, <, <=).
7pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
8 let expr = ctx.node_at_offset::<BinExpr>()?;
9 let lhs = expr.lhs()?.syntax();
10 let rhs = expr.rhs()?.syntax();
11 let op_range = expr.op()?.range();
12 // The assist should be available only if the cursor is on the operator
13 let cursor_in_range = ctx.frange.range.is_subrange(&op_range);
14 // The assist should be available only for these binary operators
15 // (it should not change the meaning of the expression)
16 let allowed_ops = [
17 BinOp::EqualityTest,
18 BinOp::NegatedEqualityTest,
19 BinOp::GreaterTest,
20 BinOp::GreaterEqualTest,
21 BinOp::LesserTest,
22 BinOp::LesserEqualTest,
23 ];
24 let op_kind = expr.op_kind()?;
25 if !cursor_in_range || !allowed_ops.iter().any(|o| *o == op_kind) {
26 return None;
27 }
28 let new_op = match op_kind {
29 BinOp::GreaterTest => Some("<"),
30 BinOp::GreaterEqualTest => Some("<="),
31 BinOp::LesserTest => Some(">"),
32 BinOp::LesserEqualTest => Some(">="),
33 _ => None,
34 };
35 ctx.add_action(AssistId("flip_binexpr"), "flip binary expression", |edit| {
36 edit.target(op_range);
37 if let Some(new_op) = new_op {
38 edit.replace(op_range, new_op);
39 }
40 edit.replace(lhs.range(), rhs.text());
41 edit.replace(rhs.range(), lhs.text());
42 });
43
44 ctx.build()
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 use crate::helpers::{check_assist, check_assist_target};
52
53 #[test]
54 fn flip_eq_operands_for_simple_stmt() {
55 check_assist(
56 flip_binexpr,
57 "fn f() { let res = 1 ==<|> 2; }",
58 "fn f() { let res = 2 ==<|> 1; }",
59 )
60 }
61
62 #[test]
63 fn flip_neq_operands_for_simple_stmt() {
64 check_assist(
65 flip_binexpr,
66 "fn f() { let res = 1 !=<|> 2; }",
67 "fn f() { let res = 2 !=<|> 1; }",
68 )
69 }
70
71 #[test]
72 fn flip_gt_operands_for_simple_stmt() {
73 check_assist(
74 flip_binexpr,
75 "fn f() { let res = 1 ><|> 2; }",
76 "fn f() { let res = 2 <<|> 1; }",
77 )
78 }
79
80 #[test]
81 fn flip_gteq_operands_for_simple_stmt() {
82 check_assist(
83 flip_binexpr,
84 "fn f() { let res = 1 >=<|> 2; }",
85 "fn f() { let res = 2 <=<|> 1; }",
86 )
87 }
88
89 #[test]
90 fn flip_lt_operands_for_simple_stmt() {
91 check_assist(
92 flip_binexpr,
93 "fn f() { let res = 1 <<|> 2; }",
94 "fn f() { let res = 2 ><|> 1; }",
95 )
96 }
97
98 #[test]
99 fn flip_lteq_operands_for_simple_stmt() {
100 check_assist(
101 flip_binexpr,
102 "fn f() { let res = 1 <=<|> 2; }",
103 "fn f() { let res = 2 >=<|> 1; }",
104 )
105 }
106
107 #[test]
108 fn flip_eq_operands_for_complex_stmt() {
109 check_assist(
110 flip_binexpr,
111 "fn f() { let res = (1 + 1) ==<|> (2 + 2); }",
112 "fn f() { let res = (2 + 2) ==<|> (1 + 1); }",
113 )
114 }
115
116 #[test]
117 fn flip_eq_operands_in_match_expr() {
118 check_assist(
119 flip_binexpr,
120 r#"
121 fn dyn_eq(&self, other: &dyn Diagnostic) -> bool {
122 match other.downcast_ref::<Self>() {
123 None => false,
124 Some(it) => it ==<|> self,
125 }
126 }
127 "#,
128 r#"
129 fn dyn_eq(&self, other: &dyn Diagnostic) -> bool {
130 match other.downcast_ref::<Self>() {
131 None => false,
132 Some(it) => self ==<|> it,
133 }
134 }
135 "#,
136 )
137 }
138
139 #[test]
140 fn flip_eq_operands_target() {
141 check_assist_target(flip_binexpr, "fn f() { let res = 1 ==<|> 2; }", "==")
142 }
143
144 #[test]
145 fn flip_gt_operands_target() {
146 check_assist_target(flip_binexpr, "fn f() { let res = 1 ><|> 2; }", ">")
147 }
148
149}