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/docComments.res b/tests/printer/comments/docComments.res index bd61bf39..23430dc7 100644 --- a/tests/printer/comments/docComments.res +++ b/tests/printer/comments/docComments.res @@ -14,4 +14,16 @@ let q = 11 * is a multi-line multiline doc comment */ -type h = int \ No newline at end of file +type h = int + +/* comment and attribute */ +@foo let x = 10 + +/** 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 \ 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..86a9cff2 100644 --- a/tests/printer/comments/expected/docComments.res.txt +++ b/tests/printer/comments/expected/docComments.res.txt @@ -15,3 +15,19 @@ let q = 11 multiline doc comment */ type h = int + +/* comment and attribute */ +@foo let x = 10 + +/** 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