diff --git a/src/Fantomas.Tests/AppTests.fs b/src/Fantomas.Tests/AppTests.fs index c4521d9f07..c7ffe1179d 100644 --- a/src/Fantomas.Tests/AppTests.fs +++ b/src/Fantomas.Tests/AppTests.fs @@ -77,7 +77,6 @@ let a s = (llloooooooooooooooooooooooooo s) " - [] let ``should split parameters over multiple lines when they exceed page width``() = formatSourceString false """module Caching = @@ -98,7 +97,8 @@ let ``should split parameters over multiple lines when they exceed page width``( module Caching = type MainCache() = member __.RetrieveLastCompoundBalance (address: PublicAddress) - (currency: Currency): NotFresh = + (currency: Currency) + : NotFresh = lock cacheFiles.CachedNetworkData (fun _ -> match balance with | NotAvailable -> NotAvailable @@ -134,7 +134,8 @@ let ``should split single parameter over multiple lines when it exceeds page wid module Caching = type MainCache() = member __.RetrieveLastCompoundBalance (address: PublicAddress) - (currency: Currency): NotFresh = + (currency: Currency) + : NotFresh = lock cacheFiles.CachedNetworkData (fun _ -> match balance with | NotAvailable -> NotAvailable diff --git a/src/Fantomas.Tests/TypeDeclarationTests.fs b/src/Fantomas.Tests/TypeDeclarationTests.fs index 50a4256b05..738daf7120 100644 --- a/src/Fantomas.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Tests/TypeDeclarationTests.fs @@ -1077,7 +1077,8 @@ let ``long type member with return type should have parameters on separate lines type C () = member __.LongMethodWithLotsOfParameters(aVeryLongType: AVeryLongTypeThatYouNeedToUse, aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse, - aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse): int = + aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse) + : int = aVeryLongType aSecondVeryLongType aThirdVeryLongType """ @@ -1144,7 +1145,8 @@ let ``keep correct indentation after multiline typed member definition`` () = |> should equal """ type SomeType() = member SomeMember(looooooooooooooooooooooooooooooooooong1: A, - looooooooooooooooooooooooooooooooooong2: A): string = + looooooooooooooooooooooooooooooooooong2: A) + : string = printfn "a" "a" @@ -1162,7 +1164,8 @@ let ``split multiple parameters over multiple lines`` () = |> should equal """ type SomeType = static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) - (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string): string = + (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string) + : string = printfn "a" "b" """ @@ -1180,7 +1183,8 @@ let ``split multiple parameters over multiple lines and have correct indentation |> should equal """ type SomeType = static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) - (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string): string = + (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string) + : string = printfn "a" "b" @@ -1197,7 +1201,8 @@ let ``member with one long parameter and return type, 850`` () = |> prepend newline |> should equal """ type SomeType = - static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string = + static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 + : string = printfn "a" "b" """ @@ -1237,7 +1242,8 @@ type SomeType = static member Serialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: SomeType) = Encode.string v.Meh - static member Deserialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee): SomeType = + static member Deserialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee) + : SomeType = Decode.SomeType loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee """ @@ -1275,7 +1281,8 @@ type C() = type C() = member _.LongMethodWithLotsOfParameters(aVeryLongType : int, aSecondVeryLongType : int, - aThirdVeryLongType : int) : int = + aThirdVeryLongType : int) + : int = aVeryLongType + aSecondVeryLongType + aThirdVeryLongType """ diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 664f799d23..a91b087b98 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -679,20 +679,47 @@ and genMemberBinding astContext b = let prefix = genMemberFlagsForMemberBinding astContext mf b.RangeOfBindingAndRhs - +> ifElse isInline (!- "inline ") sepNone +> opt sepSpace ao genAccess +> genPat ({ astContext with IsMemberDefinition = true }) p + +> ifElse isInline (!- "inline ") sepNone +> opt sepSpace ao genAccess match e with | TypedExpr(Typed, e, t) -> - genAttributesAndXmlDoc - +> prefix - +> sepColon - +> genType astContext false t - +> sepEq - +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e) + let genName, genParameters, spaceBeforeColon = + match p with + | PatLongIdent(ao, s, ps, tpso) -> + let aoc = opt sepSpace ao genAccess + let tpsoc = opt sepNone tpso (fun (ValTyparDecls(tds, _, tcs)) -> genTypeParamPostfix astContext tds tcs) + let s = if s = "``new``" then "new" else s + let hasBracket = ps |> Seq.map snd |> Seq.exists hasParenInPat + let multipleParameters = List.length ps > 1 + let spaceAfter ctx = onlyIf (ctx.Config.SpaceBeforeMember || multipleParameters || not hasBracket) sepSpace ctx + let name = (aoc -- s +> tpsoc +> spaceAfter) + + let parameters = + expressionFitsOnRestOfLine + (atCurrentColumn (col sepSpace ps (genPatWithIdent astContext))) + (atCurrentColumn (col sepNln ps (genPatWithIdent astContext))) + + name, parameters, onlyIf (hasBracket && not multipleParameters) !- " " + | _ -> sepNone, sepNone, sepNone + + let memberDefinition = + prefix + +> expressionFitsOnRestOfLine + (genName +> genParameters +> sepColon +> genType astContext false t +> sepEq) + (genName +> atCurrentColumn (genParameters +> sepNln +> spaceBeforeColon +> sepColon +> genType astContext false t +> sepEq)) + genAttributesAndXmlDoc + +> leadingExpressionIsMultiline + memberDefinition + (fun mdLong -> + ifElse + mdLong + (indent +> sepNln +> genExpr astContext e +> unindent) + (sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e))) | e -> genAttributesAndXmlDoc +> prefix + +> genPat ({ astContext with IsMemberDefinition = true }) p +> sepEq +> sepSpace +> (fun ctx -> (isShortExpressionOrAddIndentAndNewline (if isFunctionBinding p then ctx.Config.MaxFunctionBindingWidth else ctx.Config.MaxValueBindingWidth) (genExpr astContext e)) ctx)