diff options
-rw-r--r-- | .travis.yml | 47 | ||||
-rw-r--r-- | crates/ra_assists/src/flip_eq_operands.rs | 86 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/expr.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 78 |
5 files changed, 155 insertions, 62 deletions
diff --git a/.travis.yml b/.travis.yml index ebff7d8b0..4dffdea34 100644 --- a/.travis.yml +++ b/.travis.yml | |||
@@ -4,43 +4,36 @@ before_cache: | |||
4 | - rm -fr ./target/debug/{deps,.fingerprint}/{*ra_*,*test*,*tools*,*gen_lsp*,*thread_worker*} | 4 | - rm -fr ./target/debug/{deps,.fingerprint}/{*ra_*,*test*,*tools*,*gen_lsp*,*thread_worker*} |
5 | - rm -f ./target/.rustc_info.json | 5 | - rm -f ./target/.rustc_info.json |
6 | 6 | ||
7 | build: &rust_build | ||
8 | language: rust | ||
9 | rust: stable | ||
10 | script: | ||
11 | - rustup component add rustfmt | ||
12 | - rustup component add rust-src | ||
13 | - cargo test --no-run # let's measure compile time separately | ||
14 | - cargo test | ||
15 | - cargo doc --all --no-deps | ||
16 | env: | ||
17 | - RUSTFLAGS="-D warnings", CARGO_INCREMENTAL=0 | ||
18 | |||
19 | matrix: | 7 | matrix: |
20 | include: | 8 | include: |
21 | - os: linux | 9 | - os: linux |
22 | before_script: | 10 | language: rust |
11 | rust: stable | ||
12 | script: | ||
13 | - rustup component add rustfmt | ||
14 | - rustup component add rust-src | ||
15 | - cargo test --no-run # let's measure compile time separately | ||
16 | - cargo test | ||
17 | env: | ||
18 | - RUSTFLAGS="-D warnings", CARGO_INCREMENTAL=0 | ||
19 | |||
20 | - os: linux | ||
21 | if: branch = master AND type = push | ||
22 | before_stript: | ||
23 | - DEPLOY_DOCS=1 | 23 | - DEPLOY_DOCS=1 |
24 | <<: *rust_build | 24 | language: rust |
25 | rust: stable | ||
26 | script: | ||
27 | - cargo doc --all --no-deps | ||
28 | env: | ||
29 | - RUSTFLAGS="-D warnings", CARGO_INCREMENTAL=0 | ||
30 | |||
25 | - language: node_js | 31 | - language: node_js |
26 | node_js: node | 32 | node_js: node |
27 | before_script: false | 33 | before_script: false |
28 | script: | 34 | script: |
29 | - cd editors/code && npm ci && npm run travis | 35 | - cd editors/code && npm ci && npm run travis |
30 | 36 | ||
31 | - os: windows | ||
32 | if: branch = master AND type = push | ||
33 | before_script: | ||
34 | - dos2unix ./crates/ra_syntax/tests/data/parser/**/*.txt | ||
35 | - dos2unix ./crates/ra_syntax/tests/data/parser/**/*.rs | ||
36 | <<: *rust_build | ||
37 | |||
38 | allow_failures: | ||
39 | # Because Travis-Windows-Rust can be flaky | ||
40 | # We still support Windows and want the tests to be succeeding, | ||
41 | # but there are too many spurious failures | ||
42 | - os: windows | ||
43 | |||
44 | branches: | 37 | branches: |
45 | only: | 38 | only: |
46 | - staging | 39 | - staging |
diff --git a/crates/ra_assists/src/flip_eq_operands.rs b/crates/ra_assists/src/flip_eq_operands.rs new file mode 100644 index 000000000..df0bb689d --- /dev/null +++ b/crates/ra_assists/src/flip_eq_operands.rs | |||
@@ -0,0 +1,86 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::ast::{AstNode, BinExpr, BinOp}; | ||
3 | |||
4 | use crate::{AssistCtx, Assist, AssistId}; | ||
5 | |||
6 | pub(crate) fn flip_eq_operands(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | ||
7 | let expr = ctx.node_at_offset::<BinExpr>()?; | ||
8 | let lhs = expr.lhs()?.syntax(); | ||
9 | let rhs = expr.rhs()?.syntax(); | ||
10 | let op_range = expr.op()?.range(); | ||
11 | let cursor_in_range = ctx.frange.range.is_subrange(&op_range); | ||
12 | let allowed_ops = [BinOp::EqualityTest, BinOp::NegatedEqualityTest]; | ||
13 | let expr_op = expr.op_kind()?; | ||
14 | if !cursor_in_range || !allowed_ops.iter().any(|o| *o == expr_op) { | ||
15 | return None; | ||
16 | } | ||
17 | ctx.add_action(AssistId("flip_eq_operands"), "flip equality operands", |edit| { | ||
18 | edit.target(op_range); | ||
19 | edit.replace(lhs.range(), rhs.text()); | ||
20 | edit.replace(rhs.range(), lhs.text()); | ||
21 | }); | ||
22 | |||
23 | ctx.build() | ||
24 | } | ||
25 | |||
26 | #[cfg(test)] | ||
27 | mod tests { | ||
28 | use super::*; | ||
29 | |||
30 | use crate::helpers::{check_assist, check_assist_target}; | ||
31 | |||
32 | #[test] | ||
33 | fn flip_eq_operands_for_simple_stmt() { | ||
34 | check_assist( | ||
35 | flip_eq_operands, | ||
36 | "fn f() { let res = 1 ==<|> 2; }", | ||
37 | "fn f() { let res = 2 ==<|> 1; }", | ||
38 | ) | ||
39 | } | ||
40 | |||
41 | #[test] | ||
42 | fn flip_neq_operands_for_simple_stmt() { | ||
43 | check_assist( | ||
44 | flip_eq_operands, | ||
45 | "fn f() { let res = 1 !=<|> 2; }", | ||
46 | "fn f() { let res = 2 !=<|> 1; }", | ||
47 | ) | ||
48 | } | ||
49 | |||
50 | #[test] | ||
51 | fn flip_eq_operands_for_complex_stmt() { | ||
52 | check_assist( | ||
53 | flip_eq_operands, | ||
54 | "fn f() { let res = (1 + 1) ==<|> (2 + 2); }", | ||
55 | "fn f() { let res = (2 + 2) ==<|> (1 + 1); }", | ||
56 | ) | ||
57 | } | ||
58 | |||
59 | #[test] | ||
60 | fn flip_eq_operands_in_match_expr() { | ||
61 | check_assist( | ||
62 | flip_eq_operands, | ||
63 | r#" | ||
64 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { | ||
65 | match other.downcast_ref::<Self>() { | ||
66 | None => false, | ||
67 | Some(it) => it ==<|> self, | ||
68 | } | ||
69 | } | ||
70 | "#, | ||
71 | r#" | ||
72 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { | ||
73 | match other.downcast_ref::<Self>() { | ||
74 | None => false, | ||
75 | Some(it) => self ==<|> it, | ||
76 | } | ||
77 | } | ||
78 | "#, | ||
79 | ) | ||
80 | } | ||
81 | |||
82 | #[test] | ||
83 | fn flip_eq_operands_target() { | ||
84 | check_assist_target(flip_eq_operands, "fn f() { let res = 1 ==<|> 2; }", "==") | ||
85 | } | ||
86 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index fc36e8cc9..8900fbc4b 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -88,6 +88,7 @@ where | |||
88 | mod add_derive; | 88 | mod add_derive; |
89 | mod add_impl; | 89 | mod add_impl; |
90 | mod flip_comma; | 90 | mod flip_comma; |
91 | mod flip_eq_operands; | ||
91 | mod change_visibility; | 92 | mod change_visibility; |
92 | mod fill_match_arms; | 93 | mod fill_match_arms; |
93 | mod fill_struct_fields; | 94 | mod fill_struct_fields; |
@@ -106,6 +107,7 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis | |||
106 | fill_match_arms::fill_match_arms, | 107 | fill_match_arms::fill_match_arms, |
107 | fill_struct_fields::fill_struct_fields, | 108 | fill_struct_fields::fill_struct_fields, |
108 | flip_comma::flip_comma, | 109 | flip_comma::flip_comma, |
110 | flip_eq_operands::flip_eq_operands, | ||
109 | introduce_variable::introduce_variable, | 111 | introduce_variable::introduce_variable, |
110 | replace_if_let_with_match::replace_if_let_with_match, | 112 | replace_if_let_with_match::replace_if_let_with_match, |
111 | split_import::split_import, | 113 | split_import::split_import, |
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 703d99d9b..c37fd0454 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -680,7 +680,7 @@ impl ExprCollector { | |||
680 | } | 680 | } |
681 | ast::ExprKind::PrefixExpr(e) => { | 681 | ast::ExprKind::PrefixExpr(e) => { |
682 | let expr = self.collect_expr_opt(e.expr()); | 682 | let expr = self.collect_expr_opt(e.expr()); |
683 | if let Some(op) = e.op() { | 683 | if let Some(op) = e.op_kind() { |
684 | self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) | 684 | self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) |
685 | } else { | 685 | } else { |
686 | self.alloc_expr(Expr::Missing, syntax_ptr) | 686 | self.alloc_expr(Expr::Missing, syntax_ptr) |
@@ -703,7 +703,7 @@ impl ExprCollector { | |||
703 | ast::ExprKind::BinExpr(e) => { | 703 | ast::ExprKind::BinExpr(e) => { |
704 | let lhs = self.collect_expr_opt(e.lhs()); | 704 | let lhs = self.collect_expr_opt(e.lhs()); |
705 | let rhs = self.collect_expr_opt(e.rhs()); | 705 | let rhs = self.collect_expr_opt(e.rhs()); |
706 | let op = e.op(); | 706 | let op = e.op_kind(); |
707 | self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) | 707 | self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) |
708 | } | 708 | } |
709 | ast::ExprKind::TupleExpr(e) => { | 709 | ast::ExprKind::TupleExpr(e) => { |
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 | } |