Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force infix operator on a new line if a lambda is involved #959

Merged
merged 4 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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