@@ -3527,8 +3527,8 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
3527
3527
Doc. concat
3528
3528
[spacingBeforeOperator; Doc. text operatorTxt; spacingAfterOperator]
3529
3529
in
3530
- let printOperand ~isLhs expr parentOperator =
3531
- let rec flatten ~isLhs expr parentOperator =
3530
+ let printOperand ~isLhs ~ isMultiLine expr parentOperator =
3531
+ let rec flatten ~isLhs ~ isMultiLine expr parentOperator =
3532
3532
if ParsetreeViewer. isBinaryExpression expr then
3533
3533
match expr with
3534
3534
| {
@@ -3541,7 +3541,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
3541
3541
ParsetreeViewer. flattenableOperators parentOperator operator
3542
3542
&& not (ParsetreeViewer. hasAttributes expr.pexp_attributes)
3543
3543
then
3544
- let leftPrinted = flatten ~is Lhs:true left operator in
3544
+ let leftPrinted = flatten ~is Lhs:true ~is MultiLine left operator in
3545
3545
let rightPrinted =
3546
3546
let rightPrinteableAttrs, rightInternalAttrs =
3547
3547
ParsetreeViewer. partitionPrintableAttributes
@@ -3585,12 +3585,26 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
3585
3585
Doc. rparen;
3586
3586
]
3587
3587
else
3588
- Doc. concat
3589
- [
3590
- leftPrinted;
3591
- printBinaryOperator ~inline Rhs:false operator;
3592
- rightPrinted;
3593
- ]
3588
+ match operator with
3589
+ | ("|." | "|.u" ) when isMultiLine ->
3590
+ (* If the pipe-chain is written over multiple lines, break automatically
3591
+ * `let x = a->b->c -> same line, break when line-width exceeded
3592
+ * `let x = a->
3593
+ * b->c` -> pipe-chain is written on multiple lines, break the group *)
3594
+ Doc. breakableGroup ~force Break:true
3595
+ (Doc. concat
3596
+ [
3597
+ leftPrinted;
3598
+ printBinaryOperator ~inline Rhs:false operator;
3599
+ rightPrinted;
3600
+ ])
3601
+ | _ ->
3602
+ Doc. concat
3603
+ [
3604
+ leftPrinted;
3605
+ printBinaryOperator ~inline Rhs:false operator;
3606
+ rightPrinted;
3607
+ ]
3594
3608
in
3595
3609
3596
3610
let doc =
@@ -3665,7 +3679,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
3665
3679
| Braced braces -> printBraces doc expr braces
3666
3680
| Nothing -> doc)
3667
3681
in
3668
- flatten ~is Lhs expr parentOperator
3682
+ flatten ~is Lhs ~is MultiLine expr parentOperator
3669
3683
in
3670
3684
match expr.pexp_desc with
3671
3685
| Pexp_apply
@@ -3679,8 +3693,8 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
3679
3693
|| ParsetreeViewer. isBinaryExpression rhs
3680
3694
|| printAttributes ~state expr.pexp_attributes cmtTbl <> Doc. nil) ->
3681
3695
let lhsHasCommentBelow = hasCommentBelow cmtTbl lhs.pexp_loc in
3682
- let lhsDoc = printOperand ~is Lhs:true lhs op in
3683
- let rhsDoc = printOperand ~is Lhs:false rhs op in
3696
+ let lhsDoc = printOperand ~is Lhs:true ~is MultiLine: false lhs op in
3697
+ let rhsDoc = printOperand ~is Lhs:false ~is MultiLine: false rhs op in
3684
3698
Doc. group
3685
3699
(Doc. concat
3686
3700
[
@@ -3697,12 +3711,16 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
3697
3711
| Pexp_apply
3698
3712
( {pexp_desc = Pexp_ident {txt = Longident. Lident operator}},
3699
3713
[(Nolabel , lhs); (Nolabel , rhs)] ) ->
3714
+ let isMultiLine =
3715
+ lhs.pexp_loc.loc_start.pos_lnum < rhs.pexp_loc.loc_start.pos_lnum
3716
+ in
3717
+
3700
3718
let right =
3701
3719
let operatorWithRhs =
3702
3720
let rhsDoc =
3703
3721
printOperand
3704
3722
~is Lhs:(ParsetreeViewer. isRhsBinaryOperator operator)
3705
- rhs operator
3723
+ ~is MultiLine rhs operator
3706
3724
in
3707
3725
Doc. concat
3708
3726
[
@@ -3722,7 +3740,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
3722
3740
[
3723
3741
printOperand
3724
3742
~is Lhs:(not @@ ParsetreeViewer. isRhsBinaryOperator operator)
3725
- lhs operator;
3743
+ ~is MultiLine lhs operator;
3726
3744
right;
3727
3745
])
3728
3746
in
0 commit comments