mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 21:39:07 +00:00
Fix when indent in the presence of weird multiline patterns
This commit is contained in:
parent
1d51f5fbec
commit
02e07f95e7
6 changed files with 79 additions and 14 deletions
|
@ -1514,7 +1514,6 @@ fn fmt_when<'a>(
|
||||||
buf: &mut Buf,
|
buf: &mut Buf,
|
||||||
loc_condition: &'a Loc<Expr<'a>>,
|
loc_condition: &'a Loc<Expr<'a>>,
|
||||||
branches: &[&'a WhenBranch<'a>],
|
branches: &[&'a WhenBranch<'a>],
|
||||||
|
|
||||||
indent: u16,
|
indent: u16,
|
||||||
) {
|
) {
|
||||||
buf.ensure_ends_with_newline();
|
buf.ensure_ends_with_newline();
|
||||||
|
@ -1604,11 +1603,14 @@ fn fmt_when<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.indent(indent + INDENT);
|
buf.indent(indent + INDENT);
|
||||||
|
let line_indent = buf.cur_line_indent();
|
||||||
buf.push_str(" ->");
|
buf.push_str(" ->");
|
||||||
|
|
||||||
|
let inner_indent = line_indent + INDENT;
|
||||||
|
|
||||||
match expr.value {
|
match expr.value {
|
||||||
Expr::SpaceBefore(nested, spaces) => {
|
Expr::SpaceBefore(nested, spaces) => {
|
||||||
fmt_spaces_no_blank_lines(buf, spaces.iter(), indent + (INDENT * 2));
|
fmt_spaces_no_blank_lines(buf, spaces.iter(), inner_indent);
|
||||||
|
|
||||||
if is_multiline_expr {
|
if is_multiline_expr {
|
||||||
buf.ensure_ends_with_newline();
|
buf.ensure_ends_with_newline();
|
||||||
|
@ -1616,12 +1618,7 @@ fn fmt_when<'a>(
|
||||||
buf.spaces(1);
|
buf.spaces(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
nested.format_with_options(
|
nested.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, inner_indent);
|
||||||
buf,
|
|
||||||
Parens::NotNeeded,
|
|
||||||
Newlines::Yes,
|
|
||||||
indent + 2 * INDENT,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if is_multiline_expr {
|
if is_multiline_expr {
|
||||||
|
@ -1630,12 +1627,7 @@ fn fmt_when<'a>(
|
||||||
buf.spaces(1);
|
buf.spaces(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
expr.format_with_options(
|
expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, inner_indent);
|
||||||
buf,
|
|
||||||
Parens::NotNeeded,
|
|
||||||
Newlines::Yes,
|
|
||||||
indent + 2 * INDENT,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,7 @@ impl<'a> Buf<'a> {
|
||||||
self.beginning_of_line = false;
|
self.beginning_of_line = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
pub fn cur_line_indent(&self) -> u16 {
|
pub fn cur_line_indent(&self) -> u16 {
|
||||||
debug_assert!(!self.beginning_of_line, "cur_line_indent before indent");
|
debug_assert!(!self.beginning_of_line, "cur_line_indent before indent");
|
||||||
self.line_indent
|
self.line_indent
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
when 6 is
|
||||||
|
O #
|
||||||
|
B ->
|
||||||
|
when 6 is
|
||||||
|
1 -> O
|
|
@ -0,0 +1,62 @@
|
||||||
|
@0-31 SpaceAfter(
|
||||||
|
When(
|
||||||
|
@5-6 Num(
|
||||||
|
"6",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
WhenBranch {
|
||||||
|
patterns: [
|
||||||
|
@10-15 SpaceBefore(
|
||||||
|
Apply(
|
||||||
|
@10-11 Tag(
|
||||||
|
"O",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
@14-15 SpaceBefore(
|
||||||
|
Tag(
|
||||||
|
"B",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
LineComment(
|
||||||
|
"",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
value: @17-31 When(
|
||||||
|
@22-23 Num(
|
||||||
|
"6",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
WhenBranch {
|
||||||
|
patterns: [
|
||||||
|
@27-28 SpaceBefore(
|
||||||
|
NumLiteral(
|
||||||
|
"1",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
value: @30-31 Tag(
|
||||||
|
"O",
|
||||||
|
),
|
||||||
|
guard: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
guard: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
)
|
|
@ -0,0 +1,4 @@
|
||||||
|
when 6 is
|
||||||
|
O#
|
||||||
|
B->when 6 is
|
||||||
|
1->O
|
|
@ -500,6 +500,7 @@ mod test_snapshots {
|
||||||
pass/nested_if.expr,
|
pass/nested_if.expr,
|
||||||
pass/nested_list_comment_in_closure_arg.expr,
|
pass/nested_list_comment_in_closure_arg.expr,
|
||||||
pass/nested_parens_in_pattern.expr,
|
pass/nested_parens_in_pattern.expr,
|
||||||
|
pass/nested_when_comment_in_pat.expr,
|
||||||
pass/newline_after_equals.expr, // Regression test for https://github.com/roc-lang/roc/issues/51
|
pass/newline_after_equals.expr, // Regression test for https://github.com/roc-lang/roc/issues/51
|
||||||
pass/newline_after_mul.expr,
|
pass/newline_after_mul.expr,
|
||||||
pass/newline_after_opt_field.expr,
|
pass/newline_after_opt_field.expr,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue