Skip to content

Commit

Permalink
Force infix operator on a new line if a lambda is involved (#959)
Browse files Browse the repository at this point in the history
* Force infix expressions on next line for certain operators.

* Revert "Force infix expressions on next line for certain operators."

This reverts commit 3f9ac69.

* Force infix operator on a new line if a lambda is involved. Fixes #942
  • Loading branch information
nojaf authored Jul 11, 2020
1 parent 17bfa28 commit 3c128a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/Fantomas.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,29 @@ let ``addition via function`` () =
|> should equal """
let a = (+) 7 8
"""

[<Test>]
let ``lambda piped to lambda should be multiline, 942`` () =
formatSourceString false """
let r (f : 'a -> 'b) (a : 'a) : 'b =
fun () ->
f a
|> fun f -> f ()
""" config
|> prepend newline
|> should equal """
let r (f: 'a -> 'b) (a: 'a): 'b =
fun () -> f a
|> fun f -> f ()
"""

[<Test>]
let ``combining lines breaks function precedence 488`` () =
formatSourceString false """fun () -> ()
|> Some
""" config
|> prepend newline
|> should equal """
fun () -> ()
|> Some
"""
16 changes: 11 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,15 +1231,21 @@ and genExpr astContext synExpr =
| (s,_,_)::_ when ((noSpaceInfixOps.Contains s)) -> sepNone
| _ -> f

let isLambda e = match e with | Lambda _ -> true | _ -> false
let expr = genExpr astContext e
let shortExpr = expr +> sepAfterExpr sepSpace +> genInfixAppsShort astContext es
let longExpr = expr +> sepAfterExpr sepNln +> genInfixApps astContext es

atCurrentColumn
(fun ctx ->
isShortExpression
ctx.Config.MaxInfixOperatorExpression
(expr +> sepAfterExpr sepSpace +> genInfixAppsShort astContext es)
(expr +> sepAfterExpr sepNln +> genInfixApps astContext es)
ctx)
if isLambda e || List.exists (fun (_,_,e) -> isLambda e) es then
longExpr ctx
else
isShortExpression
ctx.Config.MaxInfixOperatorExpression
shortExpr
longExpr
ctx)

| TernaryApp(e1,e2,e3) ->
atCurrentColumn (genExpr astContext e1 +> !- "?" +> genExpr astContext e2 +> sepSpace +> !- "<-" +> sepSpace +> genExpr astContext e3)
Expand Down

0 comments on commit 3c128a4

Please sign in to comment.