Merge pull request #20168 from Veykril/push-wsozylrmsyns

minor: Handle match arm commas in `make::match_arm`
This commit is contained in:
Lukas Wirth 2025-07-04 09:19:22 +00:00 committed by GitHub
commit 37a7734af6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 21 deletions

View File

@ -1,6 +1,5 @@
use syntax::{ use syntax::{
Direction, SyntaxKind, T, Direction, SyntaxKind, T,
algo::neighbor,
ast::{self, AstNode, edit::IndentLevel, syntax_factory::SyntaxFactory}, ast::{self, AstNode, edit::IndentLevel, syntax_factory::SyntaxFactory},
syntax_editor::{Element, Position}, 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. // 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 // - 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. // we don't want to insert a comma at all.
let has_comma_after = let has_comma_after = match_arm.comma_token().is_some();
std::iter::successors(match_arm.syntax().last_child_or_token(), |it| { if !has_comma_after && !match_arm.expr().unwrap().is_block_like() {
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 {
insert_after_old_arm.push(make.token(T![,]).into()); 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()); 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.insert_all(Position::after(match_arm.syntax()), insert_after_old_arm);
editor.add_mappings(make.finish_with_mappings()); editor.add_mappings(make.finish_with_mappings());
edit.add_file_edits(ctx.vfs_file_id(), editor); edit.add_file_edits(ctx.vfs_file_id(), editor);
@ -256,7 +245,7 @@ fn main() {
let x = X::A; let x = X::A;
let y = match x { let y = match x {
X::A => 1i32, X::A => 1i32,
X::B => 1i32 X::B => 1i32,
}; };
} }
"#, "#,
@ -274,7 +263,7 @@ enum X { A, B }
fn main() { fn main() {
let x = X::A; let x = X::A;
match x { match x {
X::A $0| X::B => {}, X::A $0| X::B => {}
} }
} }
"#, "#,
@ -285,8 +274,8 @@ enum X { A, B }
fn main() { fn main() {
let x = X::A; let x = X::A;
match x { match x {
X::A => {}, X::A => {}
X::B => {}, 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 { 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 { return match guard {
Some(guard) => from_text(&format!("{pat} {guard} => {expr}")), Some(guard) => from_text(&format!("{pat} {guard} => {expr}{comma_str}")),
None => from_text(&format!("{pat} => {expr}")), None => from_text(&format!("{pat} => {expr}{comma_str}")),
}; };
fn from_text(text: &str) -> ast::MatchArm { 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 arms_str = arms.into_iter().fold(String::new(), |mut acc, arm| {
let needs_comma = let needs_comma =
arm.comma_token().is_none() && arm.expr().is_none_or(|it| !it.is_block_like()); 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(); let arm = arm.syntax();
format_to_acc!(acc, " {arm}{comma}\n") format_to_acc!(acc, " {arm}{comma}\n")
}); });

View File

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