aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-31 13:52:20 +0100
committerAleksey Kladov <[email protected]>2020-03-31 13:52:20 +0100
commit2fe6e23138d990a852a59d9680847cb6718565bb (patch)
treeabe30d21ef8c5d541af48da44ac0b4016ef4b56c
parent668980d8657bd358b83f74e2f54f5e228f7393ed (diff)
When adding match arm, don't let the floating comma
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs30
-rw-r--r--crates/ra_syntax/src/ast/edit.rs29
2 files changed, 45 insertions, 14 deletions
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index 41bb97928..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, make, AstNode, NameOwner};
11
12use ast::{MatchArm, Pat};
13 9
14// Assist: fill_match_arms 10// Assist: fill_match_arms
15// 11//
@@ -717,4 +713,28 @@ mod tests {
717 "#, 713 "#,
718 ); 714 );
719 } 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 }
720} 740}
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 3d023f189..b69cae234 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -369,22 +369,33 @@ impl ast::MatchArmList {
369 369
370 #[must_use] 370 #[must_use]
371 pub fn remove_placeholder(&self) -> ast::MatchArmList { 371 pub fn remove_placeholder(&self) -> ast::MatchArmList {
372 let placeholder = self.arms().find(|arm| { 372 let placeholder =
373 if let Some(ast::Pat::PlaceholderPat(_)) = arm.pat() { 373 self.arms().find(|arm| matches!(arm.pat(), Some(ast::Pat::PlaceholderPat(_))));
374 return true;
375 }
376 false
377 });
378 if let Some(placeholder) = placeholder { 374 if let Some(placeholder) = placeholder {
379 let s: SyntaxElement = placeholder.syntax().clone().into(); 375 self.remove_arm(&placeholder)
380 let e = s.clone();
381 self.replace_children(s..=e, &mut iter::empty())
382 } else { 376 } else {
383 self.clone() 377 self.clone()
384 } 378 }
385 } 379 }
386 380
387 #[must_use] 381 #[must_use]
382 fn remove_arm(&self, arm: &ast::MatchArm) -> ast::MatchArmList {
383 let start = arm.syntax().clone();
384 let end = if let Some(comma) = start
385 .siblings_with_tokens(Direction::Next)
386 .skip(1)
387 .skip_while(|it| it.kind().is_trivia())
388 .next()
389 .filter(|it| it.kind() == T![,])
390 {
391 comma
392 } else {
393 start.clone().into()
394 };
395 self.replace_children(start.into()..=end, None)
396 }
397
398 #[must_use]
388 pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList { 399 pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList {
389 let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) { 400 let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) {
390 Some(t) => t, 401 Some(t) => t,