From 9cb3927c856408fc9b8d83604b3db078fdf97280 Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 27 Apr 2021 10:55:52 +0200 Subject: [PATCH] Add space before dotget lambda function. Fixes #1681. --- src/Fantomas.Tests/DotGetTests.fs | 56 ++++++++++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 57 +++++++++++++++++++++---------- 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/src/Fantomas.Tests/DotGetTests.fs b/src/Fantomas.Tests/DotGetTests.fs index 29b79c0008..a6d5a41d0e 100644 --- a/src/Fantomas.Tests/DotGetTests.fs +++ b/src/Fantomas.Tests/DotGetTests.fs @@ -1207,3 +1207,59 @@ mock .OrNot() .End """ + +[] +let ``dotget function application should add space before argument, short`` () = + formatSourceString + false + """ +m.Property(fun p -> p.Name).HasMaxLength 64 +""" + config + |> prepend newline + |> should + equal + """ +m.Property(fun p -> p.Name).HasMaxLength 64 +""" + + +[] +let ``dotget function application should add space before argument, long`` () = + formatSourceString + false + """ +m.Property(fun p -> p.Name).IsRequired().HasColumnName("ModelName").HasMaxLength 64 +""" + config + |> prepend newline + |> should + equal + """ +m + .Property(fun p -> p.Name) + .IsRequired() + .HasColumnName("ModelName") + .HasMaxLength 64 +""" + +[] +let ``dotget lambda multiline application`` () = + formatSourceString + false + """ +m.Property(fun p -> p.Name).IsRequired().HasColumnName("ModelName").HasMaxLength +""" + config + |> prepend newline + |> should + equal + """ +m + .Property(fun p -> p.Name) + .IsRequired() + .HasColumnName( + "ModelName" + ) + .HasMaxLength +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index d11fbc98f6..ec0856322a 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1815,6 +1815,14 @@ and genExpr astContext synExpr ctx = genFunctionNameWithMultilineLids argFn lids | TypeApp (LongIdentPiecesExpr lids, ts) when (List.moreThanOne lids) -> genFunctionNameWithMultilineLids (genGenericTypeParameters astContext ts +> argFn) lids + | DotGetAppDotGetAppParenLambda _ -> + leadingExpressionIsMultiline + (genExpr astContext e) + (fun isMultiline -> + if isMultiline then + indent +> argFn +> unindent + else + argFn) | _ -> genExpr astContext e +> argFn let arguments = @@ -1846,27 +1854,31 @@ and genExpr astContext synExpr ctx = genFunctionNameWithMultilineLids (genGenericTypeParameters astContext ts +> f) lids | _ -> genExpr astContext e +> f + let lastEsIndex = es.Length - 1 + let short = genExpr astContext e +> genExpr astContext px - +> col + +> coli sepNone es - (fun (lids, e, ts) -> + (fun idx (lids, e, ts) -> genLidsWithDots lids +> genGenericTypeParameters astContext ts + +> genSpaceBeforeLids idx lastEsIndex lids e +> genExpr astContext e) let long = genLongFunctionName (genExpr astContext px) +> indent +> sepNln - +> col + +> coli sepNln es - (fun (lids, e, ts) -> + (fun idx (lids, e, ts) -> genLidsWithDotsAndNewlines lids +> genGenericTypeParameters astContext ts + +> genSpaceBeforeLids idx lastEsIndex lids e +> genExpr astContext e) +> unindent @@ -1909,28 +1921,16 @@ and genExpr astContext synExpr ctx = let lastEsIndex = es.Length - 1 let genApp (idx: int) ((lids, e, ts): (string * range) list * SynExpr * SynType list) : Context -> Context = - let addSpace ctx = - let config = - let s = fst lids.[0] - - if Char.IsUpper(s.[0]) then - ctx.Config.SpaceBeforeUppercaseInvocation - else - ctx.Config.SpaceBeforeLowercaseInvocation - - (lastEsIndex = idx) - && (not (hasParenthesis e) || config) - let short = genLidsWithDots lids +> genGenericTypeParameters astContext ts - +> ifElseCtx addSpace sepSpace sepNone + +> genSpaceBeforeLids idx lastEsIndex lids e +> genExpr astContext e let long = genLidsWithDotsAndNewlines lids +> genGenericTypeParameters astContext ts - +> ifElseCtx addSpace sepSpace sepNone + +> genSpaceBeforeLids idx lastEsIndex lids e +> genMultilineFunctionApplicationArguments sepOpenTFor sepCloseTFor astContext e expressionFitsOnRestOfLine short long @@ -2669,6 +2669,27 @@ and genLidsWithDotsAndNewlines (lids: (string * range) list) = +> !- "." +> col (sepNln +> !- ".") lids (fun (s, _) -> !-s) +and genSpaceBeforeLids + (currentIndex: int) + (lastEsIndex: int) + (lids: (string * range) list) + (arg: SynExpr) + (ctx: Context) + : Context = + let config = + let s = fst lids.[0] + + if Char.IsUpper(s.[0]) then + ctx.Config.SpaceBeforeUppercaseInvocation + else + ctx.Config.SpaceBeforeLowercaseInvocation + + if (lastEsIndex = currentIndex) + && (not (hasParenthesis arg) || config) then + sepSpace ctx + else + ctx + and genFunctionNameWithMultilineLids f lids = match lids with | (s, r) :: t ->