Skip to content

Commit be4b38d

Browse files
fix(grainfmt): Print comments found between comma-separated data statements (#1430)
1 parent 3c56097 commit be4b38d

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

compiler/src/formatting/format.re

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,19 +3742,37 @@ let data_print =
37423742
~comments: list(Parsetree.comment),
37433743
datas: list((Parsetree.export_flag, Parsetree.data_declaration)),
37443744
) => {
3745+
let previous_data: ref(option(Parsetree.data_declaration)) = ref(None);
37453746
Doc.join(
37463747
~sep=Doc.concat([Doc.comma, Doc.hardLine]),
37473748
List.map(
37483749
data => {
37493750
let (expt, decl: Parsetree.data_declaration) = data;
37503751

3752+
let leading_comments =
3753+
switch (previous_data^) {
3754+
| None => []
3755+
| Some(prev) =>
3756+
Comment_utils.get_comments_between_locations(
3757+
~loc1=prev.pdata_loc,
3758+
~loc2=decl.pdata_loc,
3759+
comments,
3760+
)
3761+
};
3762+
3763+
let leading_comment_docs =
3764+
Comment_utils.new_comments_to_docs(leading_comments);
3765+
37513766
let data_comments =
37523767
Comment_utils.get_comments_inside_location(
37533768
~location=decl.pdata_loc,
37543769
comments,
37553770
);
37563771

3772+
previous_data := Some(decl);
3773+
37573774
Doc.concat([
3775+
leading_comment_docs,
37583776
switch ((expt: Asttypes.export_flag)) {
37593777
| Nonexported => Doc.nil
37603778
| Exported => Doc.text("export ")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
enum EventType {
2+
Enter,
3+
Exit,
4+
},
5+
/**
6+
* An event is the start or end of a token amongst other events.
7+
* Tokens can “contain” other tokens, even though they are stored in a flat
8+
* list, through `enter`ing before them, and `exit`ing after them.
9+
*/
10+
type Event = (EventType, Token, TokenizeContext),
11+
/**
12+
* Another event is the start or end of a token amongst other events.
13+
* Tokens can “contain” other tokens, even though they are stored in a flat
14+
* list, through `enter`ing before them, and `exit`ing after them.
15+
*/
16+
17+
18+
enum EventType2 {
19+
Enter2,
20+
Exit2,
21+
},
22+
// line comment
23+
type Event2 = (EventType2, Token, TokenizeContext)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
enum EventType {
2+
Enter,
3+
Exit,
4+
},
5+
/**
6+
* An event is the start or end of a token amongst other events.
7+
* Tokens can “contain” other tokens, even though they are stored in a flat
8+
* list, through `enter`ing before them, and `exit`ing after them.
9+
*/
10+
type Event = (EventType, Token, TokenizeContext),
11+
/**
12+
* Another event is the start or end of a token amongst other events.
13+
* Tokens can “contain” other tokens, even though they are stored in a flat
14+
* list, through `enter`ing before them, and `exit`ing after them.
15+
*/
16+
enum EventType2 {
17+
Enter2,
18+
Exit2,
19+
},
20+
// line comment
21+
type Event2 = (EventType2, Token, TokenizeContext)

compiler/test/suites/formatter.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ describe("formatter", ({test, testSkip}) => {
4848
assertFormatOutput("rationals", "rationals");
4949
assertFormatOutput("constraints", "constraints");
5050
assertFormatOutput("only_comments", "only_comments");
51+
assertFormatOutput("data_docs", "data_docs");
5152
assertFormatOutput("custom_operators", "custom_operators");
5253
});

0 commit comments

Comments
 (0)