Skip to content

Commit

Permalink
Add space before dotget lambda function. Fixes #1681. (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 27, 2021
1 parent cb92329 commit 051f355
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 18 deletions.
56 changes: 56 additions & 0 deletions src/Fantomas.Tests/DotGetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,3 +1207,59 @@ mock
.OrNot()
.End
"""

[<Test>]
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
"""


[<Test>]
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
"""

[<Test>]
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
"""
57 changes: 39 additions & 18 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit 051f355

Please sign in to comment.