Skip to content

Commit d19633d

Browse files
authored
Keep multiline inline records in exception and constructor declarations (#8918)
1 parent 0076143 commit d19633d

5 files changed

Lines changed: 131 additions & 14 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Keep inline records in exception and constructor declarations multiline when the source has a line break after `{`.

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,14 +1665,14 @@ and print_type_param ~state (param : Parsetree.core_type * Asttypes.variance)
16651665
in
16661666
Doc.concat [printed_variance; print_typ_expr ~state typ cmt_tbl]
16671667

1668+
and get_field_start_line (ld : Parsetree.label_declaration) =
1669+
(* For spread fields (...), use the type location instead of pld_loc
1670+
because pld_loc may incorrectly include preceding whitespace *)
1671+
if ld.pld_name.txt = "..." then ld.pld_type.ptyp_loc.loc_start.pos_lnum
1672+
else ld.pld_loc.loc_start.pos_lnum
1673+
16681674
and print_record_declaration ?check_break_from_loc ?inline_record_definitions
16691675
?record_loc ~state (lds : Parsetree.label_declaration list) cmt_tbl =
1670-
let get_field_start_line (ld : Parsetree.label_declaration) =
1671-
(* For spread fields (...), use the type location instead of pld_loc
1672-
because pld_loc may incorrectly include preceding whitespace *)
1673-
if ld.pld_name.txt = "..." then ld.pld_type.ptyp_loc.loc_start.pos_lnum
1674-
else ld.pld_loc.loc_start.pos_lnum
1675-
in
16761676
let force_break =
16771677
match (check_break_from_loc, record_loc, lds) with
16781678
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum
@@ -1855,8 +1855,8 @@ and print_constructor_declaration2 ~state i
18551855
print_comments doc cmt_tbl cd.pcd_name.loc
18561856
in
18571857
let constr_args =
1858-
print_constructor_arguments ~is_dot_dot_dot ~state ~indent:true cd.pcd_args
1859-
cmt_tbl
1858+
print_constructor_arguments ~is_dot_dot_dot ~name_loc:cd.pcd_name.loc ~state
1859+
~indent:true cd.pcd_args cmt_tbl
18601860
in
18611861
let gadt =
18621862
match cd.pcd_res with
@@ -1879,8 +1879,8 @@ and print_constructor_declaration2 ~state i
18791879
]);
18801880
]
18811881

1882-
and print_constructor_arguments ?(is_dot_dot_dot = false) ~state ~indent
1883-
(cd_args : Parsetree.constructor_arguments) cmt_tbl =
1882+
and print_constructor_arguments ?(is_dot_dot_dot = false) ~name_loc ~state
1883+
~indent (cd_args : Parsetree.constructor_arguments) cmt_tbl =
18841884
match cd_args with
18851885
| Pcstr_tuple [] -> Doc.nil
18861886
| Pcstr_tuple types ->
@@ -1929,7 +1929,15 @@ and print_constructor_arguments ?(is_dot_dot_dot = false) ~state ~indent
19291929
Doc.rparen;
19301930
]
19311931
in
1932-
Doc.group (if indent then Doc.indent args else args)
1932+
(* Like a record type declaration, keep the inline record multiline when
1933+
the first field starts on a later line than the constructor name. *)
1934+
let force_break =
1935+
match lds with
1936+
| first :: _ ->
1937+
name_loc.Location.loc_end.pos_lnum < get_field_start_line first
1938+
| [] -> false
1939+
in
1940+
Doc.breakable_group ~force_break (if indent then Doc.indent args else args)
19331941

19341942
and print_label_declaration ?inline_record_definitions ~state
19351943
(ld : Parsetree.label_declaration) cmt_tbl =
@@ -6449,7 +6457,9 @@ and print_exception_def ~state (constr : Parsetree.extension_constructor)
64496457
in
64506458
Doc.concat
64516459
[
6452-
print_constructor_arguments ~state ~indent:false args cmt_tbl; gadt_doc;
6460+
print_constructor_arguments ~name_loc:constr.pext_name.loc ~state
6461+
~indent:false args cmt_tbl;
6462+
gadt_doc;
64536463
]
64546464
in
64556465
let name =
@@ -6489,7 +6499,9 @@ and print_extension_constructor ~state
64896499
in
64906500
Doc.concat
64916501
[
6492-
print_constructor_arguments ~state ~indent:false args cmt_tbl; gadt_doc;
6502+
print_constructor_arguments ~name_loc:constr.pext_name.loc ~state
6503+
~indent:false args cmt_tbl;
6504+
gadt_doc;
64936505
]
64946506
in
64956507
let name =
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// An inline record stays multiline when the source had a line break after `{`
2+
exception Foo({
3+
name: string,
4+
msg: string,
5+
})
6+
7+
exception Bar({name: string, msg: string})
8+
9+
@attr
10+
exception Baz({
11+
x: int,
12+
})
13+
14+
type t =
15+
| A({
16+
name: string,
17+
msg: string,
18+
})
19+
| B({name: string})
20+
21+
type u =
22+
| A({
23+
name: string,
24+
msg: string,
25+
})
26+
27+
type v = A({name: string, msg: string}) | B
28+
29+
type spread =
30+
| A({...r, y: int})
31+
| B({
32+
...r,
33+
y: int,
34+
})
35+
36+
type rec gadt<_> =
37+
| Gadt({
38+
x: int,
39+
}): gadt<int>
40+
41+
type ext = ..
42+
type ext +=
43+
| C({
44+
name: string,
45+
msg: string,
46+
})
47+
| D({name: string})
48+
49+
module type S = {
50+
exception E({
51+
x: int,
52+
})
53+
}

‎tests/syntax_tests/data/printer/typeDef/expected/variant.res.txt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ type color =
3535
| Hex(superLongTypeName, superLongTypeName, superLongTypeName, superLongTypeName)
3636

3737
type node<_, 'value> =
38-
| Root({mutable value: 'value, mutable updatedTime: float}): node<root, 'value>
38+
| Root({
39+
mutable value: 'value,
40+
mutable updatedTime: float,
41+
}): node<root, 'value>
3942
| Derived({
4043
mutable cachedValue: 'value,
4144
parent: node<_, 'value>,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// An inline record stays multiline when the source had a line break after `{`
2+
exception Foo({
3+
name: string,
4+
msg: string,
5+
})
6+
7+
exception Bar({name: string, msg: string})
8+
9+
@attr exception Baz({
10+
x: int,
11+
})
12+
13+
type t =
14+
| A({
15+
name: string,
16+
msg: string,
17+
})
18+
| B({name: string})
19+
20+
type u = A({
21+
name: string,
22+
msg: string,
23+
})
24+
25+
type v = A({name: string, msg: string}) | B
26+
27+
type spread = A({...r, y: int}) | B({
28+
...r,
29+
y: int,
30+
})
31+
32+
type rec gadt<_> = Gadt({
33+
x: int,
34+
}): gadt<int>
35+
36+
type ext = ..
37+
type ext +=
38+
| C({
39+
name: string,
40+
msg: string,
41+
})
42+
| D({name: string})
43+
44+
module type S = {
45+
exception E({
46+
x: int,
47+
})
48+
}

0 commit comments

Comments
 (0)