diff --git a/src/Fantomas.Tests/SignatureTests.fs b/src/Fantomas.Tests/SignatureTests.fs index 3a8c58ab05..9b33c82882 100644 --- a/src/Fantomas.Tests/SignatureTests.fs +++ b/src/Fantomas.Tests/SignatureTests.fs @@ -855,7 +855,7 @@ namespace Blah /// Comment type Foo = /// Another - Foo of int + | Foo of int """ [] @@ -1516,3 +1516,26 @@ and [] Bang = /// override GetHashCode : unit -> int """ + +[] +let ``xml comment before SynTypeDefnSimpleRepr.Union should keep bar, 1563`` () = + formatSourceString + true + """ +namespace Baz + +type Foo = + /// Hi! + | Bar of int +""" + config + |> prepend newline + |> should + equal + """ +namespace Baz + +type Foo = + /// Hi! + | Bar of int +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 4aca11372c..5070adff3a 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -3790,25 +3790,33 @@ and genSigTypeDefn astContext (SigTypeDef (ats, px, ao, tds, tcs, tdr, ms, s, pr // Add newline after un-indent to be spacing-correct +> unindent - | SigSimple (TDSRUnion (ao', xs)) -> + | SigSimple (TDSRUnion (ao', xs) as unionNode) -> + let hasLeadingTrivia (t: TriviaNode) = + RangeHelpers.rangeEq t.Range unionNode.Range + && not (List.isEmpty t.ContentBefore) + let unionCases = match xs with | [] -> id - | [ UnionCase (attrs, _, _, _, UnionCaseType fs) as x ] when List.isEmpty ms -> - let hasVerticalBar = - Option.isSome ao' + | [ UnionCase (attrs, _, _, _, UnionCaseType fields) as x ] when List.isEmpty ms -> + let hasVerticalBar (ctx: Context) = + (Option.isSome ao' && List.length fields <> 1) + || (Map.tryFindOrEmptyList SynTypeDefnSimpleRepr_Union ctx.TriviaMainNodes) + |> List.exists hasLeadingTrivia || not (List.isEmpty attrs) - || List.isEmpty fs + || List.isEmpty fields let expr = genTriviaFor SynTypeDefnSimpleRepr_Union tdr.Range (opt sepSpace ao' genAccess - +> genUnionCase - { astContext with - HasVerticalBar = hasVerticalBar } - x) + +> (fun ctx -> + genUnionCase + { astContext with + HasVerticalBar = hasVerticalBar ctx } + x + ctx)) expressionFitsOnRestOfLine (indent +> sepSpace +> expr) (indent +> sepNln +> expr) | xs ->