Skip to content

Commit

Permalink
Formatting single case DU on the same line. fsprojects#192
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Mar 7, 2018
1 parent 8dc6b9e commit 83bdc97
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
41 changes: 39 additions & 2 deletions src/Fantomas.Tests/UnionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
[<EntryPoint>]
let main argv =
Expand All @@ -154,4 +153,42 @@ type uColor =
let col3 =
Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue<uint32, uColor>(2u)
"""

[<Test>]
let ``Single case DUs on same line`` () =
formatSourceString false """
type CustomerId =
| CustomerId of int
""" config
|> prepend newline
|> should equal """
type CustomerId = CustomerId of int
"""

[<Test>]
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
"""

[<Test>]
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
"""
14 changes: 12 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 83bdc97

Please sign in to comment.