]> git.lizzy.rs Git - rust.git/commitdiff
Assist to flip equality (==) and negative equality (!=) operands.
authorMarco Groppo <marco.groppo@gmail.com>
Sun, 24 Mar 2019 13:31:44 +0000 (14:31 +0100)
committerMarco Groppo <marco.groppo@gmail.com>
Sun, 24 Mar 2019 13:42:11 +0000 (14:42 +0100)
crates/ra_assists/src/flip_eq_operands.rs [new file with mode: 0644]
crates/ra_assists/src/lib.rs

diff --git a/crates/ra_assists/src/flip_eq_operands.rs b/crates/ra_assists/src/flip_eq_operands.rs
new file mode 100644 (file)
index 0000000..22494a7
--- /dev/null
@@ -0,0 +1,87 @@
+use hir::db::HirDatabase;
+use ra_syntax::{
+    ast::{AstNode, BinExpr, BinOp}
+};
+
+use crate::{AssistCtx, Assist, AssistId};
+
+pub(crate) fn flip_eq_operands(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
+    let expr = ctx.node_at_offset::<BinExpr>()?;
+    let allowed_ops = [BinOp::EqualityTest, BinOp::NegatedEqualityTest];
+    let expr_op = expr.op()?;
+    if ! allowed_ops.iter().any(|o| *o == expr_op) {
+        return None;
+    }
+    let node = expr.syntax();
+    let prev = node.first_child()?;
+    let next = node.last_child()?;
+    ctx.add_action(AssistId("flip_eq_operands"), "flip equality operands", |edit| {
+        edit.target(node.range());
+        edit.replace(prev.range(), next.text());
+        edit.replace(next.range(), prev.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_eq_operands,
+            "fn f() { let res = 1 ==<|> 2; }",
+            "fn f() { let res = 2 ==<|> 1; }",
+        )
+    }
+
+    #[test]
+    fn flip_neq_operands_for_simple_stmt() {
+        check_assist(
+            flip_eq_operands,
+            "fn f() { let res = 1 !=<|> 2; }",
+            "fn f() { let res = 2 !=<|> 1; }",
+        )
+    }
+
+    #[test]
+    fn flip_eq_operands_for_complex_stmt() {
+        check_assist(
+            flip_eq_operands,
+            "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_eq_operands,
+            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_eq_operands, "fn f() { let res = 1 ==<|> 2; }", "1 == 2")
+    }
+}
index 871b37f58cac1a24b9eca3bebc2cbbaaceb6e1a1..eb5248ae486f5325b599f80e4bd745e25202f3be 100644 (file)
@@ -88,6 +88,7 @@ pub fn assists<H>(db: &H, range: FileRange) -> Vec<(AssistLabel, AssistAction)>
 mod add_derive;
 mod add_impl;
 mod flip_comma;
+mod flip_eq_operands;
 mod change_visibility;
 mod fill_match_arms;
 mod fill_struct_fields;
@@ -106,6 +107,7 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
         fill_match_arms::fill_match_arms,
         fill_struct_fields::fill_struct_fields,
         flip_comma::flip_comma,
+        flip_eq_operands::flip_eq_operands,
         introduce_variable::introduce_variable,
         replace_if_let_with_match::replace_if_let_with_match,
         split_import::split_import,