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

Unify comment formatting inside the empty blocks #647

Merged
merged 21 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/res_doc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ let equal = Text "="
let trailingComma = ifBreaks comma nil
let doubleQuote = Text "\""

let isNil = function
| Nil -> true
| _ -> false

let propagateForcedBreaks doc =
let rec walk doc =
match doc with
Expand Down
2 changes: 0 additions & 2 deletions src/res_doc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ val equal : t
val trailingComma : t
val doubleQuote : t [@@live]

val isNil : t -> bool

(*
* `willBreak doc` checks whether `doc` contains forced line breaks.
* This is more or less a "workaround" to make the parent of a `customLayout` break.
Expand Down
95 changes: 57 additions & 38 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ let hasNestedJsxOrMoreThanOneChild expr =
in
loop false expr

let hasCommentsInside tbl loc =
match Hashtbl.find tbl.CommentTable.inside loc with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use find_opt

| [] -> false
| _ -> true
| exception Not_found -> false

let printMultilineCommentContent txt =
(* Turns
* |* first line
Expand Down Expand Up @@ -236,10 +242,10 @@ let printCommentsInside cmtTbl loc =
if singleLine then Doc.text ("//" ^ txt)
else printMultilineCommentContent txt
in
let forceBreak =
loc.Location.loc_start.pos_lnum <> loc.Location.loc_end.pos_lnum
in
let rec loop acc comments =
let forceBreak =
loc.Location.loc_start.pos_lnum <> loc.Location.loc_end.pos_lnum
in
match comments with
| [] -> Doc.nil
| [comment] ->
Expand All @@ -258,10 +264,10 @@ let printCommentsInside cmtTbl loc =
| exception Not_found -> Doc.nil
| comments ->
Hashtbl.remove cmtTbl.inside loc;
Doc.group (loop [] comments)
loop [] comments

(* This function is used for printing comments inside an empty file *)
let printCommentsInsideFile cmtTbl loc =
let printCommentsInsideFile cmtTbl =
let rec loop acc comments =
match comments with
| [] -> Doc.nil
Expand All @@ -275,10 +281,10 @@ let printCommentsInsideFile cmtTbl loc =
let cmtDoc = printLeadingComment ~nextComment comment in
loop (cmtDoc :: acc) rest
in
match Hashtbl.find cmtTbl.CommentTable.inside loc with
match Hashtbl.find cmtTbl.CommentTable.inside Location.none with
| exception Not_found -> Doc.nil
| comments ->
Hashtbl.remove cmtTbl.inside loc;
Hashtbl.remove cmtTbl.inside Location.none;
Doc.group (loop [] comments)

let printLeadingComments node tbl loc =
Expand Down Expand Up @@ -566,7 +572,7 @@ let customLayoutThreshold = 2

let rec printStructure ~customLayout (s : Parsetree.structure) t =
match s with
| [] -> printCommentsInsideFile t Location.none
| [] -> printCommentsInsideFile t
| structure ->
printList
~getLoc:(fun s -> s.Parsetree.pstr_loc)
Expand Down Expand Up @@ -738,15 +744,16 @@ and printModType ~customLayout modType cmtTbl =
printLongidentLocation longident cmtTbl;
]
| Pmty_signature [] ->
let doc = printCommentsInside cmtTbl modType.pmty_loc in
if Doc.isNil doc then
if hasCommentsInside cmtTbl modType.pmty_loc then
let doc = printCommentsInside cmtTbl modType.pmty_loc in
Doc.concat [Doc.lbrace; doc; Doc.rbrace]
else
let shouldBreak =
modType.pmty_loc.loc_start.pos_lnum
< modType.pmty_loc.loc_end.pos_lnum
in
Doc.breakableGroup ~forceBreak:shouldBreak
(Doc.concat [Doc.lbrace; Doc.softLine; Doc.softLine; Doc.rbrace])
else Doc.concat [Doc.lbrace; doc; Doc.rbrace]
| Pmty_signature signature ->
let signatureDoc =
Doc.breakableGroup ~forceBreak:true
Expand Down Expand Up @@ -935,7 +942,7 @@ and printWithConstraint ~customLayout

and printSignature ~customLayout signature cmtTbl =
match signature with
| [] -> printCommentsInsideFile cmtTbl Location.none
| [] -> printCommentsInsideFile cmtTbl
| signature ->
printList
~getLoc:(fun s -> s.Parsetree.psig_loc)
Expand Down Expand Up @@ -3974,32 +3981,49 @@ and printJsxExpression ~customLayout lident args cmtTbl =
let name = printJsxName lident in
let formattedProps, children = printJsxProps ~customLayout args cmtTbl in
(* <div className="test" /> *)
let insideComments =
let hasChildren =
match children with
| Some
{
Parsetree.pexp_desc =
Pexp_construct ({txt = Longident.Lident "[]"}, None);
pexp_loc = loc;
} ->
printCommentsInside cmtTbl loc
| _ -> Doc.nil
false
| None -> false
| _ -> true
in
let isSelfClosing =
match children with
| Some
{
Parsetree.pexp_desc =
Pexp_construct ({txt = Longident.Lident "[]"}, None);
pexp_loc = loc;
} ->
Doc.isNil insideComments
not (hasCommentsInside cmtTbl loc)
| _ -> false
in
let lineSep =
match children with
| Some expr ->
if hasNestedJsxOrMoreThanOneChild expr then Doc.hardLine else Doc.line
| None -> Doc.line
let printChildren children =
let lineSep =
match children with
| Some expr ->
if hasNestedJsxOrMoreThanOneChild expr then Doc.hardLine else Doc.line
| None -> Doc.line
in
Doc.concat
[
Doc.indent
(Doc.concat
[
Doc.line;
(match children with
| Some childrenExpression ->
printJsxChildren ~customLayout childrenExpression ~sep:lineSep
cmtTbl
| None -> Doc.nil);
]);
lineSep;
]
in
Doc.group
(Doc.concat
Expand Down Expand Up @@ -4030,22 +4054,17 @@ and printJsxExpression ~customLayout lident args cmtTbl =
Doc.concat
[
Doc.greaterThan;
(if Doc.isNil insideComments then
Doc.concat
[
Doc.indent
(Doc.concat
[
Doc.line;
(match children with
| Some childrenExpression ->
printJsxChildren ~customLayout childrenExpression
~sep:lineSep cmtTbl
| None -> Doc.nil);
]);
lineSep;
]
else insideComments);
(if hasChildren then printChildren children
else
match children with
| Some
{
Parsetree.pexp_desc =
Pexp_construct ({txt = Longident.Lident "[]"}, None);
pexp_loc = loc;
} ->
printCommentsInside cmtTbl loc
| _ -> Doc.nil);
Doc.text "</";
name;
Doc.greaterThan;
Expand Down