Skip to content

Commit 7786d60

Browse files
authored
fix(grainfmt): Correct formatting of record assignments (#2344)
1 parent 1983324 commit 7786d60

File tree

3 files changed

+54
-64
lines changed

3 files changed

+54
-64
lines changed

compiler/src/formatting/fmt.re

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,47 @@ let is_keyword_function = expr => {
237237
};
238238
};
239239

240+
let rec is_collapsible = (root, value) => {
241+
switch (root.pexp_desc, value.pexp_desc) {
242+
| (
243+
PExpId({txt: IdentName({txt: name})}),
244+
PExpId({txt: IdentName({txt: new_name})}),
245+
)
246+
when name == new_name =>
247+
true
248+
| (
249+
PExpRecordGet(root, {txt: IdentName({txt: name})}),
250+
PExpRecordGet(new_value, {txt: IdentName({txt: new_name})}),
251+
)
252+
when name == new_name =>
253+
is_collapsible(root, new_value)
254+
| (
255+
PExpRecordSet(root, {txt: IdentName({txt: elem_name})}, _),
256+
PExpApp(
257+
_,
258+
[
259+
{
260+
paa_expr:
261+
{
262+
pexp_desc:
263+
PExpRecordGet(
264+
new_value,
265+
{txt: IdentName({txt: new_elem_name})},
266+
),
267+
},
268+
},
269+
_,
270+
],
271+
),
272+
)
273+
when elem_name == new_elem_name =>
274+
is_collapsible(root, new_value)
275+
| (_, PExpApp(_, [{paa_expr: {pexp_desc: PExpId(_)} as new_value}, _])) =>
276+
is_collapsible(root, new_value)
277+
| _ => false
278+
};
279+
};
280+
240281
let has_labeled_arg = args =>
241282
List.exists(
242283
a =>
@@ -2158,87 +2199,26 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
21582199
++ string(".")
21592200
++ fmt.print_comment_range(fmt, record.pexp_loc, elem.loc)
21602201
++ fmt.print_identifier(fmt, elem.txt)
2161-
| PExpRecordSet(
2162-
{pexp_desc: PExpId({txt: IdentName({txt: name})})} as record,
2163-
{txt: elem_name} as elem,
2164-
{
2165-
pexp_desc:
2166-
PExpApp(
2167-
_,
2168-
[
2169-
{
2170-
paa_expr:
2171-
{
2172-
pexp_desc:
2173-
PExpRecordGet(
2174-
{
2175-
pexp_desc:
2176-
PExpId({txt: IdentName({txt: new_name})}),
2177-
},
2178-
{txt: new_elem_name},
2179-
),
2180-
},
2181-
},
2182-
_,
2183-
],
2184-
),
2185-
} as new_value,
2186-
)
2187-
when name == new_name && elem_name == new_elem_name =>
2188-
fmt.print_grouped_access_expression(fmt, record)
2189-
++ string(".")
2190-
++ fmt.print_comment_range(fmt, record.pexp_loc, elem.loc)
2191-
++ fmt.print_identifier(fmt, elem.txt)
2192-
++ fmt.print_assignment(
2193-
fmt,
2194-
~collapsible=true,
2195-
~lhs_loc=elem.loc,
2196-
new_value,
2197-
)
21982202
| PExpRecordSet(record, elem, new_value) =>
21992203
fmt.print_grouped_access_expression(fmt, record)
22002204
++ string(".")
22012205
++ fmt.print_comment_range(fmt, record.pexp_loc, elem.loc)
22022206
++ fmt.print_identifier(fmt, elem.txt)
22032207
++ fmt.print_assignment(
22042208
fmt,
2205-
~collapsible=false,
2209+
~collapsible=is_collapsible(expr, new_value),
22062210
~lhs_loc=elem.loc,
22072211
new_value,
22082212
)
22092213
| PExpPrim0(_) => failwith("Impossible: PExpPrim0 in parsetree")
22102214
| PExpPrim1(_) => failwith("Impossible: PExpPrim1 in parsetree")
22112215
| PExpPrim2(_) => failwith("Impossible: PExpPrim2 in parsetree")
22122216
| PExpPrimN(_) => failwith("Impossible: PExpPrimN in parsetree")
2213-
| PExpAssign(
2214-
{pexp_desc: PExpId({txt: IdentName({txt: name})})} as binding,
2215-
{
2216-
pexp_desc:
2217-
PExpApp(
2218-
_,
2219-
[
2220-
{
2221-
paa_expr:
2222-
{pexp_desc: PExpId({txt: IdentName({txt: new_name})})},
2223-
},
2224-
_,
2225-
],
2226-
),
2227-
} as new_value,
2228-
)
2229-
when name == new_name =>
2230-
fmt.print_expression(fmt, binding)
2231-
++ fmt.print_assignment(
2232-
fmt,
2233-
~collapsible=true,
2234-
~lhs_loc=binding.pexp_loc,
2235-
new_value,
2236-
)
22372217
| PExpAssign(binding, new_value) =>
22382218
fmt.print_expression(fmt, binding)
22392219
++ fmt.print_assignment(
22402220
fmt,
2241-
~collapsible=false,
2221+
~collapsible=is_collapsible(binding, new_value),
22422222
~lhs_loc=binding.pexp_loc,
22432223
new_value,
22442224
)

compiler/test/grainfmt/application2.expected.gr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ record Foo {
6363
}
6464
let foo = { idx: 1, }
6565
foo.idx += 2
66+
foo.idx += 2
67+
68+
record Nested {
69+
x: Foo,
70+
}
71+
let nested = { x: { idx: 1, }, }
72+
nested.x.idx += 2
73+
nested.x.idx += 2

compiler/test/grainfmt/application2.input.gr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ let makePrettyPrintJSONWriter = String.forEachCodePoint(c => {
4848
void
4949
}, "")
5050

51-
record Foo { mut idx: Number }; let foo = { idx: 1 }; foo.idx += 2
51+
record Foo { mut idx: Number }; let foo = { idx: 1 }; foo.idx += 2; foo.idx = foo.idx + 2;
52+
53+
record Nested { x: Foo }; let nested = { x: { idx: 1 } }; nested.x.idx += 2; nested.x.idx = nested.x.idx + 2;

0 commit comments

Comments
 (0)