@@ -1549,7 +1549,15 @@ and print_constructor_declarations ~state ~private_flag
15491549
15501550and print_constructor_declaration2 ~state i
15511551 (cd : Parsetree.constructor_declaration ) cmt_tbl =
1552- let attrs = print_attributes ~state cd.pcd_attributes cmt_tbl in
1552+ let comment_attrs, attrs =
1553+ ParsetreeViewer. partition_doc_comment_attributes cd.pcd_attributes
1554+ in
1555+ let comment_doc =
1556+ match comment_attrs with
1557+ | [] -> Doc. nil
1558+ | comment_attrs -> print_doc_comments ~state cmt_tbl comment_attrs
1559+ in
1560+ let attrs = print_attributes ~state attrs cmt_tbl in
15531561 let is_dot_dot_dot = cd.pcd_name.txt = " ..." in
15541562 let bar =
15551563 if i > 0 || cd.pcd_attributes <> [] || is_dot_dot_dot then Doc. text " | "
@@ -1572,6 +1580,7 @@ and print_constructor_declaration2 ~state i
15721580 Doc. concat
15731581 [
15741582 bar;
1583+ comment_doc;
15751584 Doc. group
15761585 (Doc. concat
15771586 [
@@ -1934,8 +1943,20 @@ and print_typ_expr ?inline_record_definitions ~(state : State.t)
19341943 let doc =
19351944 match typ_expr.ptyp_attributes with
19361945 | _ :: _ as attrs when not should_print_its_own_attributes ->
1937- Doc. group
1938- (Doc. concat [print_attributes ~state attrs cmt_tbl; rendered_type])
1946+ let doc_comment_attr, attrs =
1947+ ParsetreeViewer. partition_doc_comment_attributes attrs
1948+ in
1949+ let comment_doc =
1950+ match doc_comment_attr with
1951+ | [] -> Doc. nil
1952+ | _ -> print_doc_comments ~state ~sep: Doc. space cmt_tbl doc_comment_attr
1953+ in
1954+ let attrs_doc =
1955+ match attrs with
1956+ | [] -> Doc. nil
1957+ | _ -> print_attributes ~state attrs cmt_tbl
1958+ in
1959+ Doc. group (Doc. concat [comment_doc; attrs_doc; rendered_type])
19391960 | _ -> rendered_type
19401961 in
19411962 print_comments doc cmt_tbl typ_expr.ptyp_loc
@@ -5568,6 +5589,15 @@ and print_bs_object_row ~state (lbl, expr) cmt_tbl =
55685589 in
55695590 print_comments doc cmt_tbl cmt_loc
55705591
5592+ and print_doc_comments ~state ?(sep = Doc. hard_line) cmt_tbl attrs =
5593+ Doc. concat
5594+ [
5595+ Doc. group
5596+ (Doc. join_with_sep
5597+ (List. map (fun attr -> print_attribute ~state attr cmt_tbl) attrs));
5598+ sep;
5599+ ]
5600+
55715601(* The optional loc indicates whether we need to print the attributes in
55725602 * relation to some location. In practise this means the following:
55735603 * `@attr type t = string` -> on the same line, print on the same line
@@ -5579,6 +5609,9 @@ and print_attributes ?loc ?(inline = false) ~state
55795609 match ParsetreeViewer. filter_parsing_attrs attrs with
55805610 | [] -> Doc. nil
55815611 | attrs ->
5612+ let comment_attrs, attrs =
5613+ ParsetreeViewer. partition_doc_comment_attributes attrs
5614+ in
55825615 let line_break =
55835616 match loc with
55845617 | None -> Doc. line
@@ -5587,15 +5620,30 @@ and print_attributes ?loc ?(inline = false) ~state
55875620 | ({loc = first_loc}, _) :: _
55885621 when loc.loc_start.pos_lnum > first_loc.loc_end.pos_lnum ->
55895622 Doc. hard_line
5590- | _ -> Doc. line)
5623+ | _ ->
5624+ let has_comment_attrs = not (comment_attrs = [] ) in
5625+ if has_comment_attrs then Doc. space else Doc. line)
55915626 in
5592- Doc. concat
5593- [
5594- Doc. group
5595- (Doc. join_with_sep
5596- (List. map (fun attr -> print_attribute ~state attr cmt_tbl) attrs));
5597- (if inline then Doc. space else line_break);
5598- ]
5627+ let comment_doc =
5628+ match comment_attrs with
5629+ | [] -> Doc. nil
5630+ | comment_attrs -> print_doc_comments ~state cmt_tbl comment_attrs
5631+ in
5632+ let attrs_doc =
5633+ match attrs with
5634+ | [] -> Doc. nil
5635+ | _ ->
5636+ Doc. concat
5637+ [
5638+ Doc. group
5639+ (Doc. join_with_sep
5640+ (List. map
5641+ (fun attr -> print_attribute ~state attr cmt_tbl)
5642+ attrs));
5643+ (if inline then Doc. space else line_break);
5644+ ]
5645+ in
5646+ Doc. concat [comment_doc; attrs_doc]
55995647
56005648and print_payload ~state (payload : Parsetree.payload ) cmt_tbl =
56015649 match payload with
0 commit comments