Skip to content

Commit 2c01609

Browse files
VeykrilGiga-Bowser
andcommitted
minor: Handle match arm commas in make::match_arm
Co-authored-by: Giga Bowser <45986823+Giga-Bowser@users.noreply.github.com>
1 parent 4183bcd commit 2c01609

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

crates/ide-assists/src/handlers/unmerge_match_arm.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use syntax::{
22
Direction, SyntaxKind, T,
3-
algo::neighbor,
43
ast::{self, AstNode, edit::IndentLevel, syntax_factory::SyntaxFactory},
54
syntax_editor::{Element, Position},
65
};
@@ -88,15 +87,8 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
8887
// body is a block, but we don't bother to check that.
8988
// - Missing after the arm with arms after, if the arm body is a block. In this case
9089
// we don't want to insert a comma at all.
91-
let has_comma_after =
92-
std::iter::successors(match_arm.syntax().last_child_or_token(), |it| {
93-
it.prev_sibling_or_token()
94-
})
95-
.map(|it| it.kind())
96-
.find(|it| !it.is_trivia())
97-
== Some(T![,]);
98-
let has_arms_after = neighbor(&match_arm, Direction::Next).is_some();
99-
if !has_comma_after && !has_arms_after {
90+
let has_comma_after = match_arm.comma_token().is_some();
91+
if !has_comma_after && !match_arm.expr().unwrap().is_block_like() {
10092
insert_after_old_arm.push(make.token(T![,]).into());
10193
}
10294

@@ -105,9 +97,6 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
10597

10698
insert_after_old_arm.push(new_match_arm.syntax().clone().into());
10799

108-
if has_comma_after {
109-
insert_after_old_arm.push(make.token(T![,]).into());
110-
}
111100
editor.insert_all(Position::after(match_arm.syntax()), insert_after_old_arm);
112101
editor.add_mappings(make.finish_with_mappings());
113102
edit.add_file_edits(ctx.vfs_file_id(), editor);
@@ -256,7 +245,7 @@ fn main() {
256245
let x = X::A;
257246
let y = match x {
258247
X::A => 1i32,
259-
X::B => 1i32
248+
X::B => 1i32,
260249
};
261250
}
262251
"#,
@@ -274,7 +263,7 @@ enum X { A, B }
274263
fn main() {
275264
let x = X::A;
276265
match x {
277-
X::A $0| X::B => {},
266+
X::A $0| X::B => {}
278267
}
279268
}
280269
"#,
@@ -285,8 +274,8 @@ enum X { A, B }
285274
fn main() {
286275
let x = X::A;
287276
match x {
288-
X::A => {},
289-
X::B => {},
277+
X::A => {}
278+
X::B => {}
290279
}
291280
}
292281
"#,

crates/syntax/src/ast/make.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,10 @@ pub fn ref_pat(pat: ast::Pat) -> ast::RefPat {
842842
}
843843

844844
pub fn match_arm(pat: ast::Pat, guard: Option<ast::MatchGuard>, expr: ast::Expr) -> ast::MatchArm {
845+
let comma_str = if expr.is_block_like() { "" } else { "," };
845846
return match guard {
846-
Some(guard) => from_text(&format!("{pat} {guard} => {expr}")),
847-
None => from_text(&format!("{pat} => {expr}")),
847+
Some(guard) => from_text(&format!("{pat} {guard} => {expr}{comma_str}")),
848+
None => from_text(&format!("{pat} => {expr}{comma_str}")),
848849
};
849850

850851
fn from_text(text: &str) -> ast::MatchArm {
@@ -877,7 +878,7 @@ pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::Mat
877878
let arms_str = arms.into_iter().fold(String::new(), |mut acc, arm| {
878879
let needs_comma =
879880
arm.comma_token().is_none() && arm.expr().is_none_or(|it| !it.is_block_like());
880-
let comma = if needs_comma { "," } else { "" };
881+
let comma = if needs_comma && arm.comma_token().is_none() { "," } else { "" };
881882
let arm = arm.syntax();
882883
format_to_acc!(acc, " {arm}{comma}\n")
883884
});

crates/syntax/src/syntax_editor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ mod tests {
435435
_ => {
436436
let var_name = 2 + 2;
437437
(var_name, true)
438-
}"#]];
438+
},"#]];
439439
expect.assert_eq(&edit.new_root.to_string());
440440

441441
assert_eq!(edit.find_annotation(placeholder_snippet).len(), 2);

0 commit comments

Comments
 (0)