From 5afb22e2b383a2894297f3508cd3e954da96da1c Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Sat, 4 Jan 2020 15:54:31 -0500 Subject: [PATCH] BinOp helper to detect assignment --- crates/ra_assists/src/assists/flip_binexpr.rs | 12 +----------- crates/ra_syntax/src/ast/expr_extensions.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/crates/ra_assists/src/assists/flip_binexpr.rs b/crates/ra_assists/src/assists/flip_binexpr.rs index 386045eb0a..2d91b2a7e5 100644 --- a/crates/ra_assists/src/assists/flip_binexpr.rs +++ b/crates/ra_assists/src/assists/flip_binexpr.rs @@ -56,17 +56,7 @@ enum FlipAction { 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, + kind if kind.is_assignment() => FlipAction::DontFlip, BinOp::GreaterTest => FlipAction::FlipAndReplaceOp("<"), BinOp::GreaterEqualTest => FlipAction::FlipAndReplaceOp("<="), BinOp::LesserTest => FlipAction::FlipAndReplaceOp(">"), diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 2fd0398378..23b6aa9014 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -126,6 +126,24 @@ pub enum BinOp { BitXorAssign, } +impl BinOp { + pub fn is_assignment(&self) -> bool { + match *self { + BinOp::Assignment + | BinOp::AddAssign + | BinOp::DivAssign + | BinOp::MulAssign + | BinOp::RemAssign + | BinOp::ShrAssign + | BinOp::ShlAssign + | BinOp::SubAssign + | BinOp::BitOrAssign + | BinOp::BitAndAssign + | BinOp::BitXorAssign => true, + _ => false, + } + } +} impl ast::BinExpr { pub fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| {