Skip to content

Commit

Permalink
Apply same union case bar rule to signature files. Fixes fsprojects#1563
Browse files Browse the repository at this point in the history
.
  • Loading branch information
nojaf committed Apr 11, 2021
1 parent 937479a commit 7ef56f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
25 changes: 24 additions & 1 deletion src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ namespace Blah
/// Comment
type Foo =
/// Another
Foo of int
| Foo of int
"""

[<Test>]
Expand Down Expand Up @@ -1516,3 +1516,26 @@ and [<CustomEquality>] Bang =
///
override GetHashCode : unit -> int
"""

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

0 comments on commit 7ef56f7

Please sign in to comment.