Skip to content

Commit 2cdeb73

Browse files
bors[bot]matklad
andauthored
Merge #3786
3786: When adding match arm, don't let the floating comma r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents d63bb85 + 2fe6e23 commit 2cdeb73

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

crates/ra_assists/src/handlers/fill_match_arms.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
//! FIXME: write short doc here
2-
31
use std::iter;
42

53
use hir::{Adt, HasSource, ModuleDef, Semantics};
64
use itertools::Itertools;
75
use ra_ide_db::RootDatabase;
6+
use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
87

98
use crate::{Assist, AssistCtx, AssistId};
10-
use ra_syntax::ast::{self, make, AstNode, NameOwner};
11-
12-
use ast::{MatchArm, Pat};
139

1410
// Assist: fill_match_arms
1511
//
@@ -717,4 +713,28 @@ mod tests {
717713
"#,
718714
);
719715
}
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+
}
720740
}

crates/ra_syntax/src/ast/edit.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,21 +369,32 @@ impl ast::MatchArmList {
369369

370370
#[must_use]
371371
pub fn remove_placeholder(&self) -> ast::MatchArmList {
372-
let placeholder = self.arms().find(|arm| {
373-
if let Some(ast::Pat::PlaceholderPat(_)) = arm.pat() {
374-
return true;
375-
}
376-
false
377-
});
372+
let placeholder =
373+
self.arms().find(|arm| matches!(arm.pat(), Some(ast::Pat::PlaceholderPat(_))));
378374
if let Some(placeholder) = placeholder {
379-
let s: SyntaxElement = placeholder.syntax().clone().into();
380-
let e = s.clone();
381-
self.replace_children(s..=e, &mut iter::empty())
375+
self.remove_arm(&placeholder)
382376
} else {
383377
self.clone()
384378
}
385379
}
386380

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+
387398
#[must_use]
388399
pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList {
389400
let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) {

0 commit comments

Comments
 (0)