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
Prev Previous commit
Next Next commit
refactor printcommentsinside
  • Loading branch information
ah-yu committed Sep 22, 2022
commit 115563a7735c5a82371e82afd21240148c7de5c8
5 changes: 2 additions & 3 deletions src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,7 @@ and walkTypeDeclaration (td : Parsetree.type_declaration) t comments =
| Ptype_abstract | Ptype_open -> rest
| Ptype_record labelDeclarations ->
let () =
if List.length labelDeclarations = 0 then
attach t.inside td.ptype_loc rest
if labelDeclarations = [] then attach t.inside td.ptype_loc rest
else
walkList
(labelDeclarations |> List.map (fun ld -> LabelDeclaration ld))
Expand Down Expand Up @@ -1026,7 +1025,7 @@ and walkExpression expr t comments =
| Pexp_array exprs | Pexp_tuple exprs ->
walkList (exprs |> List.map (fun e -> Expression e)) t comments
| Pexp_record (rows, spreadExpr) ->
if List.length rows = 0 then attach t.inside expr.pexp_loc comments
if rows = [] then attach t.inside expr.pexp_loc comments
else
let comments =
match spreadExpr with
Expand Down
77 changes: 43 additions & 34 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,41 @@ let printLeadingComment ?nextComment comment =
Doc.concat [content; separator]

let printCommentsInside cmtTbl loc =
let printComment ~isLast comment =
let singleLine = Comment.isSingleLineComment comment in
let content =
let txt = Comment.txt comment in
if singleLine then Doc.text ("//" ^ txt)
else printMultilineCommentContent txt
in
if isLast then
Doc.concat
[
Doc.indent (Doc.concat [Doc.softLine; Doc.breakParent; content]);
Doc.softLine;
]
else Doc.indent (Doc.concat [Doc.softLine; Doc.breakParent; content])
in
let rec loop acc comments =
match comments with
| [] -> Doc.nil
| [comment] ->
let cmtDoc = printComment comment ~isLast:true in
let doc =
Doc.group (Doc.concat [Doc.concat (List.rev (cmtDoc :: acc))])
in
doc
| comment :: rest ->
let cmtDoc = printComment comment ~isLast:false in
loop (cmtDoc :: acc) rest
in
match Hashtbl.find cmtTbl.CommentTable.inside loc with
| exception Not_found -> Doc.nil
| comments ->
Hashtbl.remove cmtTbl.inside loc;
Doc.group (loop [] comments)

let printCommentsForEmptyFile cmtTbl loc =
let rec loop acc comments =
match comments with
| [] -> Doc.nil
Expand Down Expand Up @@ -533,7 +568,7 @@ let customLayoutThreshold = 2

let rec printStructure ~customLayout (s : Parsetree.structure) t =
match s with
| [] -> printCommentsInside t Location.none
| [] -> printCommentsForEmptyFile t Location.none
| structure ->
printList
~getLoc:(fun s -> s.Parsetree.pstr_loc)
Expand Down Expand Up @@ -710,14 +745,7 @@ and printModType ~customLayout modType cmtTbl =
in
Doc.breakableGroup ~forceBreak:shouldBreak
(Doc.concat
[
Doc.lbrace;
Doc.indent
(Doc.concat
[Doc.softLine; printCommentsInside cmtTbl modType.pmty_loc]);
Doc.softLine;
Doc.rbrace;
])
[Doc.lbrace; printCommentsInside cmtTbl modType.pmty_loc; Doc.rbrace])
| Pmty_signature signature ->
let signatureDoc =
Doc.breakableGroup ~forceBreak:true
Expand Down Expand Up @@ -906,7 +934,7 @@ and printWithConstraint ~customLayout

and printSignature ~customLayout signature cmtTbl =
match signature with
| [] -> printCommentsInside cmtTbl Location.none
| [] -> printCommentsForEmptyFile cmtTbl Location.none
| signature ->
printList
~getLoc:(fun s -> s.Parsetree.psig_loc)
Expand Down Expand Up @@ -1231,7 +1259,7 @@ and printTypeDeclaration2 ~customLayout ~recFlag
Doc.text "..";
]
| Ptype_record lds ->
if List.length lds = 0 then
if lds = [] then
Doc.concat
[
Doc.space;
Expand Down Expand Up @@ -2247,13 +2275,7 @@ and printPattern ~customLayout (p : Parsetree.pattern) cmtTbl =
Doc.concat
[Doc.lparen; printCommentsInside cmtTbl ppat_loc; Doc.rparen]
| Some {ppat_desc = Ppat_tuple []; ppat_loc = loc} ->
Doc.concat
[
Doc.lparen;
Doc.softLine;
printCommentsInside cmtTbl loc;
Doc.rparen;
]
Doc.concat [Doc.lparen; printCommentsInside cmtTbl loc; Doc.rparen]
(* Some((1, 2) *)
| Some {ppat_desc = Ppat_tuple [({ppat_desc = Ppat_tuple _} as arg)]} ->
Doc.concat
Expand Down Expand Up @@ -2305,13 +2327,7 @@ and printPattern ~customLayout (p : Parsetree.pattern) cmtTbl =
->
Doc.text "()"
| Some {ppat_desc = Ppat_tuple []; ppat_loc = loc} ->
Doc.concat
[
Doc.lparen;
Doc.softLine;
printCommentsInside cmtTbl loc;
Doc.rparen;
]
Doc.concat [Doc.lparen; printCommentsInside cmtTbl loc; Doc.rparen]
(* Some((1, 2) *)
| Some {ppat_desc = Ppat_tuple [({ppat_desc = Ppat_tuple _} as arg)]} ->
Doc.concat
Expand Down Expand Up @@ -2857,7 +2873,7 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
in
Doc.group (Doc.concat [variantName; args])
| Pexp_record (rows, spreadExpr) ->
if List.length rows = 0 then
if rows = [] then
Doc.concat
[Doc.lbrace; printCommentsInside cmtTbl e.pexp_loc; Doc.rbrace]
else
Expand Down Expand Up @@ -5179,14 +5195,7 @@ and printModExpr ~customLayout modExpr cmtTbl =
in
Doc.breakableGroup ~forceBreak:shouldBreak
(Doc.concat
[
Doc.lbrace;
Doc.indent
(Doc.concat
[Doc.softLine; printCommentsInside cmtTbl modExpr.pmod_loc]);
Doc.softLine;
Doc.rbrace;
])
[Doc.lbrace; printCommentsInside cmtTbl modExpr.pmod_loc; Doc.rbrace])
| Pmod_structure structure ->
Doc.breakableGroup ~forceBreak:true
(Doc.concat
Expand Down