Skip to content

Commit 8a60985

Browse files
fix(grainfmt): Handle comment in or near if conditionals better (#1513)
1 parent 1565c16 commit 8a60985

File tree

3 files changed

+114
-34
lines changed

3 files changed

+114
-34
lines changed

compiler/src/formatting/format.re

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,36 @@ and print_expression =
29272927
comments,
29282928
);
29292929

2930+
let last_comment_different_line =
2931+
switch (cond_trailing_comment) {
2932+
| [] => false
2933+
| [first, ...rest] =>
2934+
let (_, first_comment_line, _, _) =
2935+
Locations.get_raw_pos_info(
2936+
Locations.get_comment_loc(first).loc_start,
2937+
);
2938+
2939+
let (_, condition_line, _, _) =
2940+
Locations.get_raw_pos_info(condition.pexp_loc.loc_end);
2941+
2942+
first_comment_line > condition_line;
2943+
};
2944+
2945+
let same_line_comments =
2946+
last_comment_different_line ? [] : cond_trailing_comment;
2947+
let later_line_comments =
2948+
last_comment_different_line ? cond_trailing_comment : [];
2949+
2950+
let print_later_comments = (~default, later_line_comments) =>
2951+
switch (later_line_comments) {
2952+
| [] => default
2953+
| cmts =>
2954+
Doc.concat([
2955+
Doc.line,
2956+
Comment_utils.new_comments_to_docs(later_line_comments),
2957+
])
2958+
};
2959+
29302960
let true_trailing_comment =
29312961
Comment_utils.get_comments_between_locs(
29322962
~begin_loc=true_expr.pexp_loc,
@@ -3017,7 +3047,7 @@ and print_expression =
30173047
]);
30183048
} else if (true_is_if) {
30193049
Doc.concat([
3020-
Doc.space,
3050+
print_later_comments(~default=Doc.space, later_line_comments),
30213051
Doc.lparen,
30223052
Doc.indent(
30233053
Doc.concat([
@@ -3036,7 +3066,7 @@ and print_expression =
30363066
} else {
30373067
Doc.indent(
30383068
Doc.concat([
3039-
Doc.line,
3069+
print_later_comments(~default=Doc.line, later_line_comments),
30403070
print_expression(
30413071
~expression_parent=GenericExpression,
30423072
~original_source,
@@ -3146,6 +3176,47 @@ and print_expression =
31463176
])
31473177
};
31483178

3179+
let inner =
3180+
Doc.concat([
3181+
Doc.softLine,
3182+
Comment_utils.inbetween_comments_to_docs(
3183+
~offset=false,
3184+
cond_leading_comment,
3185+
),
3186+
switch (cond_leading_comment) {
3187+
| [] => Doc.nil
3188+
| _ => Doc.ifBreaks(Doc.nil, Doc.space)
3189+
},
3190+
Doc.group(
3191+
print_expression(
3192+
~expression_parent=ConditionalExpression,
3193+
~original_source,
3194+
~comments=commentsInCondition,
3195+
condition,
3196+
),
3197+
),
3198+
switch (same_line_comments) {
3199+
| [] => Doc.nil
3200+
| _ =>
3201+
Doc.concat([
3202+
Doc.concat(
3203+
List.mapi(
3204+
(index, c) =>
3205+
Doc.concat([
3206+
Doc.space,
3207+
Comment_utils.nobreak_comment_to_doc(c),
3208+
switch (c) {
3209+
| Line(_) => Doc.breakParent
3210+
| _ => Doc.nil
3211+
},
3212+
]),
3213+
same_line_comments,
3214+
),
3215+
),
3216+
])
3217+
},
3218+
]);
3219+
31493220
Doc.concat([
31503221
Doc.group(
31513222
Doc.concat([
@@ -3154,37 +3225,7 @@ and print_expression =
31543225
Doc.group(
31553226
Doc.concat([
31563227
Doc.lparen,
3157-
Doc.indent(
3158-
Doc.concat([
3159-
Doc.softLine,
3160-
Comment_utils.inbetween_comments_to_docs(
3161-
~offset=false,
3162-
cond_leading_comment,
3163-
),
3164-
switch (cond_leading_comment) {
3165-
| [] => Doc.nil
3166-
| _ => Doc.ifBreaks(Doc.nil, Doc.space)
3167-
},
3168-
Doc.group(
3169-
print_expression(
3170-
~expression_parent=ConditionalExpression,
3171-
~original_source,
3172-
~comments=commentsInCondition,
3173-
condition,
3174-
),
3175-
),
3176-
if (cond_trailing_comment == []) {
3177-
Doc.nil;
3178-
} else {
3179-
Doc.concat([
3180-
Doc.space,
3181-
Comment_utils.block_trailing_comments_docs(
3182-
cond_trailing_comment,
3183-
),
3184-
]);
3185-
},
3186-
]),
3187-
),
3228+
Doc.indent(inner),
31883229
Doc.softLine,
31893230
Doc.rparen,
31903231
]),

compiler/test/formatter_inputs/comments.gr

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,26 @@ let _KEY_LEN2 = 64
106106
/* ending block 2 */
107107
// trailing comment 2
108108

109-
109+
if (true) // comment
110+
true
111+
else
112+
false
113+
114+
if (true)
115+
// comment
116+
true
117+
else
118+
false
119+
120+
if (true) /* hey */
121+
true
122+
else
123+
false
124+
125+
if (true)
126+
{
127+
// comment
128+
true
129+
}
130+
else
131+
false

compiler/test/formatter_outputs/comments.gr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,20 @@ let _KEY_LEN2 = 64
102102

103103
/* ending block 2 */
104104
// trailing comment 2
105+
106+
if (
107+
true // comment
108+
) true else false
109+
110+
if (true)
111+
// comment
112+
true else false
113+
114+
if (true /* hey */) true else false
115+
116+
if (true) {
117+
// comment
118+
true
119+
} else {
120+
false
121+
}

0 commit comments

Comments
 (0)