Skip to content

Commit 4034e8a

Browse files
Smart linebreak printer for pipe chains
1 parent a8c7f15 commit 4034e8a

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

jscomp/syntax/src/res_printer.ml

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,7 +3528,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
35283528
[spacingBeforeOperator; Doc.text operatorTxt; spacingAfterOperator]
35293529
in
35303530
let printOperand ~isLhs expr parentOperator =
3531-
let rec flatten ~isLhs expr parentOperator =
3531+
let rec flatten ~isLhs ~isMultiLinePipe expr parentOperator =
35323532
if ParsetreeViewer.isBinaryExpression expr then
35333533
match expr with
35343534
| {
@@ -3541,7 +3541,18 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
35413541
ParsetreeViewer.flattenableOperators parentOperator operator
35423542
&& not (ParsetreeViewer.hasAttributes expr.pexp_attributes)
35433543
then
3544-
let leftPrinted = flatten ~isLhs:true left operator in
3544+
(* If the pipe-chain is written over multiple lines, break automatically
3545+
* `let x = a->b->c -> same line, break when line-width exceeded
3546+
* `let x = a->
3547+
* b->c` -> pipe-chain is written on multiple lines, break the group *)
3548+
let isMultiLinePipe =
3549+
isMultiLinePipe
3550+
|| left.pexp_loc.loc_start.pos_lnum
3551+
< right.pexp_loc.loc_start.pos_lnum
3552+
in
3553+
let leftPrinted =
3554+
flatten ~isLhs:true ~isMultiLinePipe left operator
3555+
in
35453556
let rightPrinted =
35463557
let rightPrinteableAttrs, rightInternalAttrs =
35473558
ParsetreeViewer.partitionPrintableAttributes
@@ -3585,12 +3596,22 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
35853596
Doc.rparen;
35863597
]
35873598
else
3588-
Doc.concat
3589-
[
3590-
leftPrinted;
3591-
printBinaryOperator ~inlineRhs:false operator;
3592-
rightPrinted;
3593-
]
3599+
match operator with
3600+
| ("|." | "|.u") when isMultiLinePipe ->
3601+
Doc.breakableGroup ~forceBreak:true
3602+
(Doc.concat
3603+
[
3604+
leftPrinted;
3605+
printBinaryOperator ~inlineRhs:false operator;
3606+
rightPrinted;
3607+
])
3608+
| _ ->
3609+
Doc.concat
3610+
[
3611+
leftPrinted;
3612+
printBinaryOperator ~inlineRhs:false operator;
3613+
rightPrinted;
3614+
]
35943615
in
35953616

35963617
let doc =
@@ -3665,7 +3686,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
36653686
| Braced braces -> printBraces doc expr braces
36663687
| Nothing -> doc)
36673688
in
3668-
flatten ~isLhs expr parentOperator
3689+
flatten ~isLhs ~isMultiLinePipe:false expr parentOperator
36693690
in
36703691
match expr.pexp_desc with
36713692
| Pexp_apply

jscomp/syntax/tests/conversion/reason/expected/fastPipe.res.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@ let aggregateTotal = (forecast, ~audienceType) =>
2222
views: item["reach"]["views"],
2323
sample: item["reach"]["sample"],
2424
})
25+
26+
let myFunc = (strA, strB) => strA ++ strB
27+
28+
"expr1"->myFunc("expr2")->myFunc("expr3")
29+
30+
"expr1"
31+
->myFunc("expr2")
32+
->myFunc("expr3")
33+
34+
"expr1"
35+
->myFunc("expr2")
36+
->myFunc("expr3")
37+
->myFunc("expr4")

jscomp/syntax/tests/conversion/reason/fastPipe.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ let aggregateTotal = (forecast, ~audienceType) =>
2222
views: item["reach"]["views"],
2323
sample: item["reach"]["sample"],
2424
})
25+
26+
let myFunc = (strA, strB) => strA ++ strB
27+
28+
"expr1"->myFunc("expr2")->myFunc("expr3")
29+
30+
"expr1"
31+
->myFunc("expr2")->myFunc("expr3")
32+
33+
"expr1"->myFunc("expr2")
34+
->myFunc("expr3")->myFunc("expr4")

jscomp/syntax/tests/printer/expr/expected/callback.res.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,16 @@ let f = () => {
215215
</div>
216216
}
217217

218-
myPromise->Js.Promise.then_(value => {
218+
myPromise
219+
->Js.Promise.then_(value => {
219220
Js.log(value)
220221
Js.Promise.resolve(value + 2)
221-
}, _)->Js.Promise.then_(value => {
222+
}, _)
223+
->Js.Promise.then_(value => {
222224
Js.log(value)
223225
Js.Promise.resolve(value + 3)
224-
}, _)->Js.Promise.catch(err => {
226+
}, _)
227+
->Js.Promise.catch(err => {
225228
Js.log2("Failure!!", err)
226229
Js.Promise.resolve(-2)
227230
}, _)

0 commit comments

Comments
 (0)