From 3c81b03e5449cecaf92787273308375be1b8953b Mon Sep 17 00:00:00 2001 From: Alexander Schlindwein Date: Wed, 24 Jun 2020 13:33:46 +0200 Subject: [PATCH] Split all parameters of a function application over multiple lines if they exceed page width --- src/Fantomas.Tests/AppTests.fs | 136 +++++++++++++++++- src/Fantomas.Tests/CompilerDirectivesTests.fs | 8 +- .../ComputationExpressionTests.fs | 15 +- src/Fantomas.Tests/ElmishTests.fs | 7 +- .../KeepIfThenInSameLineTests.fs | 3 +- src/Fantomas.Tests/RecordTests.fs | 5 +- src/Fantomas.Tests/TypeProviderTests.fs | 3 +- src/Fantomas/CodePrinter.fs | 35 ++++- 8 files changed, 183 insertions(+), 29 deletions(-) diff --git a/src/Fantomas.Tests/AppTests.fs b/src/Fantomas.Tests/AppTests.fs index aad15157ad..2b16a03e9e 100644 --- a/src/Fantomas.Tests/AppTests.fs +++ b/src/Fantomas.Tests/AppTests.fs @@ -68,11 +68,143 @@ let a s = |> should equal @" let a s = if s <> """" then - printfn """"""fooo + printfn + """"""fooo %s %s %s -%s"""""" (llloooooooooooooooooooooooooo s) s +%s"""""" + (llloooooooooooooooooooooooooo s) + s (llloooooooooooooooooooooooooo s) (llloooooooooooooooooooooooooo s) " + + +[] +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 = + 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 = + lock cacheFiles.CachedNetworkData (fun _ -> + match balance with + | NotAvailable -> NotAvailable + | Cached (balance, time) -> + if compoundBalance < 0.0m then + ReportProblem + compoundBalance + None + currency + address + sessionCachedNetworkData + ()) + () +""" + +[] +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 = + 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 = + lock cacheFiles.CachedNetworkData (fun _ -> + match balance with + | NotAvailable -> NotAvailable + | Cached (balance, time) -> + if compoundBalance < 0.0m then + ReportProblem + looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong + ()) + () +""" + +[] +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 = + 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 = + lock cacheFiles.CachedNetworkData (fun _ -> + match balance with + | NotAvailable -> NotAvailable + | Cached (balance, time) -> + if compoundBalance < 0.0m + then ReportProblem compoundBalance None currency address sessionCachedNetworkData + ()) + () +""" + +[] +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 = + 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 = + lock cacheFiles.CachedNetworkData (fun _ -> + match balance with + | NotAvailable -> NotAvailable + | Cached (balance, time) -> + if compoundBalance < 0.0m + then ReportProblem loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong + ()) + () +""" diff --git a/src/Fantomas.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Tests/CompilerDirectivesTests.fs index ff451064c7..cdf001275b 100644 --- a/src/Fantomas.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Tests/CompilerDirectivesTests.fs @@ -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 @@ -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 diff --git a/src/Fantomas.Tests/ComputationExpressionTests.fs b/src/Fantomas.Tests/ComputationExpressionTests.fs index df9f2afbd9..600622b77a 100644 --- a/src/Fantomas.Tests/ComputationExpressionTests.fs +++ b/src/Fantomas.Tests/ComputationExpressionTests.fs @@ -318,14 +318,13 @@ let private validateLocation = |> prepend newline |> should equal """ let private validateLocation = - createValidatorFor () { - validate (fun l -> l.Id) [ isNotEmptyGuid ] - validate (fun l -> l.Name) [ isNotEmpty; hasMinLengthOf 3 ] - validate (fun l -> fst l.Location) [ isValidLatitude ] - validate (fun l -> snd l.Location) [ isValidLongitude ] - validate (fun l -> l.Price) [ isGreaterThan 0. ] - validate (fun l -> l.Date) [ isNotMinDate ] - validate (fun l -> l.Creator) [ isNotEmpty ] + createValidatorFor () { validate (fun l -> l.Id) [ isNotEmptyGuid ] + validate (fun l -> l.Name) [ isNotEmpty; hasMinLengthOf 3 ] + validate (fun l -> fst l.Location) [ isValidLatitude ] + validate (fun l -> snd l.Location) [ isValidLongitude ] + validate (fun l -> l.Price) [ isGreaterThan 0. ] + validate (fun l -> l.Date) [ isNotMinDate ] + validate (fun l -> l.Creator) [ isNotEmpty ] } """ diff --git a/src/Fantomas.Tests/ElmishTests.fs b/src/Fantomas.Tests/ElmishTests.fs index e3a7fa728e..adf5aad935 100644 --- a/src/Fantomas.Tests/ElmishTests.fs +++ b/src/Fantomas.Tests/ElmishTests.fs @@ -433,10 +433,9 @@ table [ ClassName "table table-striped table-hover mb-0" ] [ tokenDetailRow "ColorClass" (str colorClass) tokenDetailRow "CharClass" (str charClass) tokenDetailRow "Tag" (ofInt tag) - tokenDetailRow "FullMatchedLength" - (span [ ClassName "has-text-weight-semibold" ] [ - ofInt fullMatchedLength - ]) + tokenDetailRow "FullMatchedLength" (span [ ClassName "has-text-weight-semibold" ] [ + ofInt fullMatchedLength + ]) ] ] """ diff --git a/src/Fantomas.Tests/KeepIfThenInSameLineTests.fs b/src/Fantomas.Tests/KeepIfThenInSameLineTests.fs index c6f1e03295..7ab32f272c 100644 --- a/src/Fantomas.Tests/KeepIfThenInSameLineTests.fs +++ b/src/Fantomas.Tests/KeepIfThenInSameLineTests.fs @@ -24,8 +24,7 @@ let ``if only, 825`` () = 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" """ diff --git a/src/Fantomas.Tests/RecordTests.fs b/src/Fantomas.Tests/RecordTests.fs index 65f9ac7146..68693848f7 100644 --- a/src/Fantomas.Tests/RecordTests.fs +++ b/src/Fantomas.Tests/RecordTests.fs @@ -571,9 +571,8 @@ let expect = Opts.oneOf (Optional, [ Opt.flag [ "third"; "f" ] - Opt.valueWith "new value" - [ "fourth" - "ssssssssssssssssssssssssssssssssssssssssssssssssssss" ] ]) ] + Opt.valueWith "new value" [ "fourth" + "ssssssssssssssssssssssssssssssssssssssssssssssssssss" ] ]) ] args = [] commands = [] } """ diff --git a/src/Fantomas.Tests/TypeProviderTests.fs b/src/Fantomas.Tests/TypeProviderTests.fs index aba55fd506..f9153d21da 100644 --- a/src/Fantomas.Tests/TypeProviderTests.fs +++ b/src/Fantomas.Tests/TypeProviderTests.fs @@ -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"), "", '"', diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 476605ef84..9a8bd62e5f 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1322,13 +1322,34 @@ and genExpr astContext synExpr = else sepSpace ctx - atCurrentColumn - (genExpr astContext e - +> colPre sepSpace sepSpace es (fun e -> - onlyIf (isCompExpr e) (sepSpace +> sepOpenSFixed +> sepSpace) - +> indent - +> appNlnFun e (indentIfNeeded +> genExpr astContext e) - +> unindent)) + fun ctx -> + (ifElseCtx + (exceedsWidth ctx.Config.PageWidth + (atCurrentColumn + (genExpr astContext e + +> colPre sepSpace sepSpace es (fun e -> + onlyIf (isCompExpr e) (sepSpace +> sepOpenSFixed +> sepSpace) + +> indent + +> indentIfNeeded + +> genExpr astContext e + +> unindent)))) + (atCurrentColumn + (genExpr astContext e + +> colPre sepSpace sepSpace es (fun e -> + onlyIf (isCompExpr e) (sepSpace +> sepOpenSFixed +> sepSpace) + +> indent + +> indentIfNeeded + +> sepNln + +> genExpr astContext e + +> unindent))) + (atCurrentColumn + (genExpr astContext e + +> colPre sepSpace sepSpace es (fun e -> + onlyIf (isCompExpr e) (sepSpace +> sepOpenSFixed +> sepSpace) + +> indent + +> indentIfNeeded + +> genExpr astContext e + +> unindent)))) ctx | TypeApp(e, ts) -> genExpr astContext e -- "<" +> col sepComma ts (genType astContext false) -- ">" | LetOrUses(bs, e) ->