Simplify some branch formatting logic

This commit is contained in:
Richard Feldman 2022-07-13 22:55:41 -04:00
parent fa877e4184
commit b51c8b0ba4
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798

View file

@ -700,53 +700,49 @@ fn fmt_when<'a, 'buf>(
for (pattern_index, pattern) in patterns.iter().enumerate() { for (pattern_index, pattern) in patterns.iter().enumerate() {
if pattern_index == 0 { if pattern_index == 0 {
match &pattern.value { if let Pattern::SpaceBefore(sub_pattern, spaces) = &pattern.value {
Pattern::SpaceBefore(sub_pattern, spaces) => { let added_blank_line;
let added_blank_line;
if branch_index > 0 // Never render newlines before the first branch. // Never render newlines before the first branch.
&& matches!(spaces.first(), Some(CommentOrNewline::Newline)) if branch_index > 0 {
{ if prev_branch_was_multiline {
if prev_branch_was_multiline { // Multiline branches always get a full blank line after them.
// Multiline branches always get a full blank line after them. buf.ensure_ends_with_blank_line();
buf.ensure_ends_with_blank_line(); added_blank_line = true;
added_blank_line = true;
} else {
buf.ensure_ends_with_newline();
added_blank_line = false;
}
} else { } else {
buf.ensure_ends_with_newline();
added_blank_line = false; added_blank_line = false;
} }
} else {
// Write comments (which may have been attached to the previous added_blank_line = false;
// branch's expr, if there was a previous branch).
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent + INDENT);
if branch_index > 0 {
if prev_branch_was_multiline && !added_blank_line {
// Multiline branches always get a full blank line after them
// (which we may already have added before a comment).
buf.ensure_ends_with_blank_line();
} else {
buf.ensure_ends_with_newline();
}
}
fmt_pattern(buf, sub_pattern, indent + INDENT, Parens::NotNeeded);
} }
other => {
if branch_index > 0 {
if prev_branch_was_multiline {
// Multiline branches always get a full blank line after them.
buf.ensure_ends_with_blank_line();
} else {
buf.ensure_ends_with_newline();
}
}
fmt_pattern(buf, other, indent + INDENT, Parens::NotNeeded); // 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 {
if prev_branch_was_multiline && !added_blank_line {
// Multiline branches always get a full blank line after them
// (which we may already have added before a comment).
buf.ensure_ends_with_blank_line();
} else {
buf.ensure_ends_with_newline();
}
} }
fmt_pattern(buf, sub_pattern, indent + INDENT, Parens::NotNeeded);
} else {
if branch_index > 0 {
if prev_branch_was_multiline {
// Multiline branches always get a full blank line after them.
buf.ensure_ends_with_blank_line();
} else {
buf.ensure_ends_with_newline();
}
}
fmt_pattern(buf, &pattern.value, indent + INDENT, Parens::NotNeeded);
} }
} else { } else {
if is_multiline_patterns { if is_multiline_patterns {