@@ -2974,9 +2974,53 @@ and print_expression_inner =
29742974 | PExpCollectionConcat (concat_t , collections ) =>
29752975 let list_length = List . length(collections);
29762976
2977+ let flattened =
2978+ List . map(
2979+ ((t, e)) => {
2980+ let process_elem =
2981+ switch (concat_t) {
2982+ | PExpListConcat =>
2983+ let rec process_elem = list => {
2984+ switch (list. Parsetree . pexp_desc) {
2985+ | Parsetree . PExpConstruct (
2986+ {txt: IdentName ({txt: "[...]" })},
2987+ PExpConstrTuple ([ expr , rest ] ),
2988+ ) =>
2989+ switch (rest. pexp_desc) {
2990+ | Parsetree . PExpConstruct (
2991+ {txt: IdentName ({txt: "[]" })},
2992+ PExpConstrTuple (_ ),
2993+ ) =>
2994+ // Non-spread element at end of list; do not recurse
2995+ [ (false , expr)]
2996+ | _ => [ (false , expr), ... process_elem(rest)]
2997+ }
2998+ | _ => [ (true , list)]
2999+ };
3000+ };
3001+ process_elem;
3002+ | PExpArrayConcat => (
3003+ array => {
3004+ switch (array. Parsetree . pexp_desc) {
3005+ | Parsetree . PExpArray (elems ) =>
3006+ List . map(elem => (false , elem), elems)
3007+ | _ => assert (false )
3008+ };
3009+ }
3010+ )
3011+ };
3012+ switch (t) {
3013+ | Parsetree . PExpSpreadExpr => [ (true , e)]
3014+ | Parsetree . PExpNonSpreadExpr => process_elem(e)
3015+ };
3016+ },
3017+ collections,
3018+ )
3019+ |> List . flatten;
3020+
29773021 let items =
29783022 List . mapi(
2979- (index, (t , e)) => {
3023+ (index, (is_spread , e)) => {
29803024 // Do we have any comments on this line?
29813025 // If so, we break the whole list
29823026
@@ -3005,24 +3049,12 @@ and print_expression_inner =
30053049 };
30063050
30073051 let expr =
3008- switch (t) {
3009- | Parsetree . PExpNonSpreadExpr =>
3010- let e =
3011- switch (concat_t, e. pexp_desc) {
3012- | (
3013- PExpListConcat ,
3014- Parsetree . PExpConstruct (
3015- {txt: IdentName ({txt: "[...]" })},
3016- PExpConstrTuple ([ expr , _ ] ),
3017- ),
3018- )
3019- | (PExpArrayConcat , Parsetree . PExpArray ([ expr ] )) => expr
3020- | _ =>
3021- failwith (
3022- "Impossible: formatter: non-spread concat item containing invalid data" ,
3023- )
3024- };
3025-
3052+ Doc . concat([
3053+ if (is_spread) {
3054+ Doc . text("..." );
3055+ } else {
3056+ Doc . nil;
3057+ },
30263058 print_expression(
30273059 ~expression_parent= GenericExpression ,
30283060 ~original_source,
@@ -3032,25 +3064,11 @@ and print_expression_inner =
30323064 comments,
30333065 ),
30343066 e,
3035- );
3036- | Parsetree . PExpSpreadExpr =>
3037- Doc . concat([
3038- Doc . text("..." ),
3039- print_expression(
3040- ~expression_parent= GenericExpression ,
3041- ~original_source,
3042- ~comments=
3043- Comment_utils . get_comments_inside_location(
3044- ~location= e. pexp_loc,
3045- comments,
3046- ),
3047- e,
3048- ),
3049- ] )
3050- };
3067+ ),
3068+ ] );
30513069 (expr, end_line_comments);
30523070 },
3053- collections ,
3071+ flattened ,
30543072 );
30553073
30563074 let (last_line_breaks_for_comments , list_items ) =
0 commit comments