diff options
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r-- | crates/ra_assists/src/handlers/fill_match_arms.rs | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index add82e5b1..5f279d25a 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs | |||
@@ -7,7 +7,7 @@ use itertools::Itertools; | |||
7 | use ra_ide_db::RootDatabase; | 7 | use ra_ide_db::RootDatabase; |
8 | 8 | ||
9 | use crate::{Assist, AssistCtx, AssistId}; | 9 | use crate::{Assist, AssistCtx, AssistId}; |
10 | use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner}; | 10 | use ra_syntax::ast::{self, make, AstNode, NameOwner}; |
11 | 11 | ||
12 | use ast::{MatchArm, Pat}; | 12 | use ast::{MatchArm, Pat}; |
13 | 13 | ||
@@ -97,10 +97,8 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> { | |||
97 | } | 97 | } |
98 | 98 | ||
99 | ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| { | 99 | ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| { |
100 | arms.extend(missing_arms); | 100 | let new_arm_list = |
101 | 101 | match_arm_list.remove_placeholder().append_arms(missing_arms.into_iter()); | |
102 | let indent_level = IndentLevel::from_node(match_arm_list.syntax()); | ||
103 | let new_arm_list = indent_level.increase_indent(make::match_arm_list(arms)); | ||
104 | 102 | ||
105 | edit.target(match_expr.syntax().text_range()); | 103 | edit.target(match_expr.syntax().text_range()); |
106 | edit.set_cursor(expr.syntax().text_range().start()); | 104 | edit.set_cursor(expr.syntax().text_range().start()); |
@@ -655,4 +653,69 @@ mod tests { | |||
655 | "#, | 653 | "#, |
656 | ); | 654 | ); |
657 | } | 655 | } |
656 | |||
657 | #[test] | ||
658 | fn fill_match_arms_preserves_comments() { | ||
659 | check_assist( | ||
660 | fill_match_arms, | ||
661 | r#" | ||
662 | enum A { | ||
663 | One, | ||
664 | Two, | ||
665 | } | ||
666 | fn foo(a: A) { | ||
667 | match a { | ||
668 | // foo bar baz<|> | ||
669 | A::One => {} | ||
670 | // This is where the rest should be | ||
671 | } | ||
672 | } | ||
673 | "#, | ||
674 | r#" | ||
675 | enum A { | ||
676 | One, | ||
677 | Two, | ||
678 | } | ||
679 | fn foo(a: A) { | ||
680 | match <|>a { | ||
681 | // foo bar baz | ||
682 | A::One => {} | ||
683 | // This is where the rest should be | ||
684 | A::Two => {} | ||
685 | } | ||
686 | } | ||
687 | "#, | ||
688 | ); | ||
689 | } | ||
690 | |||
691 | #[test] | ||
692 | fn fill_match_arms_preserves_comments_empty() { | ||
693 | check_assist( | ||
694 | fill_match_arms, | ||
695 | r#" | ||
696 | enum A { | ||
697 | One, | ||
698 | Two, | ||
699 | } | ||
700 | fn foo(a: A) { | ||
701 | match a { | ||
702 | // foo bar baz<|> | ||
703 | } | ||
704 | } | ||
705 | "#, | ||
706 | r#" | ||
707 | enum A { | ||
708 | One, | ||
709 | Two, | ||
710 | } | ||
711 | fn foo(a: A) { | ||
712 | match <|>a { | ||
713 | // foo bar baz | ||
714 | A::One => {} | ||
715 | A::Two => {} | ||
716 | } | ||
717 | } | ||
718 | "#, | ||
719 | ); | ||
720 | } | ||
658 | } | 721 | } |