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