Skip to content

Commit

Permalink
Merge pull request #521 from nojaf/fix-520
Browse files Browse the repository at this point in the history
Fix: Don't duplicate newline between recursive types
  • Loading branch information
nojaf authored Oct 19, 2019
2 parents 8ee816f + 7bfefff commit 97adf4f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
96 changes: 96 additions & 0 deletions src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,100 @@ type C'() =
|> should equal """
type C'() =
member _.M() = ()
"""

[<Test>]
let ``don't add additional newlines between recursive type declarations, 520`` () =
formatSourceString false """module Game
type Details =
{ Name: string
Description: string }
type Item =
{ Details: Details }
type Exit =
| Passable of Details * desitnation: Room
| Locked of Details * key: Item * next: Exit
| NoExit of Details option
and Exits =
{ North: Exit
South: Exit
East: Exit
West: Exit }
and Room =
{ Details: Details
Items: Item list
Exits: Exits }
""" config
|> prepend newline
|> should equal """
module Game
type Details =
{ Name: string
Description: string }
type Item =
{ Details: Details }
type Exit =
| Passable of Details * desitnation: Room
| Locked of Details * key: Item * next: Exit
| NoExit of Details option
and Exits =
{ North: Exit
South: Exit
East: Exit
West: Exit }
and Room =
{ Details: Details
Items: Item list
Exits: Exits }
"""

[<Test>]
let ``don't add additional newlines between recursive type declarations with attributes, 520`` () =
formatSourceString false """module Game
type Exit =
| Passable of Details * desitnation: Room
| Locked of Details * key: Item * next: Exit
| NoExit of Details option
and Exits =
{ North: Exit
South: Exit
East: Exit
West: Exit }
and [<Marker()>] Room =
{ Details: Details
Items: Item list
Exits: Exits }
""" config
|> prepend newline
|> should equal """
module Game
type Exit =
| Passable of Details * desitnation: Room
| Locked of Details * key: Item * next: Exit
| NoExit of Details option
and Exits =
{ North: Exit
South: Exit
East: Exit
West: Exit }
and [<Marker>] Room =
{ Details: Details
Items: Item list
Exits: Exits }
"""
4 changes: 2 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ and genModuleDecl astContext node =
match List.tryHead ts with
| Some tsh -> sepNln +> sepNlnConsideringTriviaContentBefore tsh.Range
| None -> rep 2 sepNln

genTypeDefn { astContext with IsFirstChild = true } t
+> colPre sepTs (rep 2 sepNln) ts (genTypeDefn { astContext with IsFirstChild = false })
+> colPreEx sepTs (fun (ty: SynTypeDefn) -> sepNln +> sepNlnConsideringTriviaContentBefore ty.Range) ts (genTypeDefn { astContext with IsFirstChild = false })
| md ->
failwithf "Unexpected module declaration: %O" md
|> genTrivia node.Range
Expand Down
4 changes: 4 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ let internal colPre f2 f1 (c : seq<'T>) f (ctx : Context) =
if Seq.isEmpty c then ctx
else col f1 c f (f2 ctx)

let internal colPreEx f2 f1 (c : seq<'T>) f (ctx : Context) =
if Seq.isEmpty c then ctx
else colEx f1 c f (f2 ctx)

/// If there is a value, apply f and f' accordingly, otherwise do nothing
let internal opt (f' : Context -> _) o f (ctx : Context) =
match o with
Expand Down

0 comments on commit 97adf4f

Please sign in to comment.