mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +00:00
Fix edge case with comments and branches
This commit is contained in:
parent
b9a314dcd9
commit
b08d6a0446
2 changed files with 43 additions and 16 deletions
|
@ -540,7 +540,7 @@ fn fmt_binops<'a, 'buf>(
|
|||
loc_left_side.format_with_options(buf, apply_needs_parens, Newlines::No, curr_indent);
|
||||
|
||||
if is_multiline {
|
||||
buf.ensure_ends_in_newline();
|
||||
buf.ensure_ends_with_newline();
|
||||
curr_indent = indent + INDENT;
|
||||
buf.indent(curr_indent);
|
||||
} else {
|
||||
|
@ -711,10 +711,21 @@ fn fmt_when<'a, 'buf>(
|
|||
if pattern_index == 0 {
|
||||
match &pattern.value {
|
||||
Pattern::SpaceBefore(sub_pattern, spaces) => {
|
||||
let added_blank_line;
|
||||
|
||||
if branch_index > 0 // Never render newlines before the first branch.
|
||||
&& matches!(spaces.first(), Some(CommentOrNewline::Newline))
|
||||
{
|
||||
buf.ensure_ends_in_newline();
|
||||
if prev_branch_was_multiline {
|
||||
// Multiline branches always get a full blank line after them.
|
||||
buf.ensure_ends_with_blank_line();
|
||||
added_blank_line = true;
|
||||
} else {
|
||||
buf.ensure_ends_with_newline();
|
||||
added_blank_line = false;
|
||||
}
|
||||
} else {
|
||||
added_blank_line = false;
|
||||
}
|
||||
|
||||
// Write comments (which may have been attached to the previous
|
||||
|
@ -722,22 +733,25 @@ fn fmt_when<'a, 'buf>(
|
|||
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent + INDENT);
|
||||
|
||||
if branch_index > 0 {
|
||||
buf.ensure_ends_in_newline();
|
||||
|
||||
if prev_branch_was_multiline {
|
||||
// Multiline branches always get a full blank line after them.
|
||||
buf.newline();
|
||||
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 => {
|
||||
buf.ensure_ends_in_newline();
|
||||
|
||||
if prev_branch_was_multiline {
|
||||
// Multiline branches always get a full blank line after them.
|
||||
buf.newline();
|
||||
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);
|
||||
|
@ -745,7 +759,7 @@ fn fmt_when<'a, 'buf>(
|
|||
}
|
||||
} else {
|
||||
if is_multiline_patterns {
|
||||
buf.ensure_ends_in_newline();
|
||||
buf.ensure_ends_with_newline();
|
||||
// Indent an extra level for the `|`;
|
||||
// otherwise it'll be at the start of the line,
|
||||
// and will be incorrectly parsed as a pattern
|
||||
|
@ -774,7 +788,7 @@ fn fmt_when<'a, 'buf>(
|
|||
fmt_spaces_no_blank_lines(buf, spaces.iter(), indent + (INDENT * 2));
|
||||
|
||||
if is_multiline_expr {
|
||||
buf.ensure_ends_in_newline();
|
||||
buf.ensure_ends_with_newline();
|
||||
} else {
|
||||
buf.spaces(1);
|
||||
}
|
||||
|
@ -788,7 +802,7 @@ fn fmt_when<'a, 'buf>(
|
|||
}
|
||||
_ => {
|
||||
if is_multiline_expr {
|
||||
buf.ensure_ends_in_newline();
|
||||
buf.ensure_ends_with_newline();
|
||||
} else {
|
||||
buf.spaces(1);
|
||||
}
|
||||
|
|
|
@ -98,7 +98,7 @@ impl<'a> Buf<'a> {
|
|||
|
||||
/// Ensures the current buffer ends in a newline, if it didn't already.
|
||||
/// Doesn't add a newline if the buffer already ends in one.
|
||||
pub fn ensure_ends_in_newline(&mut self) {
|
||||
pub fn ensure_ends_with_newline(&mut self) {
|
||||
if self.spaces_to_flush > 0 {
|
||||
self.flush_spaces();
|
||||
self.newline();
|
||||
|
@ -107,6 +107,19 @@ impl<'a> Buf<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn ensure_ends_with_blank_line(&mut self) {
|
||||
if self.spaces_to_flush > 0 {
|
||||
self.flush_spaces();
|
||||
self.newline();
|
||||
self.newline();
|
||||
} else if !self.text.ends_with('\n') {
|
||||
self.newline();
|
||||
self.newline();
|
||||
} else if !self.text.ends_with("\n\n") {
|
||||
self.newline();
|
||||
}
|
||||
}
|
||||
|
||||
fn flush_spaces(&mut self) {
|
||||
if self.spaces_to_flush > 0 {
|
||||
for _ in 0..self.spaces_to_flush {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue