Fix comments in when branches

This commit is contained in:
Richard Feldman 2022-07-06 16:37:04 -04:00
parent 18edf5f0d6
commit 15932d7d47
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
2 changed files with 38 additions and 10 deletions

View file

@ -704,17 +704,29 @@ fn fmt_when<'a, 'buf>(
let is_multiline_expr = expr.is_multiline();
let is_multiline_patterns = is_when_patterns_multiline(branch);
for (index, pattern) in patterns.iter().enumerate() {
if index == 0 {
for (pattern_index, pattern) in patterns.iter().enumerate() {
if pattern_index == 0 {
match &pattern.value {
Pattern::SpaceBefore(sub_pattern, spaces) if branch_index == 0 => {
// Never include extra newlines before the first branch.
// Instead, write the comments and that's it.
Pattern::SpaceBefore(sub_pattern, spaces) => {
if branch_index > 0 // Never render newlines before the first branch.
&& matches!(spaces.first(), Some(CommentOrNewline::Newline))
{
buf.ensure_ends_in_newline();
}
// Write comments (which may have been attached to the previous
// branch's expr, if there was a previous branch).
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent + INDENT);
if branch_index > 0 {
buf.ensure_ends_in_newline();
}
fmt_pattern(buf, sub_pattern, indent + INDENT, Parens::NotNeeded);
}
other => {
buf.ensure_ends_in_newline();
fmt_pattern(buf, other, indent + INDENT, Parens::NotNeeded);
}
}
@ -769,10 +781,6 @@ fn fmt_when<'a, 'buf>(
);
}
}
if it.peek().is_some() {
buf.newline();
}
}
}