Format multiline reecord builder fields nicely

This commit is contained in:
Agustin Zubiaga 2023-05-17 12:48:37 -03:00
parent 5edcb31f32
commit 4cd2c957ca
No known key found for this signature in database
2 changed files with 69 additions and 5 deletions

View file

@ -1544,8 +1544,15 @@ fn format_record_builder_field_multiline(
buf.push_str(separator_prefix);
buf.push_str(":");
buf.spaces(1);
ann.value.format(buf, indent);
if ann.value.is_multiline() {
buf.newline();
ann.value.format(buf, indent + INDENT);
} else {
buf.spaces(1);
ann.value.format(buf, indent);
}
buf.push(',');
}
ApplyValue(name, colon_spaces, arrow_spaces, ann) => {
@ -1564,13 +1571,18 @@ fn format_record_builder_field_multiline(
if !arrow_spaces.is_empty() {
fmt_spaces(buf, arrow_spaces.iter(), indent);
buf.indent(indent);
buf.indent(indent + INDENT);
}
buf.push_str("<-");
buf.spaces(1);
ann.value.format(buf, indent);
if ann.value.is_multiline() {
buf.newline();
ann.value.format(buf, indent + INDENT);
} else {
buf.spaces(1);
ann.value.format(buf, indent);
}
buf.push(',');
}
LabelOnly(name) => {

View file

@ -1978,6 +1978,58 @@ mod test_fmt {
);
}
#[test]
fn multiline_record_builder_field() {
expr_formats_to(
indoc!(
r#"
succeed {
a: <- get "a" |> map (\x -> x * 2)
|> batch,
b: <- get "b" |> batch,
c: items
|> List.map \x -> x * 2
}
"#
),
indoc!(
r#"
succeed {
a: <-
get "a"
|> map (\x -> x * 2)
|> batch,
b: <- get "b" |> batch,
c:
items
|> List.map \x -> x * 2,
}
"#
),
);
expr_formats_same(indoc!(
r#"
succeed {
a: # I like to comment in weird places
<- get "a" |> batch,
b: <- get "b" |> batch,
}
"#
));
expr_formats_same(indoc!(
r#"
succeed {
a:
# I like to comment in weird places
<- get "a" |> batch,
b: <- get "b" |> batch,
}
"#
));
}
#[test]
fn outdentable_record_builders() {
expr_formats_to(