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