From 0f5f8dc6c82e28338154534d177c89476a3f1f08 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 19 Sep 2022 04:40:55 +0200 Subject: [PATCH 1/4] Add example of printing doc comments before attributes https://github.com/rescript-lang/syntax/issues/639 --- tests/printer/comments/docComments.res | 7 ++++++- tests/printer/comments/expected/docComments.res.txt | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/printer/comments/docComments.res b/tests/printer/comments/docComments.res index bd61bf39..c0550e59 100644 --- a/tests/printer/comments/docComments.res +++ b/tests/printer/comments/docComments.res @@ -14,4 +14,9 @@ let q = 11 * is a multi-line multiline doc comment */ -type h = int \ No newline at end of file +type h = int + +/** + doc comment and attributes + */ +@foo let x = 10 \ No newline at end of file diff --git a/tests/printer/comments/expected/docComments.res.txt b/tests/printer/comments/expected/docComments.res.txt index 4cfea8a7..b0b8d18e 100644 --- a/tests/printer/comments/expected/docComments.res.txt +++ b/tests/printer/comments/expected/docComments.res.txt @@ -15,3 +15,7 @@ let q = 11 multiline doc comment */ type h = int + +/** + external with doc comment + */ @foo let x = 10 From 0d6455fceffadd5488716a2ae691bfde95fe25cf Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 19 Sep 2022 04:43:59 +0200 Subject: [PATCH 2/4] Tests: normal comments are OK --- tests/printer/comments/docComments.res | 7 ++++--- tests/printer/comments/expected/docComments.res.txt | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/printer/comments/docComments.res b/tests/printer/comments/docComments.res index c0550e59..2039c8e0 100644 --- a/tests/printer/comments/docComments.res +++ b/tests/printer/comments/docComments.res @@ -16,7 +16,8 @@ let q = 11 */ type h = int -/** - doc comment and attributes - */ +/* comment and attribute */ +@foo let x = 10 + +/** doc comment and attribute */ @foo let x = 10 \ No newline at end of file diff --git a/tests/printer/comments/expected/docComments.res.txt b/tests/printer/comments/expected/docComments.res.txt index b0b8d18e..7866529c 100644 --- a/tests/printer/comments/expected/docComments.res.txt +++ b/tests/printer/comments/expected/docComments.res.txt @@ -16,6 +16,7 @@ let q = 11 */ type h = int -/** - external with doc comment - */ @foo let x = 10 +/* comment and attribute */ +@foo let x = 10 + +/** doc comment and attribute */ @foo let x = 10 From 03e3434930f090cb449e741f6280a8f01b3f37a6 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 19 Sep 2022 05:07:14 +0200 Subject: [PATCH 3/4] Print hard line after doc comment. Fixes https://github.com/rescript-lang/syntax/issues/639. --- CHANGELOG.md | 1 + src/res_doc.ml | 9 +++++ src/res_doc.mli | 3 ++ src/res_printer.ml | 34 ++++++++++--------- .../comments/expected/docComments.res.txt | 4 ++- 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d2efd4a..01e7297a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ #### :bug: Bug Fix +- Fix pretty printer where it would print doc comments on the same line as other attributes. - 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 - Fix issue where the printer would omit attributes for `->` and `|>` https://github.com/rescript-lang/syntax/pull/629 - Fix printing of optional fields in records https://github.com/rescript-lang/rescript-compiler/issues/5654 diff --git a/src/res_doc.ml b/src/res_doc.ml index f997f4e4..125ac772 100644 --- a/src/res_doc.ml +++ b/src/res_doc.ml @@ -133,6 +133,15 @@ let join ~sep docs = in concat (loop [] sep docs) +let joinWithSep docsWithSep = + let rec loop acc docs = + match docs with + | [] -> List.rev acc + | [(x, _sep)] -> List.rev (x :: acc) + | (x, sep) :: xs -> loop (sep :: x :: acc) xs + in + concat (loop [] docsWithSep) + let fits w stack = let width = ref w in let result = ref None in diff --git a/src/res_doc.mli b/src/res_doc.mli index cfb79fe3..f1a0c6ea 100644 --- a/src/res_doc.mli +++ b/src/res_doc.mli @@ -20,6 +20,9 @@ val customLayout : t list -> t val breakParent : t val join : sep:t -> t list -> t +(* [(doc1, sep1); (doc2,sep2)] joins as doc1 sep1 doc2 *) +val joinWithSep : (t * t) list -> t + val space : t val comma : t val dot : t diff --git a/src/res_printer.ml b/src/res_printer.ml index b6f2d9dd..1f4692dc 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -569,7 +569,7 @@ and printStructureItem ~customLayout (si : Parsetree.structure_item) cmtTbl = in Doc.concat [printAttributes ~customLayout attrs cmtTbl; exprDoc] | Pstr_attribute attr -> - printAttribute ~customLayout ~standalone:true attr cmtTbl + fst (printAttribute ~customLayout ~standalone:true attr cmtTbl) | Pstr_extension (extension, attrs) -> Doc.concat [ @@ -940,7 +940,7 @@ and printSignatureItem ~customLayout (si : Parsetree.signature_item) cmtTbl = | Psig_include includeDescription -> printIncludeDescription ~customLayout includeDescription cmtTbl | Psig_attribute attr -> - printAttribute ~customLayout ~standalone:true attr cmtTbl + fst (printAttribute ~customLayout ~standalone:true attr cmtTbl) | Psig_extension (extension, attrs) -> Doc.concat [ @@ -5033,7 +5033,7 @@ and printAttributes ?loc ?(inline = false) ~customLayout Doc.concat [ Doc.group - (Doc.join ~sep:Doc.line + (Doc.joinWithSep (List.map (fun attr -> printAttribute ~customLayout attr cmtTbl) attrs)); @@ -5134,20 +5134,22 @@ and printAttribute ?(standalone = false) ~customLayout Pstr_eval ({pexp_desc = Pexp_constant (Pconst_string (txt, _))}, _); }; ] ) -> - Doc.concat - [ - Doc.text (if standalone then "/***" else "/**"); - Doc.text txt; - Doc.text "*/"; - ] + ( Doc.concat + [ + Doc.text (if standalone then "/***" else "/**"); + Doc.text txt; + Doc.text "*/"; + ], + Doc.hardLine ) | _ -> - Doc.group - (Doc.concat - [ - Doc.text (if standalone then "@@" else "@"); - Doc.text (convertBsExternalAttribute id.txt); - printPayload ~customLayout payload cmtTbl; - ]) + ( Doc.group + (Doc.concat + [ + Doc.text (if standalone then "@@" else "@"); + Doc.text (convertBsExternalAttribute id.txt); + printPayload ~customLayout payload cmtTbl; + ]), + Doc.line ) and printModExpr ~customLayout modExpr cmtTbl = let doc = diff --git a/tests/printer/comments/expected/docComments.res.txt b/tests/printer/comments/expected/docComments.res.txt index 7866529c..bd437795 100644 --- a/tests/printer/comments/expected/docComments.res.txt +++ b/tests/printer/comments/expected/docComments.res.txt @@ -19,4 +19,6 @@ type h = int /* comment and attribute */ @foo let x = 10 -/** doc comment and attribute */ @foo let x = 10 +/** doc comment and attribute */ +@foo +let x = 10 From bc4f58c0a32b0766ad63022856fdf973a05ddb78 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 19 Sep 2022 05:09:33 +0200 Subject: [PATCH 4/4] More tests of doc comments and attributes. --- tests/printer/comments/docComments.res | 8 +++++++- tests/printer/comments/expected/docComments.res.txt | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/printer/comments/docComments.res b/tests/printer/comments/docComments.res index 2039c8e0..23430dc7 100644 --- a/tests/printer/comments/docComments.res +++ b/tests/printer/comments/docComments.res @@ -20,4 +20,10 @@ type h = int @foo let x = 10 /** doc comment and attribute */ -@foo let x = 10 \ No newline at end of file +@foo let x = 10 + +/** doc comment and 3 attributes */ +@foo @bar @baz let x = 10 + +/** doc comment and 0 attributes */ +let x = 10 \ No newline at end of file diff --git a/tests/printer/comments/expected/docComments.res.txt b/tests/printer/comments/expected/docComments.res.txt index bd437795..86a9cff2 100644 --- a/tests/printer/comments/expected/docComments.res.txt +++ b/tests/printer/comments/expected/docComments.res.txt @@ -22,3 +22,12 @@ type h = int /** doc comment and attribute */ @foo let x = 10 + +/** doc comment and 3 attributes */ +@foo +@bar +@baz +let x = 10 + +/** doc comment and 0 attributes */ +let x = 10