Skip to content

Commit

Permalink
Take SynExpr.TypeApp into account with long DotGet chains. (#1449)
Browse files Browse the repository at this point in the history
* SynExpr.TypeApp inside DotGet chain should not have space before. Fixes #1447.

* Respect SpaceBeforeUppercaseInvocation when application is last in DotGet chain. Fixes #1448.
  • Loading branch information
nojaf committed Feb 12, 2021
1 parent 0de4b79 commit b65fa47
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 23 deletions.
79 changes: 75 additions & 4 deletions src/Fantomas.Tests/DotGetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,13 @@ type IWebHostBuilderExtensions() =
.MinimumLevel
.Debug()
.WriteTo
.Logger(fun loggerConfiguration ->
.Logger (fun loggerConfiguration ->
loggerConfiguration
.Enrich
.WithProperty("host", Environment.MachineName)
.Enrich.WithProperty("user", Environment.UserName)
.Enrich
.WithProperty(
.WithProperty (
"application",
context.HostingEnvironment.ApplicationName
)
Expand Down Expand Up @@ -774,7 +774,7 @@ let blah =
"""
let blah =
Mock()
.Returns(fun _ ->
.Returns (fun _ ->
{
dasdasdsadsadsadsa = ""
Sdadsadasdasdas = "sdsadsadasdsa"
Expand Down Expand Up @@ -813,9 +813,80 @@ let blah =
"""
let blah =
Mock("foo")
.Returns(fun _ ->
.Returns (fun _ ->
{
dasdasdsadsadsadsa = ""
Sdadsadasdasdas = "sdsadsadasdsa"
})
"""

[<Test>]
let ``typedApp should not have space with chained DotGet, 1447`` () =
formatSourceString
false
"""
let x =
LoggerConfiguration<Foo>("host", Environment.MachineName)
.Enrich.WithProperty<Bar>("user", Environment.UserName)
.Enrich.WithProperty ("application", context.HostingEnvironment.ApplicationName)
"""
{ config with
SpaceBeforeUppercaseInvocation = true
SpaceBeforeMember = true }
|> prepend newline
|> should
equal
"""
let x =
LoggerConfiguration<Foo>("host", Environment.MachineName)
.Enrich.WithProperty<Bar>("user", Environment.UserName)
.Enrich.WithProperty ("application", context.HostingEnvironment.ApplicationName)
"""

[<Test>]
let ``typedApp should not have space with chained DotGet, unit arg`` () =
formatSourceString
false
"""
let x =
LoggerConfiguration<Foo>()
.Enrich.WithProperty<Bar>("user", Environment.UserName)
.Enrich.WithProperty ("application", context.HostingEnvironment.ApplicationName)
"""
{ config with
SpaceBeforeUppercaseInvocation = true
SpaceBeforeMember = true }
|> prepend newline
|> should
equal
"""
let x =
LoggerConfiguration<Foo>()
.Enrich.WithProperty<Bar>("user", Environment.UserName)
.Enrich.WithProperty ("application", context.HostingEnvironment.ApplicationName)
"""

[<Test>]
let ``typeApp followed by chained lambda, 1448`` () =
formatSourceString
false
"""
let blah =
Mock<Foo>()
.Returns (fun _ ->
{ dasdasdsadsadsadsa = ""
Sdadsadasdasdas = "sdsadsadasdsa" })
"""
{ config with
SpaceBeforeUppercaseInvocation = true
SpaceBeforeMember = true }
|> prepend newline
|> should
equal
"""
let blah =
Mock<Foo>()
.Returns (fun _ ->
{ dasdasdsadsadsadsa = ""
Sdadsadasdasdas = "sdsadsadasdsa" })
"""
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/DotIndexedGetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ myList.[7].lowerSomeFunctionCallOnSeven("looooooooooooooooooooooooooooooooonnggg
equal
"""
myList.[7]
.lowerSomeFunctionCallOnSeven(
.lowerSomeFunctionCallOnSeven (
"looooooooooooooooooooooooooooooooonnggggStringArgument",
otherArg1,
otherArg2,
Expand Down
24 changes: 24 additions & 0 deletions src/Fantomas.Tests/SynExprSetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,27 @@ match x with
lintFixes.[uri] <- fs
"""

[<Test>]
let ``space before uppercase invocation with TypeApp`` () =
formatSourceString
false
"""
Log.Logger <-
LoggerConfiguration<Foo>()
.Destructure.FSharpTypes()
.WriteTo.Console()
.CreateLogger()
"""
{ config with
SpaceBeforeUppercaseInvocation = true }
|> prepend newline
|> should
equal
"""
Log.Logger <-
LoggerConfiguration<Foo>()
.Destructure.FSharpTypes()
.WriteTo.Console()
.CreateLogger ()
"""
40 changes: 22 additions & 18 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,21 +1819,24 @@ and genExpr astContext synExpr ctx =
| DotGetApp (e, es) ->
let genLongFunctionName =
match e with
| App (LongIdentPieces (lids), [ e2 ]) when (List.moreThanOne lids) ->
genFunctionNameWithMultilineLids (genExpr astContext e2) lids
| App (TypeApp (LongIdentPieces (lids), ts), [ e2 ]) when (List.moreThanOne lids) ->
| AppOrTypeApp (LongIdentPieces (lids), ts, [ e2 ]) when (List.moreThanOne lids) ->
genFunctionNameWithMultilineLids
(genGenericTypeParameters astContext ts
(optSingle (genGenericTypeParameters astContext) ts
+> genExpr astContext e2)
lids
| App (SimpleExpr e, [ ConstExpr (SynConst.Unit, r) ]) ->
genExpr astContext e +> genConst SynConst.Unit r
| App (SimpleExpr e, [ Paren _ as px ]) ->
| AppOrTypeApp (SimpleExpr e, ts, [ ConstExpr (SynConst.Unit, r) ]) ->
genExpr astContext e
+> optSingle (genGenericTypeParameters astContext) ts
+> genConst SynConst.Unit r
| AppOrTypeApp (SimpleExpr e, ts, [ Paren _ as px ]) ->
let short =
genExpr astContext e +> genExpr astContext px
genExpr astContext e
+> optSingle (genGenericTypeParameters astContext) ts
+> genExpr astContext px

let long =
genExpr astContext e
+> optSingle (genGenericTypeParameters astContext) ts
+> genMultilineFunctionApplicationArguments sepOpenTFor sepCloseTFor astContext px

expressionFitsOnRestOfLine short long
Expand All @@ -1842,19 +1845,19 @@ 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 short =
let addSpace ctx =
let config =
let s = fst lids.[0]
let addSpace ctx =
let config =
let s = fst lids.[0]

if Char.IsUpper(s.[0]) then
ctx.Config.SpaceBeforeUppercaseInvocation
else
ctx.Config.SpaceBeforeLowercaseInvocation
if Char.IsUpper(s.[0]) then
ctx.Config.SpaceBeforeUppercaseInvocation
else
ctx.Config.SpaceBeforeLowercaseInvocation

(lastEsIndex = idx)
&& (not (hasParenthesis e) || config)
(lastEsIndex = idx)
&& (not (hasParenthesis e) || config)

let short =
genLidsWithDots lids
+> genGenericTypeParameters astContext ts
+> ifElseCtx (addSpace) sepSpace sepNone
Expand All @@ -1863,6 +1866,7 @@ and genExpr astContext synExpr ctx =
let long =
genLidsWithDotsAndNewlines lids
+> genGenericTypeParameters astContext ts
+> ifElseCtx (addSpace) sepSpace sepNone
+> genMultilineFunctionApplicationArguments sepOpenTFor sepCloseTFor astContext e

expressionFitsOnRestOfLine short long
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,12 @@ let (|AppSingleArg|_|) =
| App (e, [ (ConstExpr (SynConst.Unit, _) as px) ]) -> Some(e, px)
| _ -> None

let (|AppOrTypeApp|_|) e =
match e with
| App (TypeApp (e, ts), es) -> Some(e, Some ts, es)
| App (e, es) -> Some(e, None, es)
| _ -> None

let (|NewTuple|_|) =
function
| SynExpr.New (_, t, (Paren _ as px), _) -> Some(t, px)
Expand Down

0 comments on commit b65fa47

Please sign in to comment.