Fix assists assuming comma belonging to MATCH_ARM_LIST

This commit is contained in:
Lukas Wirth 2021-07-30 15:51:07 +02:00
parent f04cff102f
commit 82c1e61887
3 changed files with 9 additions and 13 deletions

View file

@ -72,7 +72,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
.join(" | ")
};
let arm = format!("{} => {}", pats, current_expr.syntax().text());
let arm = format!("{} => {},", pats, current_expr.syntax().text());
if let [first, .., last] = &*arms_to_merge {
let start = first.syntax().text_range().start();
@ -118,7 +118,7 @@ enum X { A, B, C }
fn main() {
let x = X::A;
let y = match x {
X::A | X::B => { 1i32 }
X::A | X::B => { 1i32 },
X::C => { 2i32 }
}
}
@ -183,7 +183,7 @@ fn main() {
let x = X::A;
let y = match x {
X::A => { 1i32 },
_ => { 2i32 }
_ => { 2i32 },
}
}
"#,

View file

@ -64,7 +64,7 @@ fn f() {
match () {
() => (),
#[cfg(a)] () => (),
//^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled
//^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled
}
#[cfg(a)] 0 // Trailing expression of block

View file

@ -359,14 +359,10 @@ impl ast::MatchArmList {
let mut elements = Vec::new();
let position = match self.arms().last() {
Some(last_arm) => {
let comma = last_arm
.syntax()
.siblings_with_tokens(Direction::Next)
.find(|it| it.kind() == T![,]);
if needs_comma(&last_arm) && comma.is_none() {
elements.push(make::token(SyntaxKind::COMMA).into());
if needs_comma(&last_arm) {
ted::append_child(last_arm.syntax(), make::token(SyntaxKind::COMMA));
}
Position::after(comma.unwrap_or_else(|| last_arm.syntax().clone().into()))
Position::after(last_arm.syntax().clone())
}
None => match self.l_curly_token() {
Some(it) => Position::after(it),
@ -377,12 +373,12 @@ impl ast::MatchArmList {
elements.push(make::tokens::whitespace(&format!("\n{}", indent)).into());
elements.push(arm.syntax().clone().into());
if needs_comma(&arm) {
elements.push(make::token(SyntaxKind::COMMA).into());
ted::append_child(arm.syntax(), make::token(SyntaxKind::COMMA));
}
ted::insert_all(position, elements);
fn needs_comma(arm: &ast::MatchArm) -> bool {
arm.expr().map_or(false, |e| !e.is_block_like())
arm.expr().map_or(false, |e| !e.is_block_like()) && arm.comma_token().is_none()
}
}
}