Skip to content

Commit

Permalink
Print trivia of SynExpr_Paren in MultiLineLambdaClosingNewline. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Apr 8, 2021
1 parent 5d1684b commit 987b5e4
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 5 deletions.
95 changes: 95 additions & 0 deletions src/Fantomas.Tests/MultiLineLambdaClosingNewlineTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,98 @@ module Caching =
cacheFiles.CachedNetworkData
(fun _ -> sessionCachedNetworkData)
"""

[<Test>]
let ``comment before paren function arg, 1607`` () =
formatSourceString
false
"""
namespace Bar
[<RequireQualifiedAccess>]
module Foo =
/// Blah
let bang<'a when 'a : equality> (a : Foo<'a>) (ans : ('a * System.TimeSpan) list) : bool =
List.length x = List.length y
&&
List.forall2
//
(fun (a, ta) (b, tb) -> a.Equals b && ta = tb)
x
y
"""
{ config with
MaxLineLength = 100
SpaceBeforeUppercaseInvocation = true
SpaceBeforeClassConstructor = true
SpaceBeforeMember = true
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
MultilineBlockBracketsOnSameColumn = true
KeepIfThenInSameLine = true
MultiLineLambdaClosingNewline = true
KeepIndentInBranch = true }
|> prepend newline
|> should
equal
"""
namespace Bar
[<RequireQualifiedAccess>]
module Foo =
/// Blah
let bang<'a when 'a : equality> (a : Foo<'a>) (ans : ('a * System.TimeSpan) list) : bool =
List.length x = List.length y
&& List.forall2
//
(fun (a, ta) (b, tb) -> a.Equals b && ta = tb)
x
y
"""

[<Test>]
let ``comment before paren function arg, idempotent`` () =
formatSourceString
false
"""
namespace Bar
[<RequireQualifiedAccess>]
module Foo =
/// Blah
let bang<'a when 'a : equality> (a : Foo<'a>) (ans : ('a * System.TimeSpan) list) : bool =
List.length x = List.length y
&& List.forall2
//
(fun (a, ta) (b, tb) -> a.Equals b && ta = tb)
x
y
"""
{ config with
MaxLineLength = 100
SpaceBeforeUppercaseInvocation = true
SpaceBeforeClassConstructor = true
SpaceBeforeMember = true
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
MultilineBlockBracketsOnSameColumn = true
KeepIfThenInSameLine = true
MultiLineLambdaClosingNewline = true
KeepIndentInBranch = true }
|> prepend newline
|> should
equal
"""
namespace Bar
[<RequireQualifiedAccess>]
module Foo =
/// Blah
let bang<'a when 'a : equality> (a : Foo<'a>) (ans : ('a * System.TimeSpan) list) : bool =
List.length x = List.length y
&& List.forall2
//
(fun (a, ta) (b, tb) -> a.Equals b && ta = tb)
x
y
"""
18 changes: 13 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3239,7 +3239,14 @@ and genApp astContext e es ctx =
sepNln
es
(fun e ->
let genLambda pats (bodyExpr: SynExpr) lpr rpr arrowRange =
let genLambda
(pats: Context -> Context)
(bodyExpr: SynExpr)
(lpr: Range)
(rpr: Range option)
(arrowRange: Range)
(pr: Range)
: Context -> Context =
let sepOpenTFor r = tokN r LPAREN sepOpenT

let sepCloseTFor r =
Expand All @@ -3259,21 +3266,22 @@ and genApp astContext e es ctx =
+> genExprAfterArrow bodyExpr
+> unindent)
(fun isMultiline -> onlyIf isMultiline sepNln +> sepCloseTFor rpr)
|> genTriviaFor SynExpr_Paren pr

match e with
| Paren (lpr, DesugaredLambda (cps, e), rpr, _) ->
| Paren (lpr, DesugaredLambda (cps, e), rpr, pr) ->
let arrowRange =
List.last cps
|> snd
|> fun lastPatRange -> ctx.MkRange lastPatRange.End e.Range.Start

genLambda (col sepSpace cps (fst >> genComplexPats astContext)) e lpr rpr arrowRange
| Paren (lpr, Lambda (e, sps, _), rpr, _) ->
genLambda (col sepSpace cps (fst >> genComplexPats astContext)) e lpr rpr arrowRange pr
| Paren (lpr, Lambda (e, sps, _), rpr, pr) ->
let arrowRange =
List.last sps
|> fun sp -> ctx.MkRange sp.Range.End e.Range.Start

genLambda (col sepSpace sps (genSimplePats astContext)) e lpr rpr arrowRange
genLambda (col sepSpace sps (genSimplePats astContext)) e lpr rpr arrowRange pr
| _ -> genExpr astContext e)

let singleLambdaArgument =
Expand Down

0 comments on commit 987b5e4

Please sign in to comment.