Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split all parameters of a function application over multiple lines if they exceed page width #929

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,131 @@ let a s =
(llloooooooooooooooooooooooooo s)
(llloooooooooooooooooooooooooo s)
"


[<Test>]
let ``should split parameters over multiple lines when they exceed page width``() =
formatSourceString false """module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem compoundBalance None currency address sessionCachedNetworkData
()
)
()""" { config with PageWidth = 60 }
|> prepend newline
|> should equal """
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem
compoundBalance
None
currency
address
sessionCachedNetworkData
())
()
"""

[<Test>]
let ``should split single parameter over multiple lines when it exceeds page width``() =
formatSourceString false """module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
()
)
()""" { config with PageWidth = 60 }
|> prepend newline
|> should equal """
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
())
()
"""

[<Test>]
let ``should not split parameters over multiple lines when they do not exceed page width``() =
formatSourceString false """module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem compoundBalance None currency address sessionCachedNetworkData
()
)
()""" { config with PageWidth = 120 }
|> prepend newline
|> should equal """
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress) (currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m
then ReportProblem compoundBalance None currency address sessionCachedNetworkData
())
()
"""

[<Test>]
let ``should not split single parameter over multiple lines when it does not exceed page width``() =
formatSourceString false """module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then
ReportProblem compoundBalance
()
)
()""" { config with PageWidth = 120 }
|> prepend newline
|> should equal """
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress) (currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
| Cached (balance, time) ->
if compoundBalance < 0.0m then ReportProblem compoundBalance
())
()
"""
8 changes: 6 additions & 2 deletions src/Fantomas.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,9 @@ type FunctionComponent =
(importValueDynamic f).``then``(fun x -> createObj [ "default" ==> x ]))

fun props ->
ReactElementType.create ReactBindings.React.Suspense (createObj [ "fallback" ==> fallback ])
ReactElementType.create
ReactBindings.React.Suspense
(createObj [ "fallback" ==> fallback ])
[ ReactElementType.create elemType props [] ]
#else

Expand Down Expand Up @@ -678,7 +680,9 @@ type FunctionComponent =
(importValueDynamic f).``then``(fun x -> createObj [ "default" ==> x ]))

fun props ->
ReactElementType.create ReactBindings.React.Suspense (createObj [ "fallback" ==> fallback ])
ReactElementType.create
ReactBindings.React.Suspense
(createObj [ "fallback" ==> fallback ])
[ ReactElementType.create elemType props [] ]
#else
fun _ -> div [] [] // React.lazy is not compatible with SSR, so just use an empty div
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/ElmishTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ table [ ClassName "table table-striped table-hover mb-0" ] [
tokenDetailRow "ColorClass" (str colorClass)
tokenDetailRow "CharClass" (str charClass)
tokenDetailRow "Tag" (ofInt tag)
tokenDetailRow "FullMatchedLength"
tokenDetailRow
"FullMatchedLength"
(span [ ClassName "has-text-weight-semibold" ] [
ofInt fullMatchedLength
])
Expand Down
8 changes: 4 additions & 4 deletions src/Fantomas.Tests/KeepIfThenInSameLineTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ let ``if only, 825`` () =
"some very very long error message"
if valueToSend <= 0m then
invalidArg "valueToSend" "Amount has to be above zero"
""" config
""" { config with PageWidth = 100 }
|> prepend newline
|> should equal """
type TransferAmount(valueToSend: decimal, balanceAtTheMomentOfSending: decimal) =
do
if balanceAtTheMomentOfSending < valueToSend then
invalidArg "balanceAtTheMomentOfSending"
"some very very long error message"
invalidArg "balanceAtTheMomentOfSending" "some very very long error message"
if valueToSend <= 0m then
invalidArg "valueToSend" "Amount has to be above zero"
"""
Expand All @@ -47,7 +46,8 @@ type TransferAmount(valueToSend: decimal, balanceAtTheMomentOfSending: decimal)
do
// comment
if balanceAtTheMomentOfSending < valueToSend then // comment
invalidArg "balanceAtTheMomentOfSending" // comment
invalidArg
"balanceAtTheMomentOfSending" // comment
"some very very long error message" // comment
if valueToSend <= 0m then // comment
invalidArg "valueToSend" "Amount has to be above zero" // comment
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ let expect =
Opts.oneOf
(Optional,
[ Opt.flag [ "third"; "f" ]
Opt.valueWith "new value"
Opt.valueWith
"new value"
[ "fourth"
"ssssssssssssssssssssssssssssssssssssssssssssssssssss" ] ]) ]
args = []
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/TypeProviderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ let ``should handle lines with more than 512 characters``() =
CommonRuntime.ConvertDecimalBack
("", CommonRuntime.GetOptionalValue((let _, _, x = row in x))) |]),
(ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions
@"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" ""
@"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data"
""
"SmallTest.csv"),
"",
'"',
Expand Down
40 changes: 35 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,13 +1346,43 @@ and genExpr astContext synExpr =
else
sepSpace ctx

atCurrentColumn
(genExpr astContext e
+> colPre sepSpace sepSpace es (fun e ->
onlyIf (isCompExpr e) (sepSpace +> sepOpenSFixed +> sepSpace)
let shortExpression =
atCurrentColumn
(genExpr astContext e
+> colPre sepSpace sepSpace es (fun e ->
onlyIf (isCompExpr e) (sepSpace +> sepOpenSFixed +> sepSpace)
+> indent
+> appNlnFun e (indentIfNeeded +> genExpr astContext e)
+> unindent))

let longExpression =
(atCurrentColumn
(genExpr astContext e
+> colPre sepSpace sepSpace es (fun e ->
ifElse (isCompExpr e)
(sepSpace
+> sepOpenSFixed
+> sepSpace
+> indent
+> appNlnFun e (indentIfNeeded +> genExpr astContext e)
+> unindent))
+> unindent)
(indent
+> indentIfNeeded
+> sepNln
+> genExpr astContext e
+> unindent))))

if List.exists (function
| Lambda _
| MatchLambda _
| Paren (Lambda (_))
| Paren (MatchLambda (_))
| MultilineString _
| CompExpr _ -> true
| _ -> false) es then
shortExpression
else
expressionFitsOnRestOfLine shortExpression longExpression

| TypeApp(e, ts) -> genExpr astContext e -- "<" +> col sepComma ts (genType astContext false) -- ">"
| LetOrUses(bs, e) ->
Expand Down