diff --git a/src/Fantomas.Tests/TypeDeclarationTests.fs b/src/Fantomas.Tests/TypeDeclarationTests.fs index 8be698805d..eed3cb4fba 100644 --- a/src/Fantomas.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Tests/TypeDeclarationTests.fs @@ -904,4 +904,100 @@ type C'() = |> should equal """ type C'() = member _.M() = () +""" + +[] +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 } +""" + +[] +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 [] 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 [] Room = + { Details: Details + Items: Item list + Exits: Exits } """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index e8d49a68f0..673b1b7719 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -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 diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index 57a8e43dc2..02ecf2dfae 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -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