Skip to content

Commit 75e805d

Browse files
committed
Format doc comment before bar
1 parent 8af5eb5 commit 75e805d

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

compiler/syntax/src/res_core.ml

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4885,19 +4885,24 @@ and parse_constr_decl_args p =
48854885
* | constr-name const-args
48864886
* | attrs constr-name const-args *)
48874887
and parse_type_constructor_declaration_with_bar p =
4888-
let doc_comment_attrs = match p.Parser.token with
4889-
| DocComment (loc, s) ->
4888+
let doc_comment_attrs =
4889+
match p.Parser.token with
4890+
| DocComment (loc, s) ->
48904891
Parser.next p;
48914892
[doc_comment_to_attribute loc s]
4892-
| _ -> []
4893+
| _ -> []
48934894
in
48944895
match p.Parser.token with
48954896
| Bar ->
48964897
let start_pos = p.Parser.start_pos in
48974898
Parser.next p;
4898-
let () = print_endline "3" in
48994899
let constr = parse_type_constructor_declaration ~start_pos p in
4900-
Some ({constr with Parsetree.pcd_attributes = doc_comment_attrs @ constr.Parsetree.pcd_attributes})
4900+
Some
4901+
{
4902+
constr with
4903+
Parsetree.pcd_attributes =
4904+
doc_comment_attrs @ constr.Parsetree.pcd_attributes;
4905+
}
49014906
| _ -> None
49024907

49034908
and parse_type_constructor_declaration ~start_pos p =
@@ -4924,20 +4929,20 @@ and parse_type_constructor_declaration ~start_pos p =
49244929

49254930
(* [|] constr-decl { | constr-decl } *)
49264931
and parse_type_constructor_declarations ?first p =
4927-
let () = print_endline "2" in
49284932
let first_constr_decl =
49294933
match first with
49304934
| None ->
4931-
let doc_comment_attrs = match p.Parser.token with
4932-
| DocComment (loc, s) ->
4935+
let doc_comment_attrs =
4936+
match p.Parser.token with
4937+
| DocComment (loc, s) ->
49334938
Parser.next p;
49344939
[doc_comment_to_attribute loc s]
4935-
| _ -> []
4940+
| _ -> []
49364941
in
49374942
let start_pos = p.Parser.start_pos in
49384943
ignore (Parser.optional p Token.Bar);
49394944
let constr = parse_type_constructor_declaration ~start_pos p in
4940-
{constr with pcd_attributes = doc_comment_attrs @ constr.pcd_attributes}
4945+
{constr with pcd_attributes = doc_comment_attrs @ constr.pcd_attributes}
49414946
| Some first_constr_decl -> first_constr_decl
49424947
in
49434948
first_constr_decl
@@ -4964,7 +4969,6 @@ and parse_type_representation ?current_type_name_path ?inline_types_context p =
49644969
let kind =
49654970
match p.Parser.token with
49664971
| Bar | Uident _ | DocComment _ ->
4967-
let () = print_endline "1" in
49684972
Parsetree.Ptype_variant (parse_type_constructor_declarations p)
49694973
| Lbrace ->
49704974
Parsetree.Ptype_record
@@ -5518,7 +5522,6 @@ and parse_type_equation_and_representation ?current_type_name_path
55185522
p
55195523
| Private -> parse_private_eq_or_repr p
55205524
| Bar | DotDot | DocComment _ ->
5521-
let () = print_endline "xxx" in
55225525
(* DOCCOMMENT: Reached here if the first variant starts with |.
55235526
It is possible that the first variant may not have | (with multiple variants)
55245527
*)
@@ -5627,7 +5630,6 @@ and parse_type_definitions ~current_type_name_path ~inline_types_context ~attrs
56275630
* implemented for now. Needed to get a feel for the complexities of
56285631
* this territory of the grammar *)
56295632
and parse_type_definition_or_extension ~attrs p =
5630-
let () = print_endline "0" in
56315633
let start_pos = p.Parser.start_pos in
56325634
Parser.expect Token.Typ p;
56335635
let rec_flag =

compiler/syntax/src/res_printer.ml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ let print_listi ~get_loc ~nodes ~print ?(force_break = false) t =
352352
| Some comment -> (Comment.loc comment).loc_start
353353
in
354354
let sep =
355-
if start_pos.pos_lnum - prev_loc.loc_end.pos_lnum > 1 then
356-
Doc.concat [Doc.hard_line; Doc.hard_line]
355+
if start_pos.pos_lnum - prev_loc.loc_end.pos_lnum > 1 then Doc.hard_line
357356
else Doc.line
358357
in
359358
let doc = print_comments (print node t i) t loc in
@@ -1550,12 +1549,35 @@ and print_constructor_declarations ~state ~private_flag
15501549
and print_constructor_declaration2 ~state i
15511550
(cd : Parsetree.constructor_declaration) cmt_tbl =
15521551
let comment_attrs, attrs =
1553-
ParsetreeViewer.partition_doc_comment_attributes cd.pcd_attributes
1552+
List.partition
1553+
(fun ((id, payload) : Parsetree.attribute) ->
1554+
match (id, payload) with
1555+
| ( {txt = "res.doc"},
1556+
PStr
1557+
[
1558+
{
1559+
pstr_desc =
1560+
Pstr_eval
1561+
({pexp_desc = Pexp_constant (Pconst_string (_, _))}, _);
1562+
};
1563+
] ) ->
1564+
true
1565+
| _ -> false)
1566+
cd.pcd_attributes
15541567
in
15551568
let comment_doc =
15561569
match comment_attrs with
15571570
| [] -> Doc.nil
1558-
| comment_attrs -> print_doc_comments ~state cmt_tbl comment_attrs
1571+
| comment_attrs ->
1572+
Doc.concat
1573+
[
1574+
Doc.group
1575+
(Doc.join_with_sep
1576+
(List.map
1577+
(fun attr -> print_attribute ~state attr cmt_tbl)
1578+
comment_attrs));
1579+
Doc.hard_line;
1580+
]
15591581
in
15601582
let attrs = print_attributes ~state attrs cmt_tbl in
15611583
let is_dot_dot_dot = cd.pcd_name.txt = "..." in
@@ -1579,8 +1601,8 @@ and print_constructor_declaration2 ~state i
15791601
in
15801602
Doc.concat
15811603
[
1582-
bar;
15831604
comment_doc;
1605+
bar;
15841606
Doc.group
15851607
(Doc.concat
15861608
[

0 commit comments

Comments
 (0)