Assist to flip (some) binary expressions.

This assist can flip the following operators: ==, !=, >, >=, <, <=.
This commit is contained in:
Marco Groppo 2019-03-25 23:53:57 +01:00
parent d88a96bd05
commit 12b5d4f795
3 changed files with 151 additions and 88 deletions

View File

@ -0,0 +1,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; }", ">")
}
}

View File

@ -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<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();
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::<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; }", "==")
}
}

View File

@ -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<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,
flip_binexpr::flip_binexpr,
introduce_variable::introduce_variable,
replace_if_let_with_match::replace_if_let_with_match,
split_import::split_import,