From 098a395e303dd3505da3cd94a91f11487d8d437e Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Mon, 8 Jun 2020 11:29:50 +0200 Subject: [PATCH] Format long member binding and constructors according to the F# style guide. (#898) --- src/Fantomas.Tests/FunctionDefinitionTests.fs | 3 +- src/Fantomas.Tests/TypeDeclarationTests.fs | 128 ++++++++++-------- src/Fantomas/CodePrinter.fs | 59 +++----- 3 files changed, 92 insertions(+), 98 deletions(-) diff --git a/src/Fantomas.Tests/FunctionDefinitionTests.fs b/src/Fantomas.Tests/FunctionDefinitionTests.fs index 3a7ffdc049..cd3e371cd6 100644 --- a/src/Fantomas.Tests/FunctionDefinitionTests.fs +++ b/src/Fantomas.Tests/FunctionDefinitionTests.fs @@ -280,8 +280,7 @@ open Accessibility [] extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [] System.Object [] - rgvarChildren, int* pcObtained) + 4s)>] System.Object [] rgvarChildren, int* pcObtained) """ [] diff --git a/src/Fantomas.Tests/TypeDeclarationTests.fs b/src/Fantomas.Tests/TypeDeclarationTests.fs index 38d46fdeca..ede06d982b 100644 --- a/src/Fantomas.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Tests/TypeDeclarationTests.fs @@ -1061,11 +1061,9 @@ let ``long type members should have parameters on separate lines, 719`` () = |> prepend newline |> should equal """ type C () = - member __.LongMethodWithLotsOfParameters( - aVeryLongType: AVeryLongTypeThatYouNeedToUse, - aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse, - aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse) - = + member __.LongMethodWithLotsOfParameters(aVeryLongType: AVeryLongTypeThatYouNeedToUse, + aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse, + aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse) = aVeryLongType aSecondVeryLongType aThirdVeryLongType """ @@ -1077,12 +1075,9 @@ let ``long type member with return type should have parameters on separate lines |> prepend newline |> should equal """ type C () = - member __.LongMethodWithLotsOfParameters( - aVeryLongType: AVeryLongTypeThatYouNeedToUse, - aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse, - aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse) - : int - = + member __.LongMethodWithLotsOfParameters(aVeryLongType: AVeryLongTypeThatYouNeedToUse, + aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse, + aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse): int = aVeryLongType aSecondVeryLongType aThirdVeryLongType """ @@ -1093,11 +1088,9 @@ let ``long constructors should have parameters on separate lines`` () = """ ({ config with SpaceBeforeClassConstructor = true }) |> prepend newline |> should equal """ -type C ( - aVeryLongType: AVeryLongTypeThatYouNeedToUse, - aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse, - aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse) - = +type C (aVeryLongType: AVeryLongTypeThatYouNeedToUse, + aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse, + aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse) = member this.X = 42 """ @@ -1129,10 +1122,8 @@ let ``keep correct indentation after multiline member definition, 845`` () = |> prepend newline |> should equal """ type SomeType() = - member SomeMember( - looooooooooooooooooooooooooooooooooong1: A, - looooooooooooooooooooooooooooooooooong2: A) - = + member SomeMember(looooooooooooooooooooooooooooooooooong1: A, + looooooooooooooooooooooooooooooooooong2: A) = printfn "a" "a" @@ -1152,11 +1143,8 @@ let ``keep correct indentation after multiline typed member definition`` () = |> prepend newline |> should equal """ type SomeType() = - member SomeMember( - looooooooooooooooooooooooooooooooooong1: A, - looooooooooooooooooooooooooooooooooong2: A) - : string - = + member SomeMember(looooooooooooooooooooooooooooooooooong1: A, + looooooooooooooooooooooooooooooooooong2: A): string = printfn "a" "a" @@ -1173,11 +1161,8 @@ let ``split multiple parameters over multiple lines`` () = |> prepend newline |> should equal """ type SomeType = - static member SomeMember - (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) - (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string) - : string - = + static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) + (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string): string = printfn "a" "b" """ @@ -1194,11 +1179,8 @@ let ``split multiple parameters over multiple lines and have correct indentation |> prepend newline |> should equal """ type SomeType = - static member SomeMember - (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) - (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string) - : string - = + static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) + (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string): string = printfn "a" "b" @@ -1215,10 +1197,7 @@ 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" """ @@ -1233,9 +1212,7 @@ let ``member with one long parameter and no return type, 850`` () = |> prepend newline |> should equal """ type SomeType = - static member SomeMember - loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 - = + static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 = printfn "a" "b" """ @@ -1253,21 +1230,14 @@ let ``multiple members with one long parameter`` () = |> prepend newline |> should equal """ type SomeType = - static member SomeMember - loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 - = + static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 = printfn "a" "b" - static member Serialize( - loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: SomeType) - = + static member Serialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: SomeType) = Encode.string v.Meh - static member Deserialize( - loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee) - : SomeType - = + static member Deserialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee): SomeType = Decode.SomeType loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee """ @@ -1286,10 +1256,54 @@ type INotifications<'a, 'b, 'c, 'd, 'e> = class end -type DeviceNotificationHandler<'Notification, 'CallbackId, 'RegisterInputData, 'RegisterOutputData, 'UnregisterOutputData> - private (client: INotifications<'Notification, 'CallbackId, 'RegisterInputData, 'RegisterOutputData, 'UnregisterOutputData>, - callbackId: 'CallbackId, - validateUnregisterOutputData: 'UnregisterOutputData -> unit) - = +type DeviceNotificationHandler<'Notification, 'CallbackId, 'RegisterInputData, 'RegisterOutputData, 'UnregisterOutputData> private (client: INotifications<'Notification, 'CallbackId, 'RegisterInputData, 'RegisterOutputData, 'UnregisterOutputData>, + callbackId: 'CallbackId, + validateUnregisterOutputData: 'UnregisterOutputData -> unit) = let a = 5 """ + + +[] +let ``long type members should be in multiple lines, 868`` () = + formatSourceString false """ +type C() = + member _.LongMethodWithLotsOfParameters(aVeryLongType: int, aSecondVeryLongType: int, aThirdVeryLongType: int) : int = + aVeryLongType + aSecondVeryLongType + aThirdVeryLongType +""" { config with PageWidth = 80; SpaceBeforeColon = true; MaxInfixOperatorExpression = 80 } + |> prepend newline + |> should equal """ +type C() = + member _.LongMethodWithLotsOfParameters(aVeryLongType : int, + aSecondVeryLongType : int, + aThirdVeryLongType : int) : int = + aVeryLongType + aSecondVeryLongType + aThirdVeryLongType +""" + +[] +let ``long type members should be in multiple lines, no return type`` () = + formatSourceString false """ +type C() = + member _.LongMethodWithLotsOfParameters(aVeryLongType: int, aSecondVeryLongType: int, aThirdVeryLongType: int) = + aVeryLongType + aSecondVeryLongType + aThirdVeryLongType +""" { config with PageWidth = 80; SpaceBeforeColon = true; MaxInfixOperatorExpression = 80 } + |> prepend newline + |> should equal """ +type C() = + member _.LongMethodWithLotsOfParameters(aVeryLongType : int, + aSecondVeryLongType : int, + aThirdVeryLongType : int) = + aVeryLongType + aSecondVeryLongType + aThirdVeryLongType +""" + +[] +let ``long type constructors should be in multiple lines, 868`` () = + formatSourceString false """ +type VersionMismatchDuringDeserializationException(message: string, innerException: System.Exception) = + inherit System.Exception(message, innerException) +""" { config with PageWidth = 80; SpaceBeforeColon = true } + |> prepend newline + |> should equal """ +type VersionMismatchDuringDeserializationException(message : string, + innerException : System.Exception) = + inherit System.Exception(message, innerException) +""" \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index b283402024..fd02e2d39c 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -681,19 +681,17 @@ and genMemberBinding astContext b = match e with | TypedExpr(Typed, e, t) -> genAttributesAndXmlDoc - +> leadingExpressionIsMultiline prefix (fun prefixIsLong -> - ifElse - prefixIsLong - (indent +> sepNln +> sepColon +> genType astContext false t +> sepNln +> !- "=" +> sepNln +> genExpr astContext e +> unindent) - (sepColon +> genType astContext false t +> sepEq +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e))) + +> prefix + +> sepColon + +> genType astContext false t + +> sepEq + +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e) + | e -> genAttributesAndXmlDoc - +> leadingExpressionIsMultiline prefix - (fun prefixIsLong -> - ifElse - prefixIsLong - (indent +> sepNln +> !- "=" +> sepNln +> genExpr astContext e +> unindent) - (sepEq +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e))) + +> prefix + +> sepEq + +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e) | ExplicitCtor(ats, px, ao, p, e, so) -> let prefix = @@ -2048,11 +2046,8 @@ and genTypeDefn astContext (TypeDef(ats, px, ao, tds, tcs, tdr, ms, s, preferPos | ObjectModel(_, MemberDefnList(impCtor, others), _) -> typeName - +> leadingExpressionIsMultiline - (opt sepNone impCtor (fun mdf -> - sepSpaceBeforeClassConstructor - +> genMemberDefn { astContext with InterfaceRange = None } mdf)) - (fun longCtor -> ifElse longCtor (indent +> sepNln +> !- "=" +> unindent) sepEq) + +> opt sepNone impCtor (fun mdf -> sepSpaceBeforeClassConstructor +> genMemberDefn { astContext with InterfaceRange = None } mdf) + +> sepEq +> indent +> genMemberDefnList { astContext with InterfaceRange = None } others +> unindent @@ -2552,24 +2547,10 @@ and genMemberDefn astContext node = +> sepOpenT +> col sepComma (simplePats ps) (genSimplePat astContext) +> sepCloseT) - (match ao with - | Some ao -> - indent - +> sepNln - +> sepSpace - +> genAccess ao - +> sepSpace - +> sepOpenT - +> atCurrentColumn (col (sepComma +> sepNln) (simplePats ps) (genSimplePat astContext)) - +> sepCloseT - +> unindent - | None -> - sepOpenT - +> indent - +> sepNln - +> col (sepComma +> sepNln) (simplePats ps) (genSimplePat astContext) - +> unindent - +> sepCloseT) + (optPre sepSpace sepSpace ao genAccess + +> sepOpenT + +> atCurrentColumn (col (sepComma +> sepNln) (simplePats ps) (genSimplePat astContext)) + +> sepCloseT) // In implicit constructor, attributes should come even before access qualifiers ifElse ats.IsEmpty sepNone (sepSpace +> genOnelinerAttributes astContext ats) @@ -2697,7 +2678,7 @@ and genPat astContext pat = ifElse astContext.IsCStylePattern (genTypeByLookup astContext t +> sepSpace +> genPat astContext p) (genPat astContext p +> sepColon +> genType astContext false t) | PatNamed(ao, PatNullary PatWild, s) -> - autoIndentAndNlnIfExpressionExceedsPageWidth (opt sepSpace ao genAccess +> infixOperatorFromTrivia pat.Range s) + opt sepSpace ao genAccess +> infixOperatorFromTrivia pat.Range s | PatNamed(ao, p, s) -> opt sepSpace ao genAccess +> genPat astContext p -- sprintf " as %s" s | PatLongIdent(ao, s, ps, tpso) -> let aoc = opt sepSpace ao genAccess @@ -2719,9 +2700,9 @@ and genPat astContext pat = let genName = aoc -- s +> tpsoc +> sepSpace let genParameters = - (expressionFitsOnRestOfLine - (atCurrentColumn (col (ifElse hasBracket sepSemi sepSpace) ps (genPatWithIdent astContext)) +> indent) - (indent +> sepNln +> col sepNln ps (genPatWithIdent astContext)) +> unindent) + expressionFitsOnRestOfLine + (atCurrentColumn (col (ifElse hasBracket sepSemi sepSpace) ps (genPatWithIdent astContext))) + (atCurrentColumn (col sepNln ps (genPatWithIdent astContext))) genName +> ifElse hasBracket sepOpenT sepNone @@ -2733,7 +2714,7 @@ and genPat astContext pat = | PatTuple ps -> expressionFitsOnRestOfLine (col sepComma ps (genPat astContext)) - (autoIndentAndNlnIfExpressionExceedsPageWidth (col (sepComma +> sepNln) ps (genPat astContext))) + (atCurrentColumn (col (sepComma +> sepNln) ps (genPat astContext))) | PatStructTuple ps -> !- "struct " +> sepOpenT +> atCurrentColumn (colAutoNlnSkip0 sepComma ps (genPat astContext)) +> sepCloseT | PatSeq(PatList, ps) ->