Skip to content

Commit

Permalink
Format function calls with multiple lambdas as multiline. Fixes fspro…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Sep 26, 2020
1 parent 3a17205 commit 3124f3d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
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

0 comments on commit 3124f3d

Please sign in to comment.