diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-25 07:04:47 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-25 07:04:47 +0000 |
commit | 965363db15a259929286f80f95c603594366bad6 (patch) | |
tree | 7d404561e0b8a9b2f23274c07895e6ee14692910 /crates/ra_syntax/src/ast.rs | |
parent | 219b1573377caafaca1dc071f8513ec8be01f199 (diff) | |
parent | 67055c47da2c94188540847b33921af25652156a (diff) |
Merge #1036
1036: Assist to flip equality (==) and negated equality (!=) operands. r=matklad a=marcogroppo
This PR adds an assist to flip the equality operands.
I hope this is the right way to do this (I'm a newbie...)
Fixes #1023.
Co-authored-by: Marco Groppo <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast.rs')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 78 |
1 files changed, 45 insertions, 33 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index d8c2cb063..226208700 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -521,7 +521,7 @@ pub enum PrefixOp { | |||
521 | } | 521 | } |
522 | 522 | ||
523 | impl PrefixExpr { | 523 | impl PrefixExpr { |
524 | pub fn op(&self) -> Option<PrefixOp> { | 524 | pub fn op_kind(&self) -> Option<PrefixOp> { |
525 | match self.syntax().first_child()?.kind() { | 525 | match self.syntax().first_child()?.kind() { |
526 | STAR => Some(PrefixOp::Deref), | 526 | STAR => Some(PrefixOp::Deref), |
527 | EXCL => Some(PrefixOp::Not), | 527 | EXCL => Some(PrefixOp::Not), |
@@ -529,6 +529,10 @@ impl PrefixExpr { | |||
529 | _ => None, | 529 | _ => None, |
530 | } | 530 | } |
531 | } | 531 | } |
532 | |||
533 | pub fn op(&self) -> Option<&SyntaxNode> { | ||
534 | self.syntax().first_child() | ||
535 | } | ||
532 | } | 536 | } |
533 | 537 | ||
534 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | 538 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
@@ -598,46 +602,54 @@ pub enum BinOp { | |||
598 | } | 602 | } |
599 | 603 | ||
600 | impl BinExpr { | 604 | impl BinExpr { |
601 | pub fn op(&self) -> Option<BinOp> { | 605 | fn op_details(&self) -> Option<(&SyntaxNode, BinOp)> { |
602 | self.syntax() | 606 | self.syntax() |
603 | .children() | 607 | .children() |
604 | .filter_map(|c| match c.kind() { | 608 | .filter_map(|c| match c.kind() { |
605 | PIPEPIPE => Some(BinOp::BooleanOr), | 609 | PIPEPIPE => Some((c, BinOp::BooleanOr)), |
606 | AMPAMP => Some(BinOp::BooleanAnd), | 610 | AMPAMP => Some((c, BinOp::BooleanAnd)), |
607 | EQEQ => Some(BinOp::EqualityTest), | 611 | EQEQ => Some((c, BinOp::EqualityTest)), |
608 | NEQ => Some(BinOp::NegatedEqualityTest), | 612 | NEQ => Some((c, BinOp::NegatedEqualityTest)), |
609 | LTEQ => Some(BinOp::LesserEqualTest), | 613 | LTEQ => Some((c, BinOp::LesserEqualTest)), |
610 | GTEQ => Some(BinOp::GreaterEqualTest), | 614 | GTEQ => Some((c, BinOp::GreaterEqualTest)), |
611 | L_ANGLE => Some(BinOp::LesserTest), | 615 | L_ANGLE => Some((c, BinOp::LesserTest)), |
612 | R_ANGLE => Some(BinOp::GreaterTest), | 616 | R_ANGLE => Some((c, BinOp::GreaterTest)), |
613 | PLUS => Some(BinOp::Addition), | 617 | PLUS => Some((c, BinOp::Addition)), |
614 | STAR => Some(BinOp::Multiplication), | 618 | STAR => Some((c, BinOp::Multiplication)), |
615 | MINUS => Some(BinOp::Subtraction), | 619 | MINUS => Some((c, BinOp::Subtraction)), |
616 | SLASH => Some(BinOp::Division), | 620 | SLASH => Some((c, BinOp::Division)), |
617 | PERCENT => Some(BinOp::Remainder), | 621 | PERCENT => Some((c, BinOp::Remainder)), |
618 | SHL => Some(BinOp::LeftShift), | 622 | SHL => Some((c, BinOp::LeftShift)), |
619 | SHR => Some(BinOp::RightShift), | 623 | SHR => Some((c, BinOp::RightShift)), |
620 | CARET => Some(BinOp::BitwiseXor), | 624 | CARET => Some((c, BinOp::BitwiseXor)), |
621 | PIPE => Some(BinOp::BitwiseOr), | 625 | PIPE => Some((c, BinOp::BitwiseOr)), |
622 | AMP => Some(BinOp::BitwiseAnd), | 626 | AMP => Some((c, BinOp::BitwiseAnd)), |
623 | DOTDOT => Some(BinOp::RangeRightOpen), | 627 | DOTDOT => Some((c, BinOp::RangeRightOpen)), |
624 | DOTDOTEQ => Some(BinOp::RangeRightClosed), | 628 | DOTDOTEQ => Some((c, BinOp::RangeRightClosed)), |
625 | EQ => Some(BinOp::Assignment), | 629 | EQ => Some((c, BinOp::Assignment)), |
626 | PLUSEQ => Some(BinOp::AddAssign), | 630 | PLUSEQ => Some((c, BinOp::AddAssign)), |
627 | SLASHEQ => Some(BinOp::DivAssign), | 631 | SLASHEQ => Some((c, BinOp::DivAssign)), |
628 | STAREQ => Some(BinOp::MulAssign), | 632 | STAREQ => Some((c, BinOp::MulAssign)), |
629 | PERCENTEQ => Some(BinOp::RemAssign), | 633 | PERCENTEQ => Some((c, BinOp::RemAssign)), |
630 | SHREQ => Some(BinOp::ShrAssign), | 634 | SHREQ => Some((c, BinOp::ShrAssign)), |
631 | SHLEQ => Some(BinOp::ShlAssign), | 635 | SHLEQ => Some((c, BinOp::ShlAssign)), |
632 | MINUSEQ => Some(BinOp::SubAssign), | 636 | MINUSEQ => Some((c, BinOp::SubAssign)), |
633 | PIPEEQ => Some(BinOp::BitOrAssign), | 637 | PIPEEQ => Some((c, BinOp::BitOrAssign)), |
634 | AMPEQ => Some(BinOp::BitAndAssign), | 638 | AMPEQ => Some((c, BinOp::BitAndAssign)), |
635 | CARETEQ => Some(BinOp::BitXorAssign), | 639 | CARETEQ => Some((c, BinOp::BitXorAssign)), |
636 | _ => None, | 640 | _ => None, |
637 | }) | 641 | }) |
638 | .next() | 642 | .next() |
639 | } | 643 | } |
640 | 644 | ||
645 | pub fn op_kind(&self) -> Option<BinOp> { | ||
646 | self.op_details().map(|t| t.1) | ||
647 | } | ||
648 | |||
649 | pub fn op(&self) -> Option<&SyntaxNode> { | ||
650 | self.op_details().map(|t| t.0) | ||
651 | } | ||
652 | |||
641 | pub fn lhs(&self) -> Option<&Expr> { | 653 | pub fn lhs(&self) -> Option<&Expr> { |
642 | children(self).nth(0) | 654 | children(self).nth(0) |
643 | } | 655 | } |