aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/invert_if.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists/src/handlers/invert_if.rs')
-rw-r--r--crates/assists/src/handlers/invert_if.rs109
1 files changed, 109 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/invert_if.rs b/crates/assists/src/handlers/invert_if.rs
new file mode 100644
index 000000000..f0e047538
--- /dev/null
+++ b/crates/assists/src/handlers/invert_if.rs
@@ -0,0 +1,109 @@
1use syntax::{
2 ast::{self, AstNode},
3 T,
4};
5
6use 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<|> !y { A } else { B }
22// }
23// ```
24// ->
25// ```
26// fn main() {
27// if y { B } else { A }
28// }
29// ```
30
31pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
32 let if_keyword = ctx.find_token_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 let cond_range = cond.syntax().text_range();
53 let flip_cond = invert_boolean_expression(cond);
54 let else_node = else_block.syntax();
55 let else_range = else_node.text_range();
56 let then_range = then_node.text_range();
57 acc.add(AssistId("invert_if", AssistKind::RefactorRewrite), "Invert if", if_range, |edit| {
58 edit.replace(cond_range, flip_cond.syntax().text());
59 edit.replace(else_range, then_node.text());
60 edit.replace(then_range, else_node.text());
61 })
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 use crate::tests::{check_assist, check_assist_not_applicable};
69
70 #[test]
71 fn invert_if_remove_inequality() {
72 check_assist(
73 invert_if,
74 "fn f() { i<|>f x != 3 { 1 } else { 3 + 2 } }",
75 "fn f() { if x == 3 { 3 + 2 } else { 1 } }",
76 )
77 }
78
79 #[test]
80 fn invert_if_remove_not() {
81 check_assist(
82 invert_if,
83 "fn f() { <|>if !cond { 3 * 2 } else { 1 } }",
84 "fn f() { if cond { 1 } else { 3 * 2 } }",
85 )
86 }
87
88 #[test]
89 fn invert_if_general_case() {
90 check_assist(
91 invert_if,
92 "fn f() { i<|>f cond { 3 * 2 } else { 1 } }",
93 "fn f() { if !cond { 1 } else { 3 * 2 } }",
94 )
95 }
96
97 #[test]
98 fn invert_if_doesnt_apply_with_cursor_not_on_if() {
99 check_assist_not_applicable(invert_if, "fn f() { if !<|>cond { 3 * 2 } else { 1 } }")
100 }
101
102 #[test]
103 fn invert_if_doesnt_apply_with_if_let() {
104 check_assist_not_applicable(
105 invert_if,
106 "fn f() { i<|>f let Some(_) = Some(1) { 1 } else { 0 } }",
107 )
108 }
109}