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

Keep lambda at same line in multiline dotget. #1663

Merged
merged 1 commit into from
Apr 20, 2021
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
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