Skip to content

Commit 640bb93

Browse files
authored
Refactor printing inline type declarations (#7741)
* refactor printing type declarations to fix inline type declarations not being printed correctly in interface files * changelog * for inline records, calculate force break from the record body itself
1 parent 2aee2fd commit 640bb93

File tree

4 files changed

+66
-41
lines changed

4 files changed

+66
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#### :bug: Bug fix
2222

23+
- Fix formatting of nested records in `.resi` files. https://github.com/rescript-lang/rescript/pull/7741
24+
2325
#### :memo: Documentation
2426

2527
#### :nail_care: Polish

compiler/syntax/src/res_printer.ml

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ let add_braces doc =
4040

4141
let add_async doc = Doc.concat [Doc.text "async "; doc]
4242

43+
let has_inline_type_definitions type_declarations =
44+
type_declarations
45+
|> List.find_opt (fun (td : Parsetree.type_declaration) ->
46+
Res_parsetree_viewer.has_inline_record_definition_attribute
47+
td.ptype_attributes)
48+
|> Option.is_some
49+
4350
let get_first_leading_comment tbl loc =
4451
match Hashtbl.find tbl.CommentTable.leading loc with
4552
| comment :: _ -> Some comment
@@ -587,29 +594,7 @@ and print_structure_item ~state (si : Parsetree.structure_item) cmt_tbl =
587594
| Asttypes.Recursive -> Doc.text "rec "
588595
in
589596
print_value_bindings ~state ~rec_flag value_bindings cmt_tbl
590-
| Pstr_type (Recursive, type_declarations)
591-
when type_declarations
592-
|> List.find_opt (fun (td : Parsetree.type_declaration) ->
593-
Res_parsetree_viewer.has_inline_record_definition_attribute
594-
td.ptype_attributes)
595-
|> Option.is_some ->
596-
let inline_record_definitions, regular_declarations =
597-
type_declarations
598-
|> List.partition (fun (td : Parsetree.type_declaration) ->
599-
Res_parsetree_viewer.has_inline_record_definition_attribute
600-
td.ptype_attributes)
601-
in
602-
print_type_declarations ~inline_record_definitions ~state
603-
~rec_flag:
604-
(if List.length regular_declarations > 1 then Doc.text "rec "
605-
else Doc.nil)
606-
regular_declarations cmt_tbl
607597
| Pstr_type (rec_flag, type_declarations) ->
608-
let rec_flag =
609-
match rec_flag with
610-
| Asttypes.Nonrecursive -> Doc.nil
611-
| Asttypes.Recursive -> Doc.text "rec "
612-
in
613598
print_type_declarations ~state ~rec_flag type_declarations cmt_tbl
614599
| Pstr_primitive value_description ->
615600
print_value_description ~state value_description cmt_tbl
@@ -985,11 +970,6 @@ and print_signature_item ~state (si : Parsetree.signature_item) cmt_tbl =
985970
| Parsetree.Psig_value value_description ->
986971
print_value_description ~state value_description cmt_tbl
987972
| Psig_type (rec_flag, type_declarations) ->
988-
let rec_flag =
989-
match rec_flag with
990-
| Asttypes.Nonrecursive -> Doc.nil
991-
| Asttypes.Recursive -> Doc.text "rec "
992-
in
993973
print_type_declarations ~state ~rec_flag type_declarations cmt_tbl
994974
| Psig_typext type_extension ->
995975
print_type_extension ~state type_extension cmt_tbl
@@ -1191,13 +1171,39 @@ and print_value_description ~state value_description cmt_tbl =
11911171
else Doc.nil);
11921172
])
11931173

1194-
and print_type_declarations ?inline_record_definitions ~state ~rec_flag
1195-
type_declarations cmt_tbl =
1196-
print_listi
1197-
~get_loc:(fun n -> n.Parsetree.ptype_loc)
1198-
~nodes:type_declarations
1199-
~print:(print_type_declaration2 ?inline_record_definitions ~state ~rec_flag)
1200-
cmt_tbl
1174+
and print_type_declarations ~state ~rec_flag type_declarations cmt_tbl =
1175+
if has_inline_type_definitions type_declarations then
1176+
let inline_record_definitions, regular_declarations =
1177+
type_declarations
1178+
|> List.partition (fun (td : Parsetree.type_declaration) ->
1179+
Res_parsetree_viewer.has_inline_record_definition_attribute
1180+
td.ptype_attributes)
1181+
in
1182+
let adjusted_rec_flag =
1183+
match rec_flag with
1184+
| Recursive ->
1185+
if List.length regular_declarations > 1 then Doc.text "rec "
1186+
else Doc.nil
1187+
| Nonrecursive -> Doc.nil
1188+
in
1189+
print_listi
1190+
~get_loc:(fun n -> n.Parsetree.ptype_loc)
1191+
~nodes:regular_declarations
1192+
~print:
1193+
(print_type_declaration2 ~inline_record_definitions ~state
1194+
~rec_flag:adjusted_rec_flag)
1195+
cmt_tbl
1196+
else
1197+
print_listi
1198+
~get_loc:(fun n -> n.Parsetree.ptype_loc)
1199+
~nodes:type_declarations
1200+
~print:
1201+
(print_type_declaration2 ~state
1202+
~rec_flag:
1203+
(match rec_flag with
1204+
| Nonrecursive -> Doc.nil
1205+
| Recursive -> Doc.text "rec "))
1206+
cmt_tbl
12011207

12021208
(*
12031209
* type_declaration = {
@@ -1458,13 +1464,14 @@ and print_type_param ~state (param : Parsetree.core_type * Asttypes.variance)
14581464
in
14591465
Doc.concat [printed_variance; print_typ_expr ~state typ cmt_tbl]
14601466

1461-
and print_record_declaration ?inline_record_definitions ~state
1462-
(lds : Parsetree.label_declaration list) cmt_tbl =
1467+
and print_record_declaration ?check_break_from_loc ?inline_record_definitions
1468+
~state (lds : Parsetree.label_declaration list) cmt_tbl =
14631469
let force_break =
1464-
match (lds, List.rev lds) with
1465-
| first :: _, last :: _ ->
1470+
match (check_break_from_loc, lds, List.rev lds) with
1471+
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum
1472+
| _, first :: _, last :: _ ->
14661473
first.pld_loc.loc_start.pos_lnum < last.pld_loc.loc_end.pos_lnum
1467-
| _ -> false
1474+
| _, _, _ -> false
14681475
in
14691476
Doc.breakable_group ~force_break
14701477
(Doc.concat
@@ -1799,8 +1806,8 @@ and print_typ_expr ?inline_record_definitions ~(state : State.t)
17991806
inline_record_definitions
18001807
|> find_inline_record_definition inline_record_name
18011808
with
1802-
| Some {ptype_kind = Ptype_record lds} ->
1803-
print_record_declaration
1809+
| Some {ptype_kind = Ptype_record lds; ptype_loc} ->
1810+
print_record_declaration ~check_break_from_loc:ptype_loc
18041811
~inline_record_definitions:(inline_record_definitions |> Option.get)
18051812
~state lds cmt_tbl
18061813
| _ -> assert false)

tests/syntax_tests/data/printer/signature/expected/type.resi.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ type color =
66
| Red
77
| Blue
88
| Black
9+
10+
type options = {
11+
permissions: {
12+
all: {
13+
stuff: bool,
14+
},
15+
},
16+
}

tests/syntax_tests/data/printer/signature/type.resi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ type color =
66
| Red
77
| Blue
88
| Black
9+
10+
type options = {
11+
permissions: {
12+
all: {
13+
stuff: bool
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)