minor: Handle match arm commas in make::match_arm

Co-authored-by: Giga Bowser <45986823+Giga-Bowser@users.noreply.github.com>
This commit is contained in:
Lukas Wirth 2025-07-04 11:05:05 +02:00
parent 4183bcdcde
commit 2c01609d6d
3 changed files with 11 additions and 21 deletions

View file

@ -1,6 +1,5 @@
use syntax::{
Direction, SyntaxKind, T,
algo::neighbor,
ast::{self, AstNode, edit::IndentLevel, syntax_factory::SyntaxFactory},
syntax_editor::{Element, Position},
};
@ -88,15 +87,8 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
// body is a block, but we don't bother to check that.
// - Missing after the arm with arms after, if the arm body is a block. In this case
// we don't want to insert a comma at all.
let has_comma_after =
std::iter::successors(match_arm.syntax().last_child_or_token(), |it| {
it.prev_sibling_or_token()
})
.map(|it| it.kind())
.find(|it| !it.is_trivia())
== Some(T![,]);
let has_arms_after = neighbor(&match_arm, Direction::Next).is_some();
if !has_comma_after && !has_arms_after {
let has_comma_after = match_arm.comma_token().is_some();
if !has_comma_after && !match_arm.expr().unwrap().is_block_like() {
insert_after_old_arm.push(make.token(T![,]).into());
}
@ -105,9 +97,6 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
insert_after_old_arm.push(new_match_arm.syntax().clone().into());
if has_comma_after {
insert_after_old_arm.push(make.token(T![,]).into());
}
editor.insert_all(Position::after(match_arm.syntax()), insert_after_old_arm);
editor.add_mappings(make.finish_with_mappings());
edit.add_file_edits(ctx.vfs_file_id(), editor);
@ -256,7 +245,7 @@ fn main() {
let x = X::A;
let y = match x {
X::A => 1i32,
X::B => 1i32
X::B => 1i32,
};
}
"#,
@ -274,7 +263,7 @@ enum X { A, B }
fn main() {
let x = X::A;
match x {
X::A $0| X::B => {},
X::A $0| X::B => {}
}
}
"#,
@ -285,8 +274,8 @@ enum X { A, B }
fn main() {
let x = X::A;
match x {
X::A => {},
X::B => {},
X::A => {}
X::B => {}
}
}
"#,

View file

@ -842,9 +842,10 @@ pub fn ref_pat(pat: ast::Pat) -> ast::RefPat {
}
pub fn match_arm(pat: ast::Pat, guard: Option<ast::MatchGuard>, expr: ast::Expr) -> ast::MatchArm {
let comma_str = if expr.is_block_like() { "" } else { "," };
return match guard {
Some(guard) => from_text(&format!("{pat} {guard} => {expr}")),
None => from_text(&format!("{pat} => {expr}")),
Some(guard) => from_text(&format!("{pat} {guard} => {expr}{comma_str}")),
None => from_text(&format!("{pat} => {expr}{comma_str}")),
};
fn from_text(text: &str) -> ast::MatchArm {
@ -877,7 +878,7 @@ pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::Mat
let arms_str = arms.into_iter().fold(String::new(), |mut acc, arm| {
let needs_comma =
arm.comma_token().is_none() && arm.expr().is_none_or(|it| !it.is_block_like());
let comma = if needs_comma { "," } else { "" };
let comma = if needs_comma && arm.comma_token().is_none() { "," } else { "" };
let arm = arm.syntax();
format_to_acc!(acc, " {arm}{comma}\n")
});

View file

@ -435,7 +435,7 @@ mod tests {
_ => {
let var_name = 2 + 2;
(var_name, true)
}"#]];
},"#]];
expect.assert_eq(&edit.new_root.to_string());
assert_eq!(edit.find_annotation(placeholder_snippet).len(), 2);