diff options
Diffstat (limited to 'crates/ide_assists/src/handlers/invert_if.rs')
-rw-r--r-- | crates/ide_assists/src/handlers/invert_if.rs | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/crates/ide_assists/src/handlers/invert_if.rs b/crates/ide_assists/src/handlers/invert_if.rs new file mode 100644 index 000000000..5b69dafd4 --- /dev/null +++ b/crates/ide_assists/src/handlers/invert_if.rs | |||
@@ -0,0 +1,146 @@ | |||
1 | use syntax::{ | ||
2 | ast::{self, AstNode}, | ||
3 | T, | ||
4 | }; | ||
5 | |||
6 | use crate::{ | ||
7 | assist_context::{AssistContext, Assists}, | ||
8 | utils::invert_boolean_expression, | ||
9 | AssistId, AssistKind, | ||
10 | }; | ||
11 | |||
12 | // Assist: invert_if | ||
13 | // | ||
14 | // Apply invert_if | ||
15 | // This transforms if expressions of the form `if !x {A} else {B}` into `if x {B} else {A}` | ||
16 | // This also works with `!=`. This assist can only be applied with the cursor | ||
17 | // on `if`. | ||
18 | // | ||
19 | // ``` | ||
20 | // fn main() { | ||
21 | // if$0 !y { A } else { B } | ||
22 | // } | ||
23 | // ``` | ||
24 | // -> | ||
25 | // ``` | ||
26 | // fn main() { | ||
27 | // if y { B } else { A } | ||
28 | // } | ||
29 | // ``` | ||
30 | |||
31 | pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
32 | let if_keyword = ctx.find_token_syntax_at_offset(T![if])?; | ||
33 | let expr = ast::IfExpr::cast(if_keyword.parent())?; | ||
34 | let if_range = if_keyword.text_range(); | ||
35 | let cursor_in_range = if_range.contains_range(ctx.frange.range); | ||
36 | if !cursor_in_range { | ||
37 | return None; | ||
38 | } | ||
39 | |||
40 | // This assist should not apply for if-let. | ||
41 | if expr.condition()?.pat().is_some() { | ||
42 | return None; | ||
43 | } | ||
44 | |||
45 | let cond = expr.condition()?.expr()?; | ||
46 | let then_node = expr.then_branch()?.syntax().clone(); | ||
47 | let else_block = match expr.else_branch()? { | ||
48 | ast::ElseBranch::Block(it) => it, | ||
49 | ast::ElseBranch::IfExpr(_) => return None, | ||
50 | }; | ||
51 | |||
52 | acc.add(AssistId("invert_if", AssistKind::RefactorRewrite), "Invert if", if_range, |edit| { | ||
53 | let flip_cond = invert_boolean_expression(cond.clone()); | ||
54 | edit.replace_ast(cond, flip_cond); | ||
55 | |||
56 | let else_node = else_block.syntax(); | ||
57 | let else_range = else_node.text_range(); | ||
58 | let then_range = then_node.text_range(); | ||
59 | |||
60 | edit.replace(else_range, then_node.text()); | ||
61 | edit.replace(then_range, else_node.text()); | ||
62 | }) | ||
63 | } | ||
64 | |||
65 | #[cfg(test)] | ||
66 | mod tests { | ||
67 | use super::*; | ||
68 | |||
69 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
70 | |||
71 | #[test] | ||
72 | fn invert_if_composite_condition() { | ||
73 | check_assist( | ||
74 | invert_if, | ||
75 | "fn f() { i$0f x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }", | ||
76 | "fn f() { if !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }", | ||
77 | ) | ||
78 | } | ||
79 | |||
80 | #[test] | ||
81 | fn invert_if_remove_not_parentheses() { | ||
82 | check_assist( | ||
83 | invert_if, | ||
84 | "fn f() { i$0f !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }", | ||
85 | "fn f() { if x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }", | ||
86 | ) | ||
87 | } | ||
88 | |||
89 | #[test] | ||
90 | fn invert_if_remove_inequality() { | ||
91 | check_assist( | ||
92 | invert_if, | ||
93 | "fn f() { i$0f x != 3 { 1 } else { 3 + 2 } }", | ||
94 | "fn f() { if x == 3 { 3 + 2 } else { 1 } }", | ||
95 | ) | ||
96 | } | ||
97 | |||
98 | #[test] | ||
99 | fn invert_if_remove_not() { | ||
100 | check_assist( | ||
101 | invert_if, | ||
102 | "fn f() { $0if !cond { 3 * 2 } else { 1 } }", | ||
103 | "fn f() { if cond { 1 } else { 3 * 2 } }", | ||
104 | ) | ||
105 | } | ||
106 | |||
107 | #[test] | ||
108 | fn invert_if_general_case() { | ||
109 | check_assist( | ||
110 | invert_if, | ||
111 | "fn f() { i$0f cond { 3 * 2 } else { 1 } }", | ||
112 | "fn f() { if !cond { 1 } else { 3 * 2 } }", | ||
113 | ) | ||
114 | } | ||
115 | |||
116 | #[test] | ||
117 | fn invert_if_doesnt_apply_with_cursor_not_on_if() { | ||
118 | check_assist_not_applicable(invert_if, "fn f() { if !$0cond { 3 * 2 } else { 1 } }") | ||
119 | } | ||
120 | |||
121 | #[test] | ||
122 | fn invert_if_doesnt_apply_with_if_let() { | ||
123 | check_assist_not_applicable( | ||
124 | invert_if, | ||
125 | "fn f() { i$0f let Some(_) = Some(1) { 1 } else { 0 } }", | ||
126 | ) | ||
127 | } | ||
128 | |||
129 | #[test] | ||
130 | fn invert_if_option_case() { | ||
131 | check_assist( | ||
132 | invert_if, | ||
133 | "fn f() { if$0 doc_style.is_some() { Class::DocComment } else { Class::Comment } }", | ||
134 | "fn f() { if doc_style.is_none() { Class::Comment } else { Class::DocComment } }", | ||
135 | ) | ||
136 | } | ||
137 | |||
138 | #[test] | ||
139 | fn invert_if_result_case() { | ||
140 | check_assist( | ||
141 | invert_if, | ||
142 | "fn f() { i$0f doc_style.is_err() { Class::Err } else { Class::Ok } }", | ||
143 | "fn f() { if doc_style.is_ok() { Class::Ok } else { Class::Err } }", | ||
144 | ) | ||
145 | } | ||
146 | } | ||