diff --git a/crates/ra_assists/src/flip_binexpr.rs b/crates/ra_assists/src/flip_binexpr.rs new file mode 100644 index 00000000000..ec377642e02 --- /dev/null +++ b/crates/ra_assists/src/flip_binexpr.rs @@ -0,0 +1,141 @@ +use hir::db::HirDatabase; +use ra_syntax::ast::{AstNode, BinExpr, BinOp}; + +use crate::{AssistCtx, Assist, AssistId}; + +/// Flip binary expression assist. +pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option { + let expr = ctx.node_at_offset::()?; + let lhs = expr.lhs()?.syntax(); + let rhs = expr.rhs()?.syntax(); + let op_range = expr.op()?.range(); + // The assist should be applied only if the cursor is on the operator + let cursor_in_range = ctx.frange.range.is_subrange(&op_range); + if !cursor_in_range { + return None; + } + let action: FlipAction = expr.op_kind()?.into(); + // The assist should not be applied for certain operators + if let FlipAction::DontFlip = action { + return None; + } + + ctx.add_action(AssistId("flip_binexpr"), "flip binary expression", |edit| { + edit.target(op_range); + if let FlipAction::FlipAndReplaceOp(new_op) = action { + edit.replace(op_range, new_op); + } + edit.replace(lhs.range(), rhs.text()); + edit.replace(rhs.range(), lhs.text()); + }); + + ctx.build() +} + +enum FlipAction { + // Flip the expression + Flip, + // Flip the expression and replace the operator with this string + FlipAndReplaceOp(&'static str), + // Do not flip the expression + DontFlip, +} + +impl From for FlipAction { + fn from(op_kind: BinOp) -> Self { + match op_kind { + BinOp::Assignment => FlipAction::DontFlip, + BinOp::AddAssign => FlipAction::DontFlip, + BinOp::DivAssign => FlipAction::DontFlip, + BinOp::MulAssign => FlipAction::DontFlip, + BinOp::RemAssign => FlipAction::DontFlip, + BinOp::ShrAssign => FlipAction::DontFlip, + BinOp::ShlAssign => FlipAction::DontFlip, + BinOp::SubAssign => FlipAction::DontFlip, + BinOp::BitOrAssign => FlipAction::DontFlip, + BinOp::BitAndAssign => FlipAction::DontFlip, + BinOp::BitXorAssign => FlipAction::DontFlip, + BinOp::GreaterTest => FlipAction::FlipAndReplaceOp("<"), + BinOp::GreaterEqualTest => FlipAction::FlipAndReplaceOp("<="), + BinOp::LesserTest => FlipAction::FlipAndReplaceOp(">"), + BinOp::LesserEqualTest => FlipAction::FlipAndReplaceOp(">="), + _ => FlipAction::Flip, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use crate::helpers::{ check_assist, check_assist_target, check_assist_not_applicable }; + + #[test] + fn flip_binexpr_target_is_the_op() { + check_assist_target(flip_binexpr, "fn f() { let res = 1 ==<|> 2; }", "==") + } + + #[test] + fn flip_binexpr_not_applicable_for_assignment() { + check_assist_not_applicable(flip_binexpr, "fn f() { let mut _x = 1; _x +=<|> 2 }") + } + + #[test] + fn flip_binexpr_works_for_eq() { + check_assist( + flip_binexpr, + "fn f() { let res = 1 ==<|> 2; }", + "fn f() { let res = 2 ==<|> 1; }", + ) + } + + #[test] + fn flip_binexpr_works_for_gt() { + check_assist( + flip_binexpr, + "fn f() { let res = 1 ><|> 2; }", + "fn f() { let res = 2 <<|> 1; }", + ) + } + + #[test] + fn flip_binexpr_works_for_lteq() { + check_assist( + flip_binexpr, + "fn f() { let res = 1 <=<|> 2; }", + "fn f() { let res = 2 >=<|> 1; }", + ) + } + + #[test] + fn flip_binexpr_works_for_complex_expr() { + check_assist( + flip_binexpr, + "fn f() { let res = (1 + 1) ==<|> (2 + 2); }", + "fn f() { let res = (2 + 2) ==<|> (1 + 1); }", + ) + } + + #[test] + fn flip_binexpr_works_inside_match() { + check_assist( + flip_binexpr, + r#" + fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { + match other.downcast_ref::() { + None => false, + Some(it) => it ==<|> self, + } + } + "#, + r#" + fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { + match other.downcast_ref::() { + None => false, + Some(it) => self ==<|> it, + } + } + "#, + ) + } +} diff --git a/crates/ra_assists/src/flip_eq_operands.rs b/crates/ra_assists/src/flip_eq_operands.rs deleted file mode 100644 index df0bb689da5..00000000000 --- a/crates/ra_assists/src/flip_eq_operands.rs +++ /dev/null @@ -1,86 +0,0 @@ -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) -> Option { - let expr = ctx.node_at_offset::()?; - let lhs = expr.lhs()?.syntax(); - let rhs = expr.rhs()?.syntax(); - let op_range = expr.op()?.range(); - let cursor_in_range = ctx.frange.range.is_subrange(&op_range); - let allowed_ops = [BinOp::EqualityTest, BinOp::NegatedEqualityTest]; - let expr_op = expr.op_kind()?; - if !cursor_in_range || !allowed_ops.iter().any(|o| *o == expr_op) { - return None; - } - ctx.add_action(AssistId("flip_eq_operands"), "flip equality operands", |edit| { - edit.target(op_range); - 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_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::() { - None => false, - Some(it) => it ==<|> self, - } - } - "#, - r#" - fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { - match other.downcast_ref::() { - None => false, - Some(it) => self ==<|> it, - } - } - "#, - ) - } - - #[test] - fn flip_eq_operands_target() { - check_assist_target(flip_eq_operands, "fn f() { let res = 1 ==<|> 2; }", "==") - } -} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 2e47b5215cf..c1514f8e523 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -88,7 +88,7 @@ where mod add_derive; mod add_impl; mod flip_comma; -mod flip_eq_operands; +mod flip_binexpr; mod change_visibility; mod fill_match_arms; mod fill_struct_fields; @@ -108,7 +108,7 @@ fn all_assists() -> &'static [fn(AssistCtx) -> Option