diff --git a/src/Fantomas.Tests/CommentTests.fs b/src/Fantomas.Tests/CommentTests.fs index d8d204080c..b8f83db4ae 100644 --- a/src/Fantomas.Tests/CommentTests.fs +++ b/src/Fantomas.Tests/CommentTests.fs @@ -1599,3 +1599,89 @@ type TorDirectory = } } """ + +[] +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 +| _ -> () +""" + +[] +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 +""" + +[] +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 -> () +""" diff --git a/src/Fantomas.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Tests/CompilerDirectivesTests.fs index f4348c85ae..a6e7566a61 100644 --- a/src/Fantomas.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Tests/CompilerDirectivesTests.fs @@ -2659,3 +2659,36 @@ let ``indented #if directive inside another non-indented #if directive should fo #endif #endif """ + +[] +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 +| _ -> () +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index a11012a516..49f9ab3bb2 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -2023,14 +2023,20 @@ 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" + +> (fun ctx -> + let lookupRange = + ctx.MkRangeWith + (mWithToLast.StartLine, mWithToLast.StartColumn) + (mWithToLast.StartLine, mWithToLast.StartColumn + 3) + + tokN lookupRange WITH !+~ "with" ctx) +> indentOnWith +> sepNln +> col sepNln cs (genClause astContext true) diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index 6fd51bee08..5684624e1b 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -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|_|) =