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

Format function calls with multiple lambdas as multiline #1168

Merged
merged 2 commits into from
Sep 26, 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
41 changes: 41 additions & 0 deletions src/Fantomas.Tests/LambdaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,44 @@ Target.create "Install" (fun x ->
// Paket restore will already happen when the build.fsx dependencies are restored
)
"""

[<Test>]
let ``function call with two lambda arguments, 1164`` () =
formatSourceString false """
let init =
addDateTimeConverter
(fun dt -> Date(dt.Year, dt.Month, dt.Day))
(fun (Date (y, m, d)) ->
System.DateTime(y, m, d))
""" { config with MaxLineLength = 85 }
|> prepend newline
|> should equal """
let init =
addDateTimeConverter
(fun dt -> Date(dt.Year, dt.Month, dt.Day))
(fun (Date (y, m, d)) -> System.DateTime(y, m, d))
"""

[<Test>]
let ``function call with two lambdas and three other arguments`` () =
formatSourceString false """
SettingControls.toggleButton (fun _ ->
UpdateOption(key, MultilineFormatterTypeOption(o, key, "character_width"))
|> dispatch) (fun _ ->
UpdateOption(key, MultilineFormatterTypeOption(o, key, "number_of_items"))
|> dispatch) "CharacterWidth" "NumberOfItems" key (v = "character_width")
""" config
|> prepend newline
|> should equal """
SettingControls.toggleButton
(fun _ ->
UpdateOption(key, MultilineFormatterTypeOption(o, key, "character_width"))
|> dispatch)
(fun _ ->
UpdateOption(key, MultilineFormatterTypeOption(o, key, "number_of_items"))
|> dispatch)
"CharacterWidth"
"NumberOfItems"
key
(v = "character_width")
"""
9 changes: 5 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1966,12 +1966,13 @@ and genExpr astContext synExpr =
+> genExpr astContext e
+> unindent))))

let hasThreeOrMoreLambdas =
let hasMultipleLambdas =
List.filter (function
| Paren (_, Lambda _, _) -> true
| Paren (_, Lambda _, _)
| Paren (_, DesugaredLambda _, _) -> true
| _ -> false) es
|> List.length
|> fun l -> l >= 3
|> fun l -> l > 1

if List.exists (function
| Lambda _
Expand All @@ -1981,7 +1982,7 @@ and genExpr astContext synExpr =
| MultilineString _
| CompExpr _ -> true
| _ -> false) es
&& not hasThreeOrMoreLambdas then
&& not hasMultipleLambdas then
shortExpression
else
expressionFitsOnRestOfLine shortExpression longExpression
Expand Down