Skip to content

Commit 78d8f58

Browse files
committed
Refactor
1 parent 4565f26 commit 78d8f58

File tree

3 files changed

+32
-50
lines changed

3 files changed

+32
-50
lines changed

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,23 @@ let filter_printable_attributes attrs = List.filter is_printable_attribute attrs
567567
let partition_printable_attributes attrs =
568568
List.partition is_printable_attribute attrs
569569

570+
let partition_doc_comment_attributes attrs =
571+
List.partition
572+
(fun ((id, payload) : Parsetree.attribute) ->
573+
match (id, payload) with
574+
| ( {txt = "res.doc"},
575+
PStr
576+
[
577+
{
578+
pstr_desc =
579+
Pstr_eval
580+
({pexp_desc = Pexp_constant (Pconst_string (_, _))}, _);
581+
};
582+
] ) ->
583+
true
584+
| _ -> false)
585+
attrs
586+
570587
let is_fun_newtype expr =
571588
match expr.pexp_desc with
572589
| Pexp_fun _ | Pexp_newtype _ -> true

compiler/syntax/src/res_parsetree_viewer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ val has_printable_attributes : Parsetree.attributes -> bool
9898
val filter_printable_attributes : Parsetree.attributes -> Parsetree.attributes
9999
val partition_printable_attributes :
100100
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes
101+
val partition_doc_comment_attributes :
102+
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes
101103

102104
val requires_special_callback_printing_last_arg :
103105
(Asttypes.arg_label * Parsetree.expression) list -> bool

compiler/syntax/src/res_printer.ml

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,35 +1550,12 @@ and print_constructor_declarations ~state ~private_flag
15501550
and print_constructor_declaration2 ~state i
15511551
(cd : Parsetree.constructor_declaration) cmt_tbl =
15521552
let comment_attrs, attrs =
1553-
List.partition
1554-
(fun ((id, payload) : Parsetree.attribute) ->
1555-
match (id, payload) with
1556-
| ( {txt = "res.doc"},
1557-
PStr
1558-
[
1559-
{
1560-
pstr_desc =
1561-
Pstr_eval
1562-
({pexp_desc = Pexp_constant (Pconst_string (_, _))}, _);
1563-
};
1564-
] ) ->
1565-
true
1566-
| _ -> false)
1567-
cd.pcd_attributes
1553+
ParsetreeViewer.partition_doc_comment_attributes cd.pcd_attributes
15681554
in
15691555
let comment_doc =
15701556
match comment_attrs with
15711557
| [] -> Doc.nil
1572-
| comment_attrs ->
1573-
Doc.concat
1574-
[
1575-
Doc.group
1576-
(Doc.join_with_sep
1577-
(List.map
1578-
(fun attr -> print_attribute ~state attr cmt_tbl)
1579-
comment_attrs));
1580-
Doc.hard_line;
1581-
]
1558+
| comment_attrs -> print_doc_comments ~state cmt_tbl comment_attrs
15821559
in
15831560
let attrs = print_attributes ~state attrs cmt_tbl in
15841561
let is_dot_dot_dot = cd.pcd_name.txt = "..." in
@@ -5600,6 +5577,15 @@ and print_bs_object_row ~state (lbl, expr) cmt_tbl =
56005577
in
56015578
print_comments doc cmt_tbl cmt_loc
56025579

5580+
and print_doc_comments ~state cmt_tbl attrs =
5581+
Doc.concat
5582+
[
5583+
Doc.group
5584+
(Doc.join_with_sep
5585+
(List.map (fun attr -> print_attribute ~state attr cmt_tbl) attrs));
5586+
Doc.hard_line;
5587+
]
5588+
56035589
(* The optional loc indicates whether we need to print the attributes in
56045590
* relation to some location. In practise this means the following:
56055591
* `@attr type t = string` -> on the same line, print on the same line
@@ -5612,21 +5598,7 @@ and print_attributes ?loc ?(inline = false) ~state
56125598
| [] -> Doc.nil
56135599
| attrs ->
56145600
let comment_attrs, attrs =
5615-
List.partition
5616-
(fun ((id, payload) : Parsetree.attribute) ->
5617-
match (id, payload) with
5618-
| ( {txt = "res.doc"},
5619-
PStr
5620-
[
5621-
{
5622-
pstr_desc =
5623-
Pstr_eval
5624-
({pexp_desc = Pexp_constant (Pconst_string (_, _))}, _);
5625-
};
5626-
] ) ->
5627-
true
5628-
| _ -> false)
5629-
attrs
5601+
ParsetreeViewer.partition_doc_comment_attributes attrs
56305602
in
56315603
let line_break =
56325604
match loc with
@@ -5641,16 +5613,7 @@ and print_attributes ?loc ?(inline = false) ~state
56415613
let comment_doc =
56425614
match comment_attrs with
56435615
| [] -> Doc.nil
5644-
| comment_attrs ->
5645-
Doc.concat
5646-
[
5647-
Doc.group
5648-
(Doc.join_with_sep
5649-
(List.map
5650-
(fun attr -> print_attribute ~state attr cmt_tbl)
5651-
comment_attrs));
5652-
Doc.hard_line;
5653-
]
5616+
| comment_attrs -> print_doc_comments ~state cmt_tbl comment_attrs
56545617
in
56555618
let attrs_doc =
56565619
match attrs with

0 commit comments

Comments
 (0)