Skip to content

Commit b993030

Browse files
fix(grainfmt): Avoid duplicating comments inside records (#1428)
1 parent 5395fd4 commit b993030

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

compiler/src/formatting/format.re

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,11 +4112,22 @@ let rec print_data =
41124112
]);
41134113
};
41144114

4115+
let pre_brace_comments = []; // We can't determine from AST if comment comes before or after brace
4116+
4117+
let remaining_comments =
4118+
remove_used_comments(~remove_comments=pre_brace_comments, comments);
4119+
41154120
let after_brace_comments =
4116-
Comment_utils.get_after_brace_comments(~loc=data.pdata_loc, comments);
4121+
Comment_utils.get_after_brace_comments(
4122+
~loc=data.pdata_loc,
4123+
remaining_comments,
4124+
);
41174125

41184126
let cleaned_comments =
4119-
remove_used_comments(~remove_comments=after_brace_comments, comments);
4127+
remove_used_comments(
4128+
~remove_comments=after_brace_comments,
4129+
remaining_comments,
4130+
);
41204131

41214132
let decl_items =
41224133
item_iterator(
@@ -4142,22 +4153,11 @@ let rec print_data =
41424153
print_type(~original_source, ~comments, t);
41434154
};
41444155

4145-
let after_angle_comments =
4146-
Comment_utils.get_after_brace_comments(
4147-
~loc=get_loc(first),
4148-
comments,
4149-
);
4150-
let cleaned_comments =
4151-
remove_used_comments(
4152-
~remove_comments=after_angle_comments,
4153-
comments,
4154-
);
4155-
41564156
let param_items =
41574157
item_iterator(
41584158
~get_loc,
41594159
~print_item,
4160-
~comments=cleaned_comments,
4160+
~comments=[],
41614161
~iterated_item=IteratedRecordData,
41624162
data.pdata_params,
41634163
);
@@ -4166,14 +4166,14 @@ let rec print_data =
41664166
Doc.concat([
41674167
force_break_if_line_comment(
41684168
~separator=Doc.softLine,
4169-
after_angle_comments,
4169+
pre_brace_comments,
41704170
),
41714171
printed_param_items,
41724172
]);
41734173
Doc.group(
41744174
Doc.concat([
41754175
Doc.text("<"),
4176-
Comment_utils.single_line_of_comments(after_angle_comments),
4176+
Comment_utils.single_line_of_comments(pre_brace_comments),
41774177
Doc.indent(printed_params_after_angle),
41784178
Doc.softLine,
41794179
Doc.text(">"),

compiler/test/formatter_inputs/records.gr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Set from "set"
2+
13
record Rec {foo: Number, bar: Number}
24
let x = {foo: 4, bar: 9}
35

@@ -27,6 +29,23 @@ record Commented { // brace comment
2729
// trailing
2830
}
2931

32+
33+
// Some Comments everywhere
34+
record GraphData<a> { // comment 1
35+
// comment 2
36+
edge: Set.Set<(a,a)>, // comment 3
37+
// comment 6
38+
nodes: Set.Set<a>, // comment10
39+
// comment 5
40+
} // comment 7
41+
42+
record GraphData2<a> {
43+
44+
edge: Set.Set<(a,a)>, // comment 3
45+
46+
nodes: Set.Set<a>,
47+
}
48+
3049
record Bucket<k, v> {
3150
mut key: k,
3251
mut value: v,

compiler/test/formatter_outputs/records.gr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Set from "set"
2+
13
record Rec {
24
foo: Number,
35
bar: Number,
@@ -42,6 +44,20 @@ record Commented { // brace comment
4244
// trailing
4345
}
4446

47+
// Some Comments everywhere
48+
record GraphData<a> { // comment 1
49+
// comment 2
50+
edge: Set.Set<(a, a)>, // comment 3
51+
// comment 6
52+
nodes: Set.Set<a>, // comment10
53+
// comment 5
54+
} // comment 7
55+
56+
record GraphData2<a> {
57+
edge: Set.Set<(a, a)>, // comment 3
58+
nodes: Set.Set<a>,
59+
}
60+
4561
record Bucket<k, v> {
4662
mut key: k,
4763
mut value: v,

0 commit comments

Comments
 (0)