Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit ffff692

Browse files
committed
Print hard line after doc comment.
Fixes #639.
1 parent 557b5fc commit ffff692

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#### :bug: Bug Fix
2424

25+
- Fix pretty printer where it would print doc comments on the same line as other attributes.
2526
- Fix location issue in error messages with JSX V4 where the body of the component is an application https://github.com/rescript-lang/syntax/pull/633
2627
- Fix issue where the printer would omit attributes for `->` and `|>` https://github.com/rescript-lang/syntax/pull/629
2728
- Fix printing of optional fields in records https://github.com/rescript-lang/rescript-compiler/issues/5654

src/res_doc.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ let join ~sep docs =
133133
in
134134
concat (loop [] sep docs)
135135

136+
let joinWithSep docsWithSep =
137+
let rec loop acc docs =
138+
match docs with
139+
| [] -> List.rev acc
140+
| [(x, _sep)] -> List.rev (x :: acc)
141+
| (x, sep) :: xs -> loop (sep :: x :: acc) xs
142+
in
143+
concat (loop [] docsWithSep)
144+
136145
let fits w stack =
137146
let width = ref w in
138147
let result = ref None in

src/res_doc.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ val customLayout : t list -> t
2020
val breakParent : t
2121
val join : sep:t -> t list -> t
2222

23+
(* [(doc1, sep1); (doc2,sep2)] joins as doc1 sep1 doc2 *)
24+
val joinWithSep : (t * t) list -> t
25+
2326
val space : t
2427
val comma : t
2528
val dot : t

src/res_printer.ml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ and printStructureItem ~customLayout (si : Parsetree.structure_item) cmtTbl =
569569
in
570570
Doc.concat [printAttributes ~customLayout attrs cmtTbl; exprDoc]
571571
| Pstr_attribute attr ->
572-
printAttribute ~customLayout ~standalone:true attr cmtTbl
572+
fst (printAttribute ~customLayout ~standalone:true attr cmtTbl)
573573
| Pstr_extension (extension, attrs) ->
574574
Doc.concat
575575
[
@@ -940,7 +940,7 @@ and printSignatureItem ~customLayout (si : Parsetree.signature_item) cmtTbl =
940940
| Psig_include includeDescription ->
941941
printIncludeDescription ~customLayout includeDescription cmtTbl
942942
| Psig_attribute attr ->
943-
printAttribute ~customLayout ~standalone:true attr cmtTbl
943+
fst (printAttribute ~customLayout ~standalone:true attr cmtTbl)
944944
| Psig_extension (extension, attrs) ->
945945
Doc.concat
946946
[
@@ -5033,7 +5033,7 @@ and printAttributes ?loc ?(inline = false) ~customLayout
50335033
Doc.concat
50345034
[
50355035
Doc.group
5036-
(Doc.join ~sep:Doc.line
5036+
(Doc.joinWithSep
50375037
(List.map
50385038
(fun attr -> printAttribute ~customLayout attr cmtTbl)
50395039
attrs));
@@ -5134,20 +5134,22 @@ and printAttribute ?(standalone = false) ~customLayout
51345134
Pstr_eval ({pexp_desc = Pexp_constant (Pconst_string (txt, _))}, _);
51355135
};
51365136
] ) ->
5137-
Doc.concat
5138-
[
5139-
Doc.text (if standalone then "/***" else "/**");
5140-
Doc.text txt;
5141-
Doc.text "*/";
5142-
]
5137+
( Doc.concat
5138+
[
5139+
Doc.text (if standalone then "/***" else "/**");
5140+
Doc.text txt;
5141+
Doc.text "*/";
5142+
],
5143+
Doc.hardLine )
51435144
| _ ->
5144-
Doc.group
5145-
(Doc.concat
5146-
[
5147-
Doc.text (if standalone then "@@" else "@");
5148-
Doc.text (convertBsExternalAttribute id.txt);
5149-
printPayload ~customLayout payload cmtTbl;
5150-
])
5145+
( Doc.group
5146+
(Doc.concat
5147+
[
5148+
Doc.text (if standalone then "@@" else "@");
5149+
Doc.text (convertBsExternalAttribute id.txt);
5150+
printPayload ~customLayout payload cmtTbl;
5151+
]),
5152+
Doc.line )
51515153

51525154
and printModExpr ~customLayout modExpr cmtTbl =
51535155
let doc =

tests/printer/comments/expected/docComments.res.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ type h = int
1919
/* comment and attribute */
2020
@foo let x = 10
2121

22-
/** doc comment and attribute */ @foo let x = 10
22+
/** doc comment and attribute */
23+
@foo
24+
let x = 10

0 commit comments

Comments
 (0)