diff --git a/src/Fantomas.Tests/UnionTests.fs b/src/Fantomas.Tests/UnionTests.fs index e6dda61d18..590995b4ed 100644 --- a/src/Fantomas.Tests/UnionTests.fs +++ b/src/Fantomas.Tests/UnionTests.fs @@ -126,8 +126,7 @@ let main argv = | _ -> 0""" config |> prepend newline |> should equal """ -type TestUnion = - | Test of A : int * B : int +type TestUnion = Test of A : int * B : int [] let main argv = @@ -154,4 +153,42 @@ type uColor = let col3 = Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue(2u) +""" + +[] +let ``Single case DUs on same line`` () = + formatSourceString false """ +type CustomerId = + | CustomerId of int + """ config + |> prepend newline + |> should equal """ +type CustomerId = CustomerId of int +""" + +[] +let ``Single case DU with private access modifier`` () = + formatSourceString false """ +type CustomerId = + private + | CustomerId of int + """ config + |> prepend newline + |> should equal """ +type CustomerId = private CustomerId of int +""" + +[] +let ``Single case DU with member should be on a newline`` () = + formatSourceString false """ +type CustomerId = + | CustomerId of int + member this.Test() = + printfn "%A" this + """ config + |> prepend newline + |> should equal """ +type CustomerId = + | CustomerId of int + member this.Test() = printfn "%A" this """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 2baa58c40d..4d5349569f 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -8,6 +8,7 @@ open Fantomas open Fantomas.FormatConfig open Fantomas.SourceParser open Fantomas.SourceTransformer +open Fantomas.SourceTransformer /// This type consists of contextual information which is important for formatting type ASTContext = @@ -690,9 +691,18 @@ and genTypeDefn astContext (TypeDef(ats, px, ao, tds, tcs, tdr, ms, s)) = +> unindent | Simple(TDSRUnion(ao', xs)) -> + let unionCases = + match xs with + | [] -> id + | [x] when List.isEmpty ms -> + indent +> sepSpace +> opt sepSpace ao' genAccess + +> genUnionCase { astContext with HasVerticalBar = false } x + | xs -> + indent +> sepNln +> opt sepNln ao' genAccess + +> col sepNln xs (genUnionCase { astContext with HasVerticalBar = true }) + typeName +> sepEq - +> indent +> sepNln +> opt sepNln ao' genAccess - +> col sepNln xs (genUnionCase { astContext with HasVerticalBar = true }) + +> unionCases +> genMemberDefnList { astContext with IsInterface = false } ms +> unindent