Skip to content

Commit 732027f

Browse files
committed
fixed formatting
1 parent 7af1d6a commit 732027f

File tree

5 files changed

+99
-60
lines changed

5 files changed

+99
-60
lines changed

compiler/src/formatting/format.re

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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) =

compiler/src/parsing/ast_helper.re

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,6 @@ module Expression = {
431431
switch (List.rev(a)) {
432432
| [] => empty
433433
| [base, ...rest] =>
434-
let base =
435-
switch (base) {
436-
| ListItem(expr) =>
437-
tuple_construct(~loc, ~attributes?, ident_cons, [expr, empty])
438-
| ListSpread(expr, _) => expr
439-
};
440434
let has_nonfinal_spread =
441435
List.exists(
442436
expr =>
@@ -447,29 +441,44 @@ module Expression = {
447441
rest,
448442
);
449443
if (has_nonfinal_spread) {
444+
let base =
445+
switch (base) {
446+
| ListItem(expr) => (
447+
PExpNonSpreadExpr,
448+
tuple_construct(
449+
~loc,
450+
~attributes?,
451+
ident_cons,
452+
[expr, empty],
453+
),
454+
)
455+
| ListSpread(expr, _) => (PExpSpreadExpr, expr)
456+
};
450457
let grouped =
451458
List.fold_left(
452459
(acc, expr) => {
453460
switch (expr) {
454461
| ListSpread(expr, loc) => [
455-
{...expr, pexp_loc: loc},
462+
(PExpSpreadExpr, {...expr, pexp_loc: loc}),
456463
...acc,
457464
]
458465
| ListItem(expr) =>
459-
let (first, rest) =
460-
switch (acc) {
461-
| [first, ...rest] => (first, rest)
462-
| _ => assert(false)
463-
};
464-
[
465-
tuple_construct(
466-
~loc,
467-
~attributes?,
468-
ident_cons,
469-
[expr, first],
470-
),
471-
...rest,
472-
];
466+
switch (acc) {
467+
| [(_, first), ...rest] => [
468+
(
469+
PExpNonSpreadExpr,
470+
tuple_construct(
471+
~loc,
472+
~attributes?,
473+
ident_cons,
474+
[expr, first],
475+
),
476+
),
477+
...rest,
478+
]
479+
| _ =>
480+
failwith("Impossible: processed ListItem with empty acc")
481+
}
473482
}
474483
},
475484
[base],
@@ -479,9 +488,15 @@ module Expression = {
479488
~loc,
480489
~attributes?,
481490
PExpListConcat,
482-
List.map(expr => (PExpSpreadExpr, expr), grouped),
491+
grouped,
483492
);
484493
} else {
494+
let base =
495+
switch (base) {
496+
| ListItem(expr) =>
497+
tuple_construct(~loc, ~attributes?, ident_cons, [expr, empty])
498+
| ListSpread(expr, _) => expr
499+
};
485500
List.fold_left(
486501
(acc, expr) => {
487502
switch (expr) {

compiler/test/grainfmt/spreads.expected.gr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ let rec reject2 = (fn, list) => {
3939
}
4040

4141
let l = ["a", "b", "c"]
42+
let a = [...l, "a", "b", ...l, "d"]
4243
let a = [
4344
...l,
4445
"secondInAVeryLongString",

compiler/test/grainfmt/spreads.input.gr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let rec reject2 = (fn, list) => {
3333
}
3434

3535
let l = ["a", "b", "c"]
36+
let a = [...l, "a", "b", ...l, "d"]
3637
let a = [...l, "secondInAVeryLongString", "thirdInAStringThatWillBreakLine", ...l]
3738

3839
let a = [

stdlib/runtime/concat.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
title: Concat
33
---
44

5+
## Values
6+
7+
Functions and constants included in the Concat module.
8+
59
### Concat.**listConcat**
610

711
```grain
8-
listConcat : Array<List<a>> -> List<a>
12+
listConcat : (listsArray: Array<List<a>>) -> List<a>
913
```
1014

1115
### Concat.**arrayConcat**
1216

1317
```grain
14-
arrayConcat : Array<Array<a>> -> Array<a>
18+
arrayConcat : (arraysArray: Array<Array<a>>) -> Array<a>
1519
```
1620

0 commit comments

Comments
 (0)