aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers
diff options
context:
space:
mode:
authorMatthew Hall <[email protected]>2020-03-28 20:44:12 +0000
committerMatthew Hall <[email protected]>2020-03-28 20:58:46 +0000
commitecc2615ba2da0e083a8dbfbf203d1fd7fe0bcaaf (patch)
treee35cea1b482c65b98e1025da47e9670df61e92ed /crates/ra_assists/src/handlers
parent1c2d4135db867efe335a0654d86429bea7bb9caf (diff)
Append new match arms rather than replacing all of them
This means we now retain comments when filling in match arms.
Diffstat (limited to 'crates/ra_assists/src/handlers')
-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..c45981c5c 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 // TODO: Fill this in<|>
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 // TODO: Fill this in
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 // TODO: Fill this in<|>
703 }
704 }
705 "#,
706 r#"
707 enum A {
708 One,
709 Two,
710 }
711 fn foo(a: A) {
712 match <|>a {
713 // TODO: Fill this in
714 A::One => {}
715 A::Two => {}
716 }
717 }
718 "#,
719 );
720 }
658} 721}