Skip to content

Commit 3c56097

Browse files
fix(grainfmt): Properly format comments within a record (#1435)
1 parent 4e4903f commit 3c56097

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

compiler/src/formatting/format.re

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,19 @@ and print_record =
14531453
};
14541454

14551455
let after_brace_comments =
1456-
Comment_utils.get_after_brace_comments(~loc=recloc, comments);
1456+
switch (fields) {
1457+
| [field, ..._] =>
1458+
let (ident, expr) = field;
1459+
1460+
Comment_utils.get_after_brace_comments(
1461+
~loc=recloc,
1462+
~first=ident.loc,
1463+
comments,
1464+
);
1465+
1466+
| _ => Comment_utils.get_after_brace_comments(~loc=recloc, comments) // let s = {} is not legal syntax, but we can use all the comments
1467+
};
1468+
14571469
let cleaned_comments =
14581470
remove_used_comments(~remove_comments=after_brace_comments, comments);
14591471

compiler/test/formatter_inputs/records.gr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,16 @@ let { // a comment 3
6060
l2,
6161
l3
6262
} = x
63+
64+
let s= { num: 1, var: A, // end line comment
65+
str: "" }
66+
67+
68+
let s= { num: 1,
69+
var: A, // end line comment
70+
str: "" }
71+
72+
let s= { // comment 1
73+
// comment 2
74+
num: 1, var: A, // end line comment
75+
str: "" }

compiler/test/formatter_outputs/records.gr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,22 @@ let { // a comment 3
7575
l2,
7676
l3,
7777
} = x
78+
79+
let s = {
80+
num: 1,
81+
var: A, // end line comment
82+
str: "",
83+
}
84+
85+
let s = {
86+
num: 1,
87+
var: A, // end line comment
88+
str: "",
89+
}
90+
91+
let s = { // comment 1
92+
// comment 2
93+
num: 1,
94+
var: A, // end line comment
95+
str: "",
96+
}

0 commit comments

Comments
 (0)