aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/flip_binexpr.rs
blob: 8a0737b5563ca83272b43b939a6cca8e3a9aa4a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use hir::db::HirDatabase;
use ra_syntax::ast::{AstNode, BinExpr, BinOp};

use crate::{AssistCtx, Assist, AssistId};

/// Flip binary comparison expressions (==, !=, >, >=, <, <=).
pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
    let expr = ctx.node_at_offset::<BinExpr>()?;
    let lhs = expr.lhs()?.syntax();
    let rhs = expr.rhs()?.syntax();
    let op_range = expr.op()?.range();
    // The assist should be available only if the cursor is on the operator
    let cursor_in_range = ctx.frange.range.is_subrange(&op_range);
    // The assist should be available only for these binary operators
    // (it should not change the meaning of the expression)
    let allowed_ops = [
        BinOp::EqualityTest,
        BinOp::NegatedEqualityTest,
        BinOp::GreaterTest,
        BinOp::GreaterEqualTest,
        BinOp::LesserTest,
        BinOp::LesserEqualTest,
    ];
    let op_kind = expr.op_kind()?;
    if !cursor_in_range || !allowed_ops.iter().any(|o| *o == op_kind) {
        return None;
    }
    let new_op = match op_kind {
        BinOp::GreaterTest => Some("<"),
        BinOp::GreaterEqualTest => Some("<="),
        BinOp::LesserTest => Some(">"),
        BinOp::LesserEqualTest => Some(">="),
        _ => None,
    };
    ctx.add_action(AssistId("flip_binexpr"), "flip binary expression", |edit| {
        edit.target(op_range);
        if let Some(new_op) = new_op {
            edit.replace(op_range, new_op);
        }
        edit.replace(lhs.range(), rhs.text());
        edit.replace(rhs.range(), lhs.text());
    });

    ctx.build()
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::helpers::{check_assist, check_assist_target};

    #[test]
    fn flip_eq_operands_for_simple_stmt() {
        check_assist(
            flip_binexpr,
            "fn f() { let res = 1 ==<|> 2; }",
            "fn f() { let res = 2 ==<|> 1; }",
        )
    }

    #[test]
    fn flip_neq_operands_for_simple_stmt() {
        check_assist(
            flip_binexpr,
            "fn f() { let res = 1 !=<|> 2; }",
            "fn f() { let res = 2 !=<|> 1; }",
        )
    }

    #[test]
    fn flip_gt_operands_for_simple_stmt() {
        check_assist(
            flip_binexpr,
            "fn f() { let res = 1 ><|> 2; }",
            "fn f() { let res = 2 <<|> 1; }",
        )
    }

    #[test]
    fn flip_gteq_operands_for_simple_stmt() {
        check_assist(
            flip_binexpr,
            "fn f() { let res = 1 >=<|> 2; }",
            "fn f() { let res = 2 <=<|> 1; }",
        )
    }

    #[test]
    fn flip_lt_operands_for_simple_stmt() {
        check_assist(
            flip_binexpr,
            "fn f() { let res = 1 <<|> 2; }",
            "fn f() { let res = 2 ><|> 1; }",
        )
    }

    #[test]
    fn flip_lteq_operands_for_simple_stmt() {
        check_assist(
            flip_binexpr,
            "fn f() { let res = 1 <=<|> 2; }",
            "fn f() { let res = 2 >=<|> 1; }",
        )
    }

    #[test]
    fn flip_eq_operands_for_complex_stmt() {
        check_assist(
            flip_binexpr,
            "fn f() { let res = (1 + 1) ==<|> (2 + 2); }",
            "fn f() { let res = (2 + 2) ==<|> (1 + 1); }",
        )
    }

    #[test]
    fn flip_eq_operands_in_match_expr() {
        check_assist(
            flip_binexpr,
            r#"
            fn dyn_eq(&self, other: &dyn Diagnostic) -> bool {
                match other.downcast_ref::<Self>() {
                    None => false,
                    Some(it) => it ==<|> self,
                }
            }
            "#,
            r#"
            fn dyn_eq(&self, other: &dyn Diagnostic) -> bool {
                match other.downcast_ref::<Self>() {
                    None => false,
                    Some(it) => self ==<|> it,
                }
            }
            "#,
        )
    }

    #[test]
    fn flip_eq_operands_target() {
        check_assist_target(flip_binexpr, "fn f() { let res = 1 ==<|> 2; }", "==")
    }

    #[test]
    fn flip_gt_operands_target() {
        check_assist_target(flip_binexpr, "fn f() { let res = 1 ><|> 2; }", ">")
    }

}