Skip to content

Commit 97ae553

Browse files
committed
Refactor
1 parent a7a4057 commit 97ae553

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
@@ -5601,6 +5578,15 @@ and print_bs_object_row ~state (lbl, expr) cmt_tbl =
56015578
in
56025579
print_comments doc cmt_tbl cmt_loc
56035580

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

0 commit comments

Comments
 (0)