rust/crates/ide_assists/src/handlers/flip_binexpr.rs

135 lines
3.8 KiB
Rust
Raw Normal View History

2020-08-12 11:26:51 -05:00
use syntax::ast::{AstNode, BinExpr, BinOp};
2020-06-28 17:36:05 -05:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-10-26 11:08:13 -05:00
// Assist: flip_binexpr
//
// Flips operands of a binary expression.
//
// ```
// fn main() {
2021-01-06 14:15:48 -06:00
// let _ = 90 +$0 2;
2019-10-26 11:08:13 -05:00
// }
// ```
// ->
// ```
// fn main() {
// let _ = 2 + 90;
// }
// ```
pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let expr = ctx.find_node_at_offset::<BinExpr>()?;
2019-07-19 03:24:41 -05:00
let lhs = expr.lhs()?.syntax().clone();
let rhs = expr.rhs()?.syntax().clone();
2019-07-20 04:58:27 -05:00
let op_range = expr.op_token()?.text_range();
// The assist should be applied only if the cursor is on the operator
2020-04-24 16:40:41 -05:00
let cursor_in_range = op_range.contains_range(ctx.frange.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;
}
2020-06-28 17:36:05 -05:00
acc.add(
2020-07-02 16:48:35 -05:00
AssistId("flip_binexpr", AssistKind::RefactorRewrite),
2020-06-28 17:36:05 -05:00
"Flip binary expression",
op_range,
|edit| {
if let FlipAction::FlipAndReplaceOp(new_op) = action {
edit.replace(op_range, new_op);
}
edit.replace(lhs.text_range(), rhs.text());
edit.replace(rhs.text_range(), lhs.text());
},
)
}
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<BinOp> for FlipAction {
fn from(op_kind: BinOp) -> Self {
match op_kind {
2020-01-04 14:54:31 -06:00
kind if kind.is_assignment() => 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::*;
2020-05-06 03:16:55 -05:00
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
#[test]
fn flip_binexpr_target_is_the_op() {
2021-01-06 14:15:48 -06:00
check_assist_target(flip_binexpr, "fn f() { let res = 1 ==$0 2; }", "==")
}
#[test]
fn flip_binexpr_not_applicable_for_assignment() {
2021-01-06 14:15:48 -06:00
check_assist_not_applicable(flip_binexpr, "fn f() { let mut _x = 1; _x +=$0 2 }")
}
#[test]
fn flip_binexpr_works_for_eq() {
2021-01-06 14:15:48 -06:00
check_assist(flip_binexpr, "fn f() { let res = 1 ==$0 2; }", "fn f() { let res = 2 == 1; }")
}
#[test]
fn flip_binexpr_works_for_gt() {
2021-01-06 14:15:48 -06:00
check_assist(flip_binexpr, "fn f() { let res = 1 >$0 2; }", "fn f() { let res = 2 < 1; }")
}
#[test]
fn flip_binexpr_works_for_lteq() {
2021-01-06 14:15:48 -06:00
check_assist(flip_binexpr, "fn f() { let res = 1 <=$0 2; }", "fn f() { let res = 2 >= 1; }")
}
#[test]
fn flip_binexpr_works_for_complex_expr() {
check_assist(
flip_binexpr,
2021-01-06 14:15:48 -06:00
"fn f() { let res = (1 + 1) ==$0 (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::<Self>() {
None => false,
2021-01-06 14:15:48 -06:00
Some(it) => it ==$0 self,
}
}
"#,
r#"
fn dyn_eq(&self, other: &dyn Diagnostic) -> bool {
match other.downcast_ref::<Self>() {
None => false,
Some(it) => self == it,
}
}
"#,
)
}
}