Skip to content

Commit

Permalink
fix(grainfmt): Print comments found between comma-separated data stat…
Browse files Browse the repository at this point in the history
…ements (#1430)
  • Loading branch information
marcusroberts authored Sep 15, 2022
1 parent 3c56097 commit be4b38d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions compiler/src/formatting/format.re
Original file line number Diff line number Diff line change
Expand Up @@ -3742,19 +3742,37 @@ let data_print =
~comments: list(Parsetree.comment),
datas: list((Parsetree.export_flag, Parsetree.data_declaration)),
) => {
let previous_data: ref(option(Parsetree.data_declaration)) = ref(None);
Doc.join(
~sep=Doc.concat([Doc.comma, Doc.hardLine]),
List.map(
data => {
let (expt, decl: Parsetree.data_declaration) = data;

let leading_comments =
switch (previous_data^) {
| None => []
| Some(prev) =>
Comment_utils.get_comments_between_locations(
~loc1=prev.pdata_loc,
~loc2=decl.pdata_loc,
comments,
)
};

let leading_comment_docs =
Comment_utils.new_comments_to_docs(leading_comments);

let data_comments =
Comment_utils.get_comments_inside_location(
~location=decl.pdata_loc,
comments,
);

previous_data := Some(decl);

Doc.concat([
leading_comment_docs,
switch ((expt: Asttypes.export_flag)) {
| Nonexported => Doc.nil
| Exported => Doc.text("export ")
Expand Down
23 changes: 23 additions & 0 deletions compiler/test/formatter_inputs/data_docs.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
enum EventType {
Enter,
Exit,
},
/**
* An event is the start or end of a token amongst other events.
* Tokens can “contain” other tokens, even though they are stored in a flat
* list, through `enter`ing before them, and `exit`ing after them.
*/
type Event = (EventType, Token, TokenizeContext),
/**
* Another event is the start or end of a token amongst other events.
* Tokens can “contain” other tokens, even though they are stored in a flat
* list, through `enter`ing before them, and `exit`ing after them.
*/


enum EventType2 {
Enter2,
Exit2,
},
// line comment
type Event2 = (EventType2, Token, TokenizeContext)
21 changes: 21 additions & 0 deletions compiler/test/formatter_outputs/data_docs.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
enum EventType {
Enter,
Exit,
},
/**
* An event is the start or end of a token amongst other events.
* Tokens can “contain” other tokens, even though they are stored in a flat
* list, through `enter`ing before them, and `exit`ing after them.
*/
type Event = (EventType, Token, TokenizeContext),
/**
* Another event is the start or end of a token amongst other events.
* Tokens can “contain” other tokens, even though they are stored in a flat
* list, through `enter`ing before them, and `exit`ing after them.
*/
enum EventType2 {
Enter2,
Exit2,
},
// line comment
type Event2 = (EventType2, Token, TokenizeContext)
1 change: 1 addition & 0 deletions compiler/test/suites/formatter.re
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ describe("formatter", ({test, testSkip}) => {
assertFormatOutput("rationals", "rationals");
assertFormatOutput("constraints", "constraints");
assertFormatOutput("only_comments", "only_comments");
assertFormatOutput("data_docs", "data_docs");
assertFormatOutput("custom_operators", "custom_operators");
});

0 comments on commit be4b38d

Please sign in to comment.