aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs100
1 files changed, 91 insertions, 9 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..8d1af9933 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -1,15 +1,11 @@
1//! FIXME: write short doc here
2
3use std::iter; 1use std::iter;
4 2
5use hir::{Adt, HasSource, ModuleDef, Semantics}; 3use hir::{Adt, HasSource, ModuleDef, Semantics};
6use itertools::Itertools; 4use itertools::Itertools;
7use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
8 7
9use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
10use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner};
11
12use ast::{MatchArm, Pat};
13 9
14// Assist: fill_match_arms 10// Assist: fill_match_arms
15// 11//
@@ -97,10 +93,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
97 } 93 }
98 94
99 ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| { 95 ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| {
100 arms.extend(missing_arms); 96 let new_arm_list = match_arm_list.remove_placeholder().append_arms(missing_arms);
101
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 97
105 edit.target(match_expr.syntax().text_range()); 98 edit.target(match_expr.syntax().text_range());
106 edit.set_cursor(expr.syntax().text_range().start()); 99 edit.set_cursor(expr.syntax().text_range().start());
@@ -655,4 +648,93 @@ mod tests {
655 "#, 648 "#,
656 ); 649 );
657 } 650 }
651
652 #[test]
653 fn fill_match_arms_preserves_comments() {
654 check_assist(
655 fill_match_arms,
656 r#"
657 enum A {
658 One,
659 Two,
660 }
661 fn foo(a: A) {
662 match a {
663 // foo bar baz<|>
664 A::One => {}
665 // This is where the rest should be
666 }
667 }
668 "#,
669 r#"
670 enum A {
671 One,
672 Two,
673 }
674 fn foo(a: A) {
675 match <|>a {
676 // foo bar baz
677 A::One => {}
678 // This is where the rest should be
679 A::Two => {}
680 }
681 }
682 "#,
683 );
684 }
685
686 #[test]
687 fn fill_match_arms_preserves_comments_empty() {
688 check_assist(
689 fill_match_arms,
690 r#"
691 enum A {
692 One,
693 Two,
694 }
695 fn foo(a: A) {
696 match a {
697 // foo bar baz<|>
698 }
699 }
700 "#,
701 r#"
702 enum A {
703 One,
704 Two,
705 }
706 fn foo(a: A) {
707 match <|>a {
708 // foo bar baz
709 A::One => {}
710 A::Two => {}
711 }
712 }
713 "#,
714 );
715 }
716
717 #[test]
718 fn fill_match_arms_placeholder() {
719 check_assist(
720 fill_match_arms,
721 r#"
722 enum A { One, Two, }
723 fn foo(a: A) {
724 match a<|> {
725 _ => (),
726 }
727 }
728 "#,
729 r#"
730 enum A { One, Two, }
731 fn foo(a: A) {
732 match <|>a {
733 A::One => {}
734 A::Two => {}
735 }
736 }
737 "#,
738 );
739 }
658} 740}