mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
When adding match arm, don't let the floating comma
This commit is contained in:
parent
668980d865
commit
2fe6e23138
2 changed files with 45 additions and 14 deletions
|
@ -369,21 +369,32 @@ impl ast::MatchArmList {
|
|||
|
||||
#[must_use]
|
||||
pub fn remove_placeholder(&self) -> ast::MatchArmList {
|
||||
let placeholder = self.arms().find(|arm| {
|
||||
if let Some(ast::Pat::PlaceholderPat(_)) = arm.pat() {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
});
|
||||
let placeholder =
|
||||
self.arms().find(|arm| matches!(arm.pat(), Some(ast::Pat::PlaceholderPat(_))));
|
||||
if let Some(placeholder) = placeholder {
|
||||
let s: SyntaxElement = placeholder.syntax().clone().into();
|
||||
let e = s.clone();
|
||||
self.replace_children(s..=e, &mut iter::empty())
|
||||
self.remove_arm(&placeholder)
|
||||
} else {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
fn remove_arm(&self, arm: &ast::MatchArm) -> ast::MatchArmList {
|
||||
let start = arm.syntax().clone();
|
||||
let end = if let Some(comma) = start
|
||||
.siblings_with_tokens(Direction::Next)
|
||||
.skip(1)
|
||||
.skip_while(|it| it.kind().is_trivia())
|
||||
.next()
|
||||
.filter(|it| it.kind() == T![,])
|
||||
{
|
||||
comma
|
||||
} else {
|
||||
start.clone().into()
|
||||
};
|
||||
self.replace_children(start.into()..=end, None)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList {
|
||||
let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue