diff options
-rw-r--r-- | crates/ra_assists/src/flip_binexpr.rs | 141 | ||||
-rw-r--r-- | crates/ra_assists/src/flip_eq_operands.rs | 86 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 4 |
3 files changed, 143 insertions, 88 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..ec377642e --- /dev/null +++ b/crates/ra_assists/src/flip_binexpr.rs | |||
@@ -0,0 +1,141 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::ast::{AstNode, BinExpr, BinOp}; | ||
3 | |||
4 | use crate::{AssistCtx, Assist, AssistId}; | ||
5 | |||
6 | /// Flip binary expression assist. | ||
7 | pub(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 applied only if the cursor is on the operator | ||
13 | let cursor_in_range = ctx.frange.range.is_subrange(&op_range); | ||
14 | if !cursor_in_range { | ||
15 | return None; | ||
16 | } | ||
17 | let action: FlipAction = expr.op_kind()?.into(); | ||
18 | // The assist should not be applied for certain operators | ||
19 | if let FlipAction::DontFlip = action { | ||
20 | return None; | ||
21 | } | ||
22 | |||
23 | ctx.add_action(AssistId("flip_binexpr"), "flip binary expression", |edit| { | ||
24 | edit.target(op_range); | ||
25 | if let FlipAction::FlipAndReplaceOp(new_op) = action { | ||
26 | edit.replace(op_range, new_op); | ||
27 | } | ||
28 | edit.replace(lhs.range(), rhs.text()); | ||
29 | edit.replace(rhs.range(), lhs.text()); | ||
30 | }); | ||
31 | |||
32 | ctx.build() | ||
33 | } | ||
34 | |||
35 | enum FlipAction { | ||
36 | // Flip the expression | ||
37 | Flip, | ||
38 | // Flip the expression and replace the operator with this string | ||
39 | FlipAndReplaceOp(&'static str), | ||
40 | // Do not flip the expression | ||
41 | DontFlip, | ||
42 | } | ||
43 | |||
44 | impl From<BinOp> for FlipAction { | ||
45 | fn from(op_kind: BinOp) -> Self { | ||
46 | match op_kind { | ||
47 | BinOp::Assignment => FlipAction::DontFlip, | ||
48 | BinOp::AddAssign => FlipAction::DontFlip, | ||
49 | BinOp::DivAssign => FlipAction::DontFlip, | ||
50 | BinOp::MulAssign => FlipAction::DontFlip, | ||
51 | BinOp::RemAssign => FlipAction::DontFlip, | ||
52 | BinOp::ShrAssign => FlipAction::DontFlip, | ||
53 | BinOp::ShlAssign => FlipAction::DontFlip, | ||
54 | BinOp::SubAssign => FlipAction::DontFlip, | ||
55 | BinOp::BitOrAssign => FlipAction::DontFlip, | ||
56 | BinOp::BitAndAssign => FlipAction::DontFlip, | ||
57 | BinOp::BitXorAssign => FlipAction::DontFlip, | ||
58 | BinOp::GreaterTest => FlipAction::FlipAndReplaceOp("<"), | ||
59 | BinOp::GreaterEqualTest => FlipAction::FlipAndReplaceOp("<="), | ||
60 | BinOp::LesserTest => FlipAction::FlipAndReplaceOp(">"), | ||
61 | BinOp::LesserEqualTest => FlipAction::FlipAndReplaceOp(">="), | ||
62 | _ => FlipAction::Flip, | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | #[cfg(test)] | ||
68 | mod tests { | ||
69 | use super::*; | ||
70 | |||
71 | use crate::helpers::{ check_assist, check_assist_target, check_assist_not_applicable }; | ||
72 | |||
73 | #[test] | ||
74 | fn flip_binexpr_target_is_the_op() { | ||
75 | check_assist_target(flip_binexpr, "fn f() { let res = 1 ==<|> 2; }", "==") | ||
76 | } | ||
77 | |||
78 | #[test] | ||
79 | fn flip_binexpr_not_applicable_for_assignment() { | ||
80 | check_assist_not_applicable(flip_binexpr, "fn f() { let mut _x = 1; _x +=<|> 2 }") | ||
81 | } | ||
82 | |||
83 | #[test] | ||
84 | fn flip_binexpr_works_for_eq() { | ||
85 | check_assist( | ||
86 | flip_binexpr, | ||
87 | "fn f() { let res = 1 ==<|> 2; }", | ||
88 | "fn f() { let res = 2 ==<|> 1; }", | ||
89 | ) | ||
90 | } | ||
91 | |||
92 | #[test] | ||
93 | fn flip_binexpr_works_for_gt() { | ||
94 | check_assist( | ||
95 | flip_binexpr, | ||
96 | "fn f() { let res = 1 ><|> 2; }", | ||
97 | "fn f() { let res = 2 <<|> 1; }", | ||
98 | ) | ||
99 | } | ||
100 | |||
101 | #[test] | ||
102 | fn flip_binexpr_works_for_lteq() { | ||
103 | check_assist( | ||
104 | flip_binexpr, | ||
105 | "fn f() { let res = 1 <=<|> 2; }", | ||
106 | "fn f() { let res = 2 >=<|> 1; }", | ||
107 | ) | ||
108 | } | ||
109 | |||
110 | #[test] | ||
111 | fn flip_binexpr_works_for_complex_expr() { | ||
112 | check_assist( | ||
113 | flip_binexpr, | ||
114 | "fn f() { let res = (1 + 1) ==<|> (2 + 2); }", | ||
115 | "fn f() { let res = (2 + 2) ==<|> (1 + 1); }", | ||
116 | ) | ||
117 | } | ||
118 | |||
119 | #[test] | ||
120 | fn flip_binexpr_works_inside_match() { | ||
121 | check_assist( | ||
122 | flip_binexpr, | ||
123 | r#" | ||
124 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { | ||
125 | match other.downcast_ref::<Self>() { | ||
126 | None => false, | ||
127 | Some(it) => it ==<|> self, | ||
128 | } | ||
129 | } | ||
130 | "#, | ||
131 | r#" | ||
132 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { | ||
133 | match other.downcast_ref::<Self>() { | ||
134 | None => false, | ||
135 | Some(it) => self ==<|> it, | ||
136 | } | ||
137 | } | ||
138 | "#, | ||
139 | ) | ||
140 | } | ||
141 | } | ||
diff --git a/crates/ra_assists/src/flip_eq_operands.rs b/crates/ra_assists/src/flip_eq_operands.rs deleted file mode 100644 index df0bb689d..000000000 --- a/crates/ra_assists/src/flip_eq_operands.rs +++ /dev/null | |||
@@ -1,86 +0,0 @@ | |||
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 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 2e47b5215..c1514f8e5 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -88,7 +88,7 @@ where | |||
88 | mod add_derive; | 88 | mod add_derive; |
89 | mod add_impl; | 89 | mod add_impl; |
90 | mod flip_comma; | 90 | mod flip_comma; |
91 | mod flip_eq_operands; | 91 | mod flip_binexpr; |
92 | mod change_visibility; | 92 | mod change_visibility; |
93 | mod fill_match_arms; | 93 | mod fill_match_arms; |
94 | mod fill_struct_fields; | 94 | mod fill_struct_fields; |
@@ -108,7 +108,7 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis | |||
108 | fill_match_arms::fill_match_arms, | 108 | fill_match_arms::fill_match_arms, |
109 | fill_struct_fields::fill_struct_fields, | 109 | fill_struct_fields::fill_struct_fields, |
110 | flip_comma::flip_comma, | 110 | flip_comma::flip_comma, |
111 | flip_eq_operands::flip_eq_operands, | 111 | flip_binexpr::flip_binexpr, |
112 | introduce_variable::introduce_variable, | 112 | introduce_variable::introduce_variable, |
113 | replace_if_let_with_match::replace_if_let_with_match, | 113 | replace_if_let_with_match::replace_if_let_with_match, |
114 | split_import::split_import, | 114 | split_import::split_import, |