Skip to content

Commit 38e1555

Browse files
Fix duplicated trivias in combination with double nested try-with statements #1969 (#1971)
* fix WIP draft * fix test name to avoid confusion * resolve some review comments * cleanup * nits * reformat using 'dotnet fantomas src -r' * follow test code conventions * cover one more case
1 parent 8d864c3 commit 38e1555

File tree

4 files changed

+128
-3
lines changed

4 files changed

+128
-3
lines changed

src/Fantomas.Tests/CommentTests.fs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,3 +1599,89 @@ type TorDirectory =
15991599
}
16001600
}
16011601
"""
1602+
1603+
[<Test>]
1604+
let ``double try-with, comment before inner 'with' not duplicated, 1969`` () =
1605+
formatSourceString
1606+
false
1607+
"""
1608+
try
1609+
try
1610+
()
1611+
// xxx
1612+
with
1613+
| _ -> ()
1614+
with
1615+
| _ -> ()
1616+
"""
1617+
config
1618+
|> prepend newline
1619+
|> should
1620+
equal
1621+
"""
1622+
try
1623+
try
1624+
()
1625+
// xxx
1626+
with
1627+
| _ -> ()
1628+
with
1629+
| _ -> ()
1630+
"""
1631+
1632+
[<Test>]
1633+
let ``comment shold not be lost`` () =
1634+
formatSourceString
1635+
false
1636+
"""
1637+
try
1638+
a
1639+
// comment
1640+
with
1641+
1642+
1643+
| b -> c
1644+
"""
1645+
config
1646+
|> prepend newline
1647+
|> should
1648+
equal
1649+
"""
1650+
try
1651+
a
1652+
// comment
1653+
with
1654+
1655+
1656+
| b -> c
1657+
"""
1658+
1659+
[<Test>]
1660+
let ``nested try/with with comment on with`` () =
1661+
formatSourceString
1662+
false
1663+
"""
1664+
try
1665+
a
1666+
with
1667+
| b ->
1668+
try c
1669+
// inner comment
1670+
with
1671+
| d -> ()
1672+
"""
1673+
config
1674+
|> prepend newline
1675+
|> should
1676+
equal
1677+
"""
1678+
try
1679+
a
1680+
with
1681+
| b ->
1682+
try
1683+
c
1684+
// inner comment
1685+
with
1686+
| d -> ()
1687+
"""

src/Fantomas.Tests/CompilerDirectivesTests.fs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,3 +2659,36 @@ let ``indented #if directive inside another non-indented #if directive should fo
26592659
#endif
26602660
#endif
26612661
"""
2662+
2663+
[<Test>]
2664+
let ``double try-with, inner #if directive should not throw error, 1969`` () =
2665+
formatSourceString
2666+
false
2667+
"""
2668+
try
2669+
try
2670+
()
2671+
#if FOO
2672+
()
2673+
#endif
2674+
with
2675+
| _ -> ()
2676+
with
2677+
| _ -> ()
2678+
"""
2679+
config
2680+
|> prepend newline
2681+
|> should
2682+
equal
2683+
"""
2684+
try
2685+
try
2686+
()
2687+
#if FOO
2688+
()
2689+
#endif
2690+
with
2691+
| _ -> ()
2692+
with
2693+
| _ -> ()
2694+
"""

src/Fantomas/CodePrinter.fs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,14 +2023,20 @@ and genExpr astContext synExpr ctx =
20232023

20242024
atCurrentColumn (colWithNlnWhenItemIsMultilineUsingConfig items) ctx
20252025
// Could customize a bit if e is single line
2026-
| TryWith (e, cs) ->
2026+
| TryWith (e, mWithToLast, cs) ->
20272027
atCurrentColumn (
20282028
kw TRY !- "try "
20292029
+> indent
20302030
+> sepNln
20312031
+> genExpr astContext e
20322032
+> unindent
2033-
+> kw WITH !+~ "with"
2033+
+> (fun ctx ->
2034+
let lookupRange =
2035+
ctx.MkRangeWith
2036+
(mWithToLast.StartLine, mWithToLast.StartColumn)
2037+
(mWithToLast.StartLine, mWithToLast.StartColumn + 3)
2038+
2039+
tokN lookupRange WITH !+~ "with" ctx)
20342040
+> indentOnWith
20352041
+> sepNln
20362042
+> col sepNln cs (genClause astContext true)

src/Fantomas/SourceParser.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ let (|LongIdentSet|_|) =
10731073

10741074
let (|TryWith|_|) =
10751075
function
1076-
| SynExpr.TryWith (e, _, cs, _, _, _, _) -> Some(e, cs)
1076+
| SynExpr.TryWith (e, _, cs, mWithToLast, _, _, _) -> Some(e, mWithToLast, cs)
10771077
| _ -> None
10781078

10791079
let (|TryFinally|_|) =

0 commit comments

Comments
 (0)