Skip to content

Commit

Permalink
Add newline if there is trivia after the bar in a union case. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 12, 2022
1 parent 4a4a853 commit 7b35c89
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Fixed
* Multiline parentheses objectExpression in indexer expression. [#2176](https://github.com/fsprojects/fantomas/issues/2176)
* Blank line over SynEnumCase is restored incorrectly. [#2155](https://github.com/fsprojects/fantomas/issues/2155)
* XML Doc comments on DU cases seem to get shuffled. [#2188](https://github.com/fsprojects/fantomas/issues/2188)
* Comment after bar is lost in single case DU. [#2182](https://github.com/fsprojects/fantomas/issues/2182)

## [4.7.6] - 2022-04-04

Expand Down
27 changes: 27 additions & 0 deletions src/Fantomas.Tests/UnionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -996,3 +996,30 @@ type Meh =
/// When the editor lost focus.
| Few of DateTime
"""

[<Test>]
let ``comment after bar of single case DU, 2182`` () =
formatSourceString
false
"""
type LongIdentWithDots =
| //[<Experimental("This construct is subject to change in future versions of FSharp.Compiler.Service and should only be used if no adequate alternative is available.")>]
LongIdentWithDots of
leadingId: LongIdent *
operatorName: OperatorName option *
trailingId: LongIdent *
dotRanges: range list
"""
config
|> prepend newline
|> should
equal
"""
type LongIdentWithDots =
| //[<Experimental("This construct is subject to change in future versions of FSharp.Compiler.Service and should only be used if no adequate alternative is available.")>]
LongIdentWithDots of
leadingId: LongIdent *
operatorName: OperatorName option *
trailingId: LongIdent *
dotRanges: range list
"""
17 changes: 10 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4212,20 +4212,23 @@ and genUnionCase
(hasVerticalBar: bool)
(UnionCase (ats, px, barRange, _, s, identRange, UnionCaseType fs, range))
=
let shortExpr =
colPre wordOf sepStar fs (genField { astContext with IsUnionField = true } "")
let shortExpr = col sepStar fs (genField { astContext with IsUnionField = true } "")

let longExpr =
wordOf
+> indent
indent
+> sepNln
+> atCurrentColumn (col (sepStar +> sepNln) fs (genField { astContext with IsUnionField = true } ""))
+> unindent

genPreXmlDoc px
+> ifElse hasVerticalBar (genTriviaForOptionOr SynUnionCase_Bar barRange sepBar) sepNone
+> genOnelinerAttributes astContext ats
+> genTriviaFor Ident_ identRange !-s
+> genTriviaForOptionOr SynUnionCase_Bar barRange (ifElse hasVerticalBar sepBar sepNone)
+> atCurrentColumn (
// If the bar has a comment after, add a newline and print the identifier on the same column on the next line.
sepNlnWhenWriteBeforeNewlineNotEmpty sepNone
+> genOnelinerAttributes astContext ats
+> genTriviaFor Ident_ identRange !-s
+> onlyIf (List.isNotEmpty fs) wordOf
)
+> onlyIf (List.isNotEmpty fs) (expressionFitsOnRestOfLine shortExpr longExpr)
|> genTriviaFor SynUnionCase_ range

Expand Down

0 comments on commit 7b35c89

Please sign in to comment.