Skip to content

Commit 7b35c89

Browse files
authored
Add newline if there is trivia after the bar in a union case. Fixes #2182. (#2199)
1 parent 4a4a853 commit 7b35c89

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

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

811
## [4.7.6] - 2022-04-04
912

src/Fantomas.Tests/UnionTests.fs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,3 +996,30 @@ type Meh =
996996
/// When the editor lost focus.
997997
| Few of DateTime
998998
"""
999+
1000+
[<Test>]
1001+
let ``comment after bar of single case DU, 2182`` () =
1002+
formatSourceString
1003+
false
1004+
"""
1005+
type LongIdentWithDots =
1006+
| //[<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.")>]
1007+
LongIdentWithDots of
1008+
leadingId: LongIdent *
1009+
operatorName: OperatorName option *
1010+
trailingId: LongIdent *
1011+
dotRanges: range list
1012+
"""
1013+
config
1014+
|> prepend newline
1015+
|> should
1016+
equal
1017+
"""
1018+
type LongIdentWithDots =
1019+
| //[<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.")>]
1020+
LongIdentWithDots of
1021+
leadingId: LongIdent *
1022+
operatorName: OperatorName option *
1023+
trailingId: LongIdent *
1024+
dotRanges: range list
1025+
"""

src/Fantomas/CodePrinter.fs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4212,20 +4212,23 @@ and genUnionCase
42124212
(hasVerticalBar: bool)
42134213
(UnionCase (ats, px, barRange, _, s, identRange, UnionCaseType fs, range))
42144214
=
4215-
let shortExpr =
4216-
colPre wordOf sepStar fs (genField { astContext with IsUnionField = true } "")
4215+
let shortExpr = col sepStar fs (genField { astContext with IsUnionField = true } "")
42174216

42184217
let longExpr =
4219-
wordOf
4220-
+> indent
4218+
indent
42214219
+> sepNln
42224220
+> atCurrentColumn (col (sepStar +> sepNln) fs (genField { astContext with IsUnionField = true } ""))
42234221
+> unindent
42244222

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

0 commit comments

Comments
 (0)