aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-30 11:19:02 +0100
committerGitHub <[email protected]>2020-03-30 11:19:02 +0100
commitd2ea3f25b53deac5785485ab5dfe5e6b3b893bf5 (patch)
treeb979afc6883c973d4b95765a292e6e5c8b819e96 /crates/ra_assists/src
parent3901198e87df9c85b4a2e996240ca05c6950900f (diff)
parentddb9cc47d17204e5d52529ca04a4093f8ed8ec08 (diff)
Merge #3761
3761: Append new match arms rather than replacing all of them r=matklad a=mattyhall This means we now retain comments when filling in match arms. This fixes #3687. This is my first contribution so apologies if it needs a rethink! I think in particular the way I find the position to append to and remove_if_only_whitespace are a little hairy. Co-authored-by: Matthew Hall <[email protected]>
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs73
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;
7use ra_ide_db::RootDatabase; 7use ra_ide_db::RootDatabase;
8 8
9use crate::{Assist, AssistCtx, AssistId}; 9use crate::{Assist, AssistCtx, AssistId};
10use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner}; 10use ra_syntax::ast::{self, make, AstNode, NameOwner};
11 11
12use ast::{MatchArm, Pat}; 12use 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}