Record updates

This commit is contained in:
Joshua Warner 2024-12-01 15:59:34 -08:00
parent b9862b47dc
commit edcdd99f59
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
22 changed files with 194 additions and 42 deletions

View file

@ -617,6 +617,7 @@ impl<'a> Formattable for ModuleImport<'a> {
let indent = if !before_name.is_empty()
|| (params.is_multiline() && exposed.is_some())
|| params.map(|p| !p.before.is_empty()).unwrap_or(false)
|| alias.is_multiline()
|| exposed.map_or(false, |e| e.keyword.is_multiline())
{

View file

@ -1819,7 +1819,10 @@ fn fmt_record_like<'a, 'b: 'a, Field, Format, ToSpacesAround>(
if is_multiline {
let field_indent = indent + INDENT;
for (index, field) in loc_fields.iter().enumerate() {
let mut last_after: &[CommentOrNewline<'_>] = &[];
for field in loc_fields.iter() {
// comma addition is handled by the `format_field_multiline` function
// since we can have stuff like:
// { x # comment
@ -1827,53 +1830,33 @@ fn fmt_record_like<'a, 'b: 'a, Field, Format, ToSpacesAround>(
// }
// In this case, we have to move the comma before the comment.
let is_first_item = index == 0;
let field_lifted = to_space_around(buf.text.bump(), &field.value);
if !field_lifted.before.is_empty() {
let is_only_newlines = field_lifted.before.iter().all(|s| s.is_newline());
if !is_first_item
&& !is_only_newlines
&& count_leading_newlines(field_lifted.before.iter()) > 1
{
buf.newline();
}
fmt_comments_only(
buf,
field_lifted.before.iter(),
NewlineAt::Top,
field_indent,
);
let before = merge_spaces(buf.text.bump(), last_after, field_lifted.before);
if !is_only_newlines
&& count_leading_newlines(field_lifted.before.iter().rev()) > 0
{
buf.newline();
}
}
fmt_comments_only(
buf,
field_lifted.before.iter(),
NewlineAt::Bottom,
field_indent,
);
// if index == 0
// && field_lifted
// .before
// .first()
// .map_or(false, |s| s.is_comment())
// {
// buf.ensure_ends_with_newline();
// }
fmt_comments_only(buf, before.iter(), NewlineAt::Bottom, field_indent);
buf.ensure_ends_with_newline();
format_field_multiline(buf, &field_lifted.item, field_indent, "");
fmt_comments_only(
buf,
field_lifted.after.iter(),
NewlineAt::Bottom,
field_indent,
);
last_after = field_lifted.after;
}
if count_leading_newlines(final_comments.iter()) > 1 {
buf.newline();
}
let after = merge_spaces(buf.text.bump(), last_after, final_comments);
fmt_comments_only(buf, final_comments.iter(), NewlineAt::Bottom, field_indent);
// if count_leading_newlines(final_comments.iter()) > 1 {
// buf.newline();
// }
buf.newline();
fmt_comments_only(buf, after.iter(), NewlineAt::Both, field_indent);
buf.ensure_ends_with_newline();
} else {
// is_multiline == false
buf.spaces(1);

View file

@ -0,0 +1,22 @@
expect
html : Html {}
html =
Element "a" 43 [HtmlAttr "href" "https://www.roc-lang.org/"] [Text "Roc"]
actual : { nodes : List RenderedNode, siblingIds : List U64 }
actual =
indexNodes { nodes: [], siblingIds: [] } html
expected : { nodes : List RenderedNode, siblingIds : List U64 }
expected = {
nodes: [
RenderedText "Roc",
RenderedElement "a" { emptyRenderedAttrs & htmlAttrs: Dict.fromList [("href", "https://www.roc-lang.org/")] } [0],
],
siblingIds: [1],
}
(actual.nodes == expected.nodes)
&& (actual.siblingIds == expected.siblingIds)

View file

@ -0,0 +1,55 @@
SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(2147483648),
],
regions: [
@0-12,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
space_after: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
spaces: [],
type_defs: [],
value_defs: [
ModuleImport(
ModuleImport {
before_name: [],
name: @7-8 ImportedModuleName {
package: None,
name: ModuleName(
"P",
),
},
params: Some(
ModuleImportParams {
before: [
Newline,
],
params: @10-12 [],
},
),
alias: None,
exposed: None,
},
),
],
},
@13-14 SpaceBefore(
Var {
module_name: "",
ident: "y",
},
[
Newline,
],
),
),
[
Newline,
],
)

View file

@ -1,34 +1,66 @@
{
u8: 123u8,
u16: 123u16,
u32: 123u32,
u64: 123u64,
u128: 123u128,
i8: 123i8,
i16: 123i16,
i32: 123i32,
i64: 123i64,
i128: 123i128,
dec: 123dec,
u8Neg: -123u8,
u16Neg: -123u16,
u32Neg: -123u32,
u64Neg: -123u64,
u128Neg: -123u128,
i8Neg: -123i8,
i16Neg: -123i16,
i32Neg: -123i32,
i64Neg: -123i64,
i128Neg: -123i128,
decNeg: -123dec,
u8Bin: 0b101u8,
u16Bin: 0b101u16,
u32Bin: 0b101u32,
u64Bin: 0b101u64,
u128Bin: 0b101u128,
i8Bin: 0b101i8,
i16Bin: 0b101i16,
i32Bin: 0b101i32,
i64Bin: 0b101i64,
i128Bin: 0b101i128,
}

View file

@ -1,4 +1,6 @@
{ Foo.Bar.baz <-
x: 5,
y: 0,
}

View file

@ -1,6 +1,10 @@
{ Foo.Bar.baz <-
x: 5,
y: 0,
_z: 3,
_: 2,
}

View file

@ -0,0 +1,14 @@
Record(
[
@3-4 SpaceBefore(
LabelOnly(
@3-4 "a",
),
[
LineComment(
"",
),
],
),
],
)

View file

@ -0,0 +1,12 @@
Record(
Collection {
items: [],
final_comments: [
Newline,
Newline,
LineComment(
"",
),
],
},
)

View file

@ -1,4 +1,6 @@
{
answer: 42,
launchTheNukes!: \{} -> boom,
}

View file

@ -483,6 +483,7 @@ mod test_snapshots {
pass/newline_after_sub.expr,
pass/newline_and_spaces_before_less_than.expr,
pass/newline_before_add.expr,
pass/newline_before_import_curlies.expr,
pass/newline_before_sub.expr,
pass/newline_in_packages.full,
pass/newline_in_type_alias_application.expr,
@ -559,8 +560,10 @@ mod test_snapshots {
pass/record_access_after_tuple.expr,
pass/record_builder.expr,
pass/record_builder_ignored_fields.expr,
pass/record_comment_newline_field.expr,
pass/record_destructure_def.expr,
pass/record_destructure_field_bang.expr,
pass/record_double_newline_comment_field.expr,
pass/record_func_type_decl.expr,
pass/record_literal_field_bang.expr,
pass/record_type_with_function.expr,