Fix when indent in the presence of weird multiline patterns

This commit is contained in:
Joshua Warner 2024-12-13 11:05:16 -08:00
parent 1d51f5fbec
commit 02e07f95e7
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
6 changed files with 79 additions and 14 deletions

View file

@ -1514,7 +1514,6 @@ fn fmt_when<'a>(
buf: &mut Buf,
loc_condition: &'a Loc<Expr<'a>>,
branches: &[&'a WhenBranch<'a>],
indent: u16,
) {
buf.ensure_ends_with_newline();
@ -1604,11 +1603,14 @@ fn fmt_when<'a>(
}
buf.indent(indent + INDENT);
let line_indent = buf.cur_line_indent();
buf.push_str(" ->");
let inner_indent = line_indent + INDENT;
match expr.value {
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 {
buf.ensure_ends_with_newline();
@ -1616,12 +1618,7 @@ fn fmt_when<'a>(
buf.spaces(1);
}
nested.format_with_options(
buf,
Parens::NotNeeded,
Newlines::Yes,
indent + 2 * INDENT,
);
nested.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, inner_indent);
}
_ => {
if is_multiline_expr {
@ -1630,12 +1627,7 @@ fn fmt_when<'a>(
buf.spaces(1);
}
expr.format_with_options(
buf,
Parens::NotNeeded,
Newlines::Yes,
indent + 2 * INDENT,
);
expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, inner_indent);
}
}

View file

@ -69,6 +69,7 @@ impl<'a> Buf<'a> {
self.beginning_of_line = false;
}
#[track_caller]
pub fn cur_line_indent(&self) -> u16 {
debug_assert!(!self.beginning_of_line, "cur_line_indent before indent");
self.line_indent