Skip to content

Commit 654ab8c

Browse files
marcusrobertsMarcus Roberts
andauthored
fix(grainfmt): Handle comments after braces properly when on one line (#1578)
fix(grainfmt): Handle comments after braces properly Co-authored-by: Marcus Roberts <marcus@marcusr.com>
1 parent 060fc7b commit 654ab8c

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

compiler/src/formatting/comment_utils.re

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,20 @@ let get_after_brace_comments =
300300
switch (first) {
301301
| None => cmts
302302
| Some(leading) =>
303-
let fstclog = Locations.get_comment_loc(fst);
304-
305-
let (_, itemstartline, itemstartc, _) =
303+
let (_, firststartline, firststartc, _) =
306304
Locations.get_raw_pos_info(leading.loc_start);
307305

308-
if (itemstartline > startline) {
309-
cmts;
310-
} else {
311-
let (_, cmtstartline, cmtstartc, _) =
312-
Locations.get_raw_pos_info(fstclog.loc_start);
313-
if (cmtstartline >= itemstartline && cmtstartc > itemstartc) {
314-
[];
315-
} else {
316-
cmts;
317-
};
318-
};
306+
List.filter(
307+
cmt => {
308+
let cmt_loc = Locations.get_comment_loc(cmt);
309+
let (_, cmtendline, cmtendc, _) =
310+
Locations.get_raw_pos_info(cmt_loc.loc_end);
311+
cmtendline < firststartline
312+
|| cmtendline == firststartline
313+
&& cmtendc <= firststartc;
314+
},
315+
cmts,
316+
);
319317
}
320318
};
321319
};

compiler/test/formatter_inputs/records.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ let { // a comment 3
9292
// comment 2
9393
num: 1, var: A, // end line comment
9494
str: "" }
95+
96+
let y = {/* comment 1 */x, /* comment 2 */longlonglongnamenamename2: 12345, /* comment 3 */ longlonglongnamenamename3: 12345 /* comment 4 */} // end line comment

compiler/test/formatter_outputs/records.gr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,9 @@ let s = { // comment 1
110110
var: A, // end line comment
111111
str: "",
112112
}
113+
114+
let y = { /* comment 1 */
115+
x, /* comment 2 */
116+
longlonglongnamenamename2: 12345, /* comment 3 */
117+
longlonglongnamenamename3: 12345, /* comment 4 */
118+
} // end line comment

0 commit comments

Comments
 (0)