Skip to main content

ide_assists/handlers/
flip_binexpr.rs

1use syntax::{
2    SyntaxKind, T,
3    ast::{self, AstNode, BinExpr, RangeItem},
4    syntax_editor::Position,
5};
6
7use crate::{AssistContext, AssistId, Assists};
8
9// Assist: flip_binexpr
10//
11// Flips operands of a binary expression.
12//
13// ```
14// fn main() {
15//     let _ = 90 +$0 2;
16// }
17// ```
18// ->
19// ```
20// fn main() {
21//     let _ = 2 + 90;
22// }
23// ```
24pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> Option<()> {
25    let expr = ctx.find_node_at_offset::<BinExpr>()?;
26    let lhs = expr.lhs()?;
27    let rhs = expr.rhs()?;
28
29    let lhs = match &lhs {
30        ast::Expr::BinExpr(bin_expr) if bin_expr.op_kind() == expr.op_kind() => bin_expr.rhs()?,
31        _ => lhs,
32    };
33
34    let op_token = expr.op_token()?;
35    // The assist should be applied only if the cursor is on the operator
36    let cursor_in_range = op_token.text_range().contains_range(ctx.selection_trimmed());
37    if !cursor_in_range {
38        return None;
39    }
40    let action: FlipAction = expr.op_kind()?.into();
41    // The assist should not be applied for certain operators
42    if let FlipAction::DontFlip = action {
43        return None;
44    }
45
46    acc.add(
47        AssistId::refactor_rewrite("flip_binexpr"),
48        "Flip binary expression",
49        op_token.text_range(),
50        |builder| {
51            let editor = builder.make_editor(&expr.syntax().parent().unwrap());
52            let make = editor.make();
53            if let FlipAction::FlipAndReplaceOp(binary_op) = action {
54                editor.replace(op_token, make.token(binary_op))
55            };
56            editor.replace(lhs.syntax(), rhs.syntax());
57            editor.replace(rhs.syntax(), lhs.syntax());
58            builder.add_file_edits(ctx.vfs_file_id(), editor);
59        },
60    )
61}
62
63enum FlipAction {
64    // Flip the expression
65    Flip,
66    // Flip the expression and replace the operator with this string
67    FlipAndReplaceOp(SyntaxKind),
68    // Do not flip the expression
69    DontFlip,
70}
71
72impl From<ast::BinaryOp> for FlipAction {
73    fn from(op_kind: ast::BinaryOp) -> Self {
74        match op_kind {
75            ast::BinaryOp::Assignment { .. } => FlipAction::DontFlip,
76            ast::BinaryOp::CmpOp(ast::CmpOp::Ord { ordering, strict }) => {
77                let rev_op = match (ordering, strict) {
78                    (ast::Ordering::Less, true) => T![>],
79                    (ast::Ordering::Less, false) => T![>=],
80                    (ast::Ordering::Greater, true) => T![<],
81                    (ast::Ordering::Greater, false) => T![<=],
82                };
83                FlipAction::FlipAndReplaceOp(rev_op)
84            }
85            _ => FlipAction::Flip,
86        }
87    }
88}
89
90// Assist: flip_range_expr
91//
92// Flips operands of a range expression.
93//
94// ```
95// fn main() {
96//     let _ = 90..$02;
97// }
98// ```
99// ->
100// ```
101// fn main() {
102//     let _ = 2..90;
103// }
104// ```
105// ---
106// ```
107// fn main() {
108//     let _ = 90..$0;
109// }
110// ```
111// ->
112// ```
113// fn main() {
114//     let _ = ..90;
115// }
116// ```
117pub(crate) fn flip_range_expr(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> Option<()> {
118    let range_expr = ctx.find_node_at_offset::<ast::RangeExpr>()?;
119    let op = range_expr.op_token()?;
120    let start = range_expr.start();
121    let end = range_expr.end();
122
123    if !op.text_range().contains_range(ctx.selection_trimmed()) {
124        return None;
125    }
126    if start.is_none() && end.is_none() {
127        return None;
128    }
129
130    acc.add(
131        AssistId::refactor_rewrite("flip_range_expr"),
132        "Flip range expression",
133        op.text_range(),
134        |builder| {
135            let editor = builder.make_editor(range_expr.syntax());
136
137            match (start, end) {
138                (Some(start), Some(end)) => {
139                    editor.replace(start.syntax(), end.syntax());
140                    editor.replace(end.syntax(), start.syntax());
141                }
142                (Some(start), None) => {
143                    editor.delete(start.syntax());
144                    editor.insert(Position::after(&op), start.syntax());
145                }
146                (None, Some(end)) => {
147                    editor.delete(end.syntax());
148                    editor.insert(Position::before(&op), end.syntax());
149                }
150                (None, None) => (),
151            }
152
153            builder.add_file_edits(ctx.vfs_file_id(), editor);
154        },
155    )
156}
157
158#[cfg(test)]
159mod tests {
160    use super::*;
161
162    use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
163
164    #[test]
165    fn flip_binexpr_target_is_the_op() {
166        check_assist_target(flip_binexpr, "fn f() { let res = 1 ==$0 2; }", "==")
167    }
168
169    #[test]
170    fn flip_binexpr_not_applicable_for_assignment() {
171        check_assist_not_applicable(flip_binexpr, "fn f() { let mut _x = 1; _x +=$0 2 }")
172    }
173
174    #[test]
175    fn flip_binexpr_works_for_eq() {
176        check_assist(flip_binexpr, "fn f() { let res = 1 ==$0 2; }", "fn f() { let res = 2 == 1; }")
177    }
178
179    #[test]
180    fn flip_binexpr_works_for_gt() {
181        check_assist(flip_binexpr, "fn f() { let res = 1 >$0 2; }", "fn f() { let res = 2 < 1; }")
182    }
183
184    #[test]
185    fn flip_binexpr_works_for_lteq() {
186        check_assist(flip_binexpr, "fn f() { let res = 1 <=$0 2; }", "fn f() { let res = 2 >= 1; }")
187    }
188
189    #[test]
190    fn flip_binexpr_works_for_complex_expr() {
191        check_assist(
192            flip_binexpr,
193            "fn f() { let res = (1 + 1) ==$0 (2 + 2); }",
194            "fn f() { let res = (2 + 2) == (1 + 1); }",
195        )
196    }
197
198    #[test]
199    fn flip_binexpr_works_for_lhs_arith() {
200        check_assist(
201            flip_binexpr,
202            r"fn f() { let res = 1 + (2 - 3) +$0 4 + 5; }",
203            r"fn f() { let res = 1 + 4 + (2 - 3) + 5; }",
204        )
205    }
206
207    #[test]
208    fn flip_binexpr_works_for_lhs_cmp() {
209        check_assist(
210            flip_binexpr,
211            r"fn f() { let res = 1 + (2 - 3) >$0 4 + 5; }",
212            r"fn f() { let res = 4 + 5 < 1 + (2 - 3); }",
213        )
214    }
215
216    #[test]
217    fn flip_binexpr_works_inside_match() {
218        check_assist(
219            flip_binexpr,
220            r#"
221            fn dyn_eq(&self, other: &dyn Diagnostic) -> bool {
222                match other.downcast_ref::<Self>() {
223                    None => false,
224                    Some(it) => it ==$0 self,
225                }
226            }
227            "#,
228            r#"
229            fn dyn_eq(&self, other: &dyn Diagnostic) -> bool {
230                match other.downcast_ref::<Self>() {
231                    None => false,
232                    Some(it) => self == it,
233                }
234            }
235            "#,
236        )
237    }
238}