From 00cff3f6c5c09839d096b05157b8f19c0c692393 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Sun, 16 Oct 2022 10:54:57 +0800 Subject: [PATCH] put > on a new line if having trailing comments --- src/res_printer.ml | 37 +++++++-------------- tests/printer/comments/expected/jsx.res.txt | 6 ++-- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/res_printer.ml b/src/res_printer.ml index 07c584a9..8a0bf8cc 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -113,26 +113,16 @@ let hasNestedJsxOrMoreThanOneChild expr = in loop false expr -let hasTailSingleLineComment tbl loc = - let rec getLastElement elements = - match elements with - | [] -> None - | [element] -> Some element - | _ :: rest -> getLastElement rest - in - match Hashtbl.find_opt tbl.CommentTable.trailing loc with - | None -> false - | Some comments -> ( - let lastComment = getLastElement comments in - match lastComment with - | None -> false - | Some comment -> Comment.isSingleLineComment comment) - let hasCommentsInside tbl loc = match Hashtbl.find_opt tbl.CommentTable.inside loc with | None -> false | _ -> true +let hasTrailingComments tbl loc = + match Hashtbl.find_opt tbl.CommentTable.trailing loc with + | None -> false + | _ -> true + let printMultilineCommentContent txt = (* Turns * |* first line @@ -4058,18 +4048,15 @@ and printJsxExpression ~customLayout lident args cmtTbl = when isSelfClosing -> Doc.text "/>" | _ -> - (* if last trailing comment of tag is single line comment then put > on the next line + (* if tag A has trailing comments then put > on the next line *) - if hasTailSingleLineComment cmtTbl lident.Asttypes.loc then + if hasTrailingComments cmtTbl lident.Asttypes.loc then Doc.concat [Doc.softLine; Doc.greaterThan] - else - Doc.ifBreaks - (Doc.lineSuffix Doc.greaterThan) - Doc.greaterThan); + else Doc.greaterThan); ]); (if isSelfClosing then Doc.nil else @@ -4216,7 +4203,7 @@ and printJsxProps ~customLayout args cmtTbl : {loc with loc_end = expr.pexp_loc.loc_end} | _ -> expr.pexp_loc in - let tailSingleLineCommentPresent = hasTailSingleLineComment cmtTbl loc in + let trailingCommentsPresent = hasTrailingComments cmtTbl loc in let propDoc = printJsxProp ~customLayout lastProp cmtTbl in let formattedProps = Doc.concat @@ -4228,8 +4215,8 @@ and printJsxProps ~customLayout args cmtTbl : Doc.group (Doc.join ~sep:Doc.line (propDoc :: props |> List.rev)); ]); - (* print > on new line if last comment is single line comment *) - (match (isSelfClosing children, tailSingleLineCommentPresent) with + (* print > on new line if the last prop has trailing comments *) + (match (isSelfClosing children, trailingCommentsPresent) with (* we always put /> on a new line when a self-closing tag breaks *) | true, _ -> Doc.line | false, true -> Doc.softLine diff --git a/tests/printer/comments/expected/jsx.res.txt b/tests/printer/comments/expected/jsx.res.txt index 8fbf20d4..98f1bf3e 100644 --- a/tests/printer/comments/expected/jsx.res.txt +++ b/tests/printer/comments/expected/jsx.res.txt @@ -33,7 +33,8 @@ module Cite = { + /* comment */ +> @@ -44,7 +45,8 @@ module Cite = { +/* comment */ +>