Skip to content

Commit

Permalink
Keep lambda at same line in multiline dotget. Fixes #1662. (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Apr 20, 2021
1 parent a733bcd commit 33dfe68
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/Fantomas.Tests/DotGetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,3 +1125,85 @@ module Foo =
()
"""

[<Test>]
let ``single line dotget lambda, followed by application`` () =
formatSourceString
false
"""
Foo(fun x -> x).Bar().Meh
"""
config
|> prepend newline
|> should
equal
"""
Foo(fun x -> x).Bar().Meh
"""

[<Test>]
let ``multiline dotget lambda, followed by application, 1662`` () =
formatSourceString
false
"""
type Class() =
member this.``kk``() =
async {
mock
.Setup(fun m ->
m.CreateBlah
(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Id>(), It.IsAny<uint32>()))
.Returns(Some mock)
.End
}
|> Async.StartImmediate
"""
config
|> prepend newline
|> should
equal
"""
type Class() =
member this.kk() =
async {
mock
.Setup(fun m ->
m.CreateBlah(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Id>(), It.IsAny<uint32>()))
.Returns(Some mock)
.End
}
|> Async.StartImmediate
"""

[<Test>]
let ``multiline dotget lambda, followed by multiple applications`` () =
formatSourceString
false
"""
mock
.Setup(fun m ->
// some comment
m.CreateBlah
(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Id>(), It.IsAny<uint32>()))
.Returns(Some mock)
.OrNot()
.End
"""
{ config with MaxLineLength = 80 }
|> prepend newline
|> should
equal
"""
mock
.Setup(fun m ->
// some comment
m.CreateBlah(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<Id>(),
It.IsAny<uint32>()
))
.Returns(Some mock)
.OrNot()
.End
"""
29 changes: 29 additions & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,35 @@ and genExpr astContext synExpr ctx =
+> !-(sprintf ".%s" s)
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (col sepSpace es (genExpr astContext))

// Foo(fun x -> x).Bar().Meh
| DotGetAppDotGetAppParenLambda (e, px, appLids, es, lids) ->
let short =
genExpr astContext e
+> genExpr astContext px
+> genLidsWithDots appLids
+> col sepComma es (genExpr astContext)
+> genLidsWithDots lids

let long =
let functionName =
match e with
| LongIdentPieces lids when (List.moreThanOne lids) -> genFunctionNameWithMultilineLids id lids
| TypeApp (LongIdentPieces lids, ts) when (List.moreThanOne lids) ->
genFunctionNameWithMultilineLids (genGenericTypeParameters astContext ts) lids
| _ -> genExpr astContext e

functionName
+> indent
+> genExpr astContext px
+> sepNln
+> genLidsWithDotsAndNewlines appLids
+> col sepComma es (genExpr astContext)
+> sepNln
+> genLidsWithDotsAndNewlines lids
+> unindent

fun ctx -> isShortExpression ctx.Config.MaxDotGetExpressionWidth short long ctx

// Foo().Bar
| DotGetAppParen (e, px, lids) ->
let shortAppExpr =
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,12 @@ let (|DotGetAppParen|_|) e =
| DotGet (App (e, [ ConstExpr (SynConst.Unit, _) as px ]), lids) -> Some(e, px, lids)
| _ -> None

let (|DotGetAppDotGetAppParenLambda|_|) (e: SynExpr) =
match e with
| DotGet (App (DotGet (App (e, [ Paren (_, SynExpr.Lambda _, _, _) as px ]), appLids), es), lids) ->
Some(e, px, appLids, es, lids)
| _ -> None

/// Gather series of application for line breaking
let rec (|DotGetApp|_|) =
function
Expand Down

0 comments on commit 33dfe68

Please sign in to comment.