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
45 changes: 26 additions & 19 deletions src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,12 @@ and walkTypeDeclaration (td : Parsetree.type_declaration) t comments =
| Ptype_abstract | Ptype_open -> rest
| Ptype_record labelDeclarations ->
let () =
walkList
(labelDeclarations |> List.map (fun ld -> LabelDeclaration ld))
t rest
if List.length labelDeclarations = 0 then
attach t.inside td.ptype_loc rest
else
walkList
(labelDeclarations |> List.map (fun ld -> LabelDeclaration ld))
t rest
in
[]
| Ptype_variant constructorDeclarations ->
Expand Down Expand Up @@ -1023,22 +1026,26 @@ 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) ->
let comments =
match spreadExpr with
| None -> comments
| Some expr ->
let leading, inside, trailing = partitionByLoc comments expr.pexp_loc in
attach t.leading expr.pexp_loc leading;
walkExpression expr t inside;
let afterExpr, rest =
partitionAdjacentTrailing expr.pexp_loc trailing
in
attach t.trailing expr.pexp_loc afterExpr;
rest
in
walkList
(rows |> List.map (fun (li, e) -> ExprRecordRow (li, e)))
t comments
if List.length rows = 0 then attach t.inside expr.pexp_loc comments
else
let comments =
match spreadExpr with
| None -> comments
| Some expr ->
let leading, inside, trailing =
partitionByLoc comments expr.pexp_loc
in
attach t.leading expr.pexp_loc leading;
walkExpression expr t inside;
let afterExpr, rest =
partitionAdjacentTrailing expr.pexp_loc trailing
in
attach t.trailing expr.pexp_loc afterExpr;
rest
in
walkList
(rows |> List.map (fun (li, e) -> ExprRecordRow (li, e)))
t comments
| Pexp_field (expr, longident) ->
let leading, inside, trailing = partitionByLoc comments expr.pexp_loc in
let trailing =
Expand Down
155 changes: 85 additions & 70 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1231,23 +1231,34 @@ and printTypeDeclaration2 ~customLayout ~recFlag
Doc.text "..";
]
| Ptype_record lds ->
let manifest =
match td.ptype_manifest with
| None -> Doc.nil
| Some typ ->
Doc.concat
[
Doc.concat [Doc.space; Doc.text equalSign; Doc.space];
printTypExpr ~customLayout typ cmtTbl;
]
in
Doc.concat
[
manifest;
Doc.concat [Doc.space; Doc.text equalSign; Doc.space];
printPrivateFlag td.ptype_private;
printRecordDeclaration ~customLayout lds cmtTbl;
]
if List.length lds = 0 then
Doc.concat
[
Doc.space;
Doc.text equalSign;
Doc.space;
Doc.lbrace;
printCommentsInside cmtTbl td.ptype_loc;
Doc.rbrace;
]
else
let manifest =
match td.ptype_manifest with
| None -> Doc.nil
| Some typ ->
Doc.concat
[
Doc.concat [Doc.space; Doc.text equalSign; Doc.space];
printTypExpr ~customLayout typ cmtTbl;
]
in
Doc.concat
[
manifest;
Doc.concat [Doc.space; Doc.text equalSign; Doc.space];
printPrivateFlag td.ptype_private;
printRecordDeclaration ~customLayout lds cmtTbl;
]
| Ptype_variant cds ->
let manifest =
match td.ptype_manifest with
Expand Down Expand Up @@ -2846,59 +2857,63 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
in
Doc.group (Doc.concat [variantName; args])
| Pexp_record (rows, spreadExpr) ->
let spread =
match spreadExpr with
| None -> Doc.nil
| Some expr ->
Doc.concat
[
Doc.dotdotdot;
(let doc =
printExpressionWithComments ~customLayout expr cmtTbl
in
match Parens.expr expr with
| Parens.Parenthesized -> addParens doc
| Braced braces -> printBraces doc expr braces
| Nothing -> doc);
Doc.comma;
Doc.line;
]
in
(* If the record is written over multiple lines, break automatically
* `let x = {a: 1, b: 3}` -> same line, break when line-width exceeded
* `let x = {
* a: 1,
* b: 2,
* }` -> record is written on multiple lines, break the group *)
let forceBreak =
e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum
in
let punningAllowed =
match (spreadExpr, rows) with
| None, [_] -> false (* disallow punning for single-element records *)
| _ -> true
in
Doc.breakableGroup ~forceBreak
(Doc.concat
[
Doc.lbrace;
Doc.indent
(Doc.concat
[
Doc.softLine;
spread;
Doc.join
~sep:(Doc.concat [Doc.text ","; Doc.line])
(List.map
(fun row ->
printExpressionRecordRow ~customLayout row cmtTbl
punningAllowed)
rows);
]);
Doc.trailingComma;
Doc.softLine;
Doc.rbrace;
])
if List.length rows = 0 then
Doc.concat
[Doc.lbrace; printCommentsInside cmtTbl e.pexp_loc; Doc.rbrace]
else
let spread =
match spreadExpr with
| None -> Doc.nil
| Some expr ->
Doc.concat
[
Doc.dotdotdot;
(let doc =
printExpressionWithComments ~customLayout expr cmtTbl
in
match Parens.expr expr with
| Parens.Parenthesized -> addParens doc
| Braced braces -> printBraces doc expr braces
| Nothing -> doc);
Doc.comma;
Doc.line;
]
in
(* If the record is written over multiple lines, break automatically
* `let x = {a: 1, b: 3}` -> same line, break when line-width exceeded
* `let x = {
* a: 1,
* b: 2,
* }` -> record is written on multiple lines, break the group *)
let forceBreak =
e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum
in
let punningAllowed =
match (spreadExpr, rows) with
| None, [_] -> false (* disallow punning for single-element records *)
| _ -> true
in
Doc.breakableGroup ~forceBreak
(Doc.concat
[
Doc.lbrace;
Doc.indent
(Doc.concat
[
Doc.softLine;
spread;
Doc.join
~sep:(Doc.concat [Doc.text ","; Doc.line])
(List.map
(fun row ->
printExpressionRecordRow ~customLayout row cmtTbl
punningAllowed)
rows);
]);
Doc.trailingComma;
Doc.softLine;
Doc.rbrace;
])
| Pexp_extension extension -> (
match extension with
| ( {txt = "bs.obj" | "obj"},
Expand Down
5 changes: 5 additions & 0 deletions tests/printer/comments/expected/expr.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ let user = /* before */ {
/* c4 */ age /* c5 */: /* c6 */ 31 /* c7 */,
} // after

// Pexp_record with empty record
let user = /* before */ {/* comment1 */
Copy link
Contributor

Choose a reason for hiding this comment

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

The comments inside look a bit suboptimal, can we go for:

{
   /* comment1 */
   // comment2
 }

// comment2
} // after

// bs object sugar
let user = /* before */ {
// above name
Expand Down
4 changes: 4 additions & 0 deletions tests/printer/comments/expected/typexpr.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type t = /* c0 */ string /* c1 */ as 'x // after
// Ptyp_poly
type fn = {f: /* c0 */ 'a /* c1 */ 'b /* c2 */. /* c3 */ string /* c4 */}

type fn = {/* comment1 */
Copy link
Contributor

@IwanKaramazow IwanKaramazow Sep 22, 2022

Choose a reason for hiding this comment

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

Can we tweak the output to look more like the input:

type fn = {
   /* comment1 */
   // comment2
 }

// comment2
}

// Ptyp_arrow
type add = /* before */ (
/* c0 */ int /* c1 */,
Expand Down
5 changes: 5 additions & 0 deletions tests/printer/comments/expr.res
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ let user = /* before */ {
/* c4 */ age /* c5 */ : /* c6 */ 31 /* c7 */,
} // after

// Pexp_record with empty record
let user = /* before */ {
/* comment1 */
// comment2
} // after

// bs object sugar
let user = /* before */ {
Expand Down
5 changes: 5 additions & 0 deletions tests/printer/comments/typexpr.res
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type fn = {
f: /* c0 */ 'a /* c1 */ 'b /* c2*/ . /* c3 */ string /* c4 */
}

type fn = {
/* comment1 */
// comment2
}

// Ptyp_arrow
type add = /* before */ ( /* c0 */ int /* c1 */, /* c2 */ int /* c3 */) => /* before return */ int /* after */
type add = /* before */ ( /* c0 */ ~a:int /* c1 */, /* c2 */ ~b:int /* c3 */) => /* before return */ int /* after */
Expand Down