mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 12:18:19 +00:00
Fix comments in when
branches
This commit is contained in:
parent
18edf5f0d6
commit
15932d7d47
2 changed files with 38 additions and 10 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4038,7 +4038,7 @@ mod test_fmt {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn multiline_binop_conditional_with_comments() {
|
||||
fn multiline_binop_if_with_comments() {
|
||||
expr_formats_same(indoc!(
|
||||
r#"
|
||||
if
|
||||
|
@ -4054,6 +4054,26 @@ mod test_fmt {
|
|||
"#
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiline_binop_when_with_comments() {
|
||||
expr_formats_same(indoc!(
|
||||
r#"
|
||||
when
|
||||
x
|
||||
+ 1 # comment 1
|
||||
> 0 # comment 2
|
||||
is
|
||||
y ->
|
||||
3
|
||||
* 2 # comment 3
|
||||
< 1 # comment 4
|
||||
z ->
|
||||
4
|
||||
/ 5 # comment 5
|
||||
< 1 # comment 6
|
||||
_ ->
|
||||
42
|
||||
"#
|
||||
));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue