Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,3 +1599,59 @@ type TorDirectory =
}
}
"""

[<Test>]
let ``double try-with, comment before inner 'with' not duplicated, 1969`` () =
formatSourceString
false
"""
try
try
()
// xxx
with
| _ -> ()
with
| _ -> ()
"""
config
|> prepend newline
|> should
equal
"""
try
try
()
// xxx
with
| _ -> ()
with
| _ -> ()
"""

[<Test>]
let ``comment shold not be lost`` () =
formatSourceString
false
"""
try
a
// comment
with


| b -> c
"""
config
|> prepend newline
|> should
equal
"""
try
a
// comment
with


| b -> c
"""
33 changes: 33 additions & 0 deletions src/Fantomas.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2659,3 +2659,36 @@ let ``indented #if directive inside another non-indented #if directive should fo
#endif
#endif
"""

[<Test>]
let ``double try-with, inner #if directive should not throw error, 1969`` () =
formatSourceString
false
"""
try
try
()
#if FOO
()
#endif
with
| _ -> ()
with
| _ -> ()
"""
config
|> prepend newline
|> should
equal
"""
try
try
()
#if FOO
()
#endif
with
| _ -> ()
with
| _ -> ()
"""
4 changes: 2 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,14 +2023,14 @@ and genExpr astContext synExpr ctx =

atCurrentColumn (colWithNlnWhenItemIsMultilineUsingConfig items) ctx
// Could customize a bit if e is single line
| TryWith (e, cs) ->
| TryWith (e, mWithToLast, cs) ->
atCurrentColumn (
kw TRY !- "try "
+> indent
+> sepNln
+> genExpr astContext e
+> unindent
+> kw WITH !+~ "with"
+> tokN mWithToLast WITH !+~ "with"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Close, but this still doesn't cover all cases:

[<Test>]
let ``nested try/with with comment on with`` () =
    formatSourceString
        false
        """
try
    a
with
| b ->
    try c
    // inner comment
    with
    | d -> ()
"""
        config
    |> prepend newline
    |> should
        equal
        """
try
    a
with
| b ->
    try
        c
    // inner comment
    with
    | d -> ()
"""

This will still fail, unfortunately. (online tool)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code and test cases. Please take a look when you find some free time 🙏

+> indentOnWith
+> sepNln
+> col sepNln cs (genClause astContext true)
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ let (|LongIdentSet|_|) =

let (|TryWith|_|) =
function
| SynExpr.TryWith (e, _, cs, _, _, _, _) -> Some(e, cs)
| SynExpr.TryWith (e, _, cs, mWithToLast, _, _, _) -> Some(e, mWithToLast, cs)
| _ -> None

let (|TryFinally|_|) =
Expand Down