@@ -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 ] ),
0 commit comments