Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Don't duplicate newline between recursive types #521

Merged
merged 2 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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