diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index e9bdeee0c0..66d6ac39d7 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -1754,3 +1754,30 @@ match x with match x with | :? (int) as i -> () """ + +[] +let ``don't add parenthesis if last clause is single line, 1698`` () = + formatSourceString + false + """ + let select px = + match px with + | Shared.Foo _ -> "foo" + | Shared.LongerFoobarFoo -> "lf" + | Shared.Barry -> "barry" + |> List.singleton + |> instr "ziggy" +""" + { config with IndentSize = 2 } + |> prepend newline + |> should + equal + """ +let select px = + match px with + | Shared.Foo _ -> "foo" + | Shared.LongerFoobarFoo -> "lf" + | Shared.Barry -> "barry" + |> List.singleton + |> instr "ziggy" +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index ce5981b970..a953b847d1 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1698,7 +1698,18 @@ and genExpr astContext synExpr ctx = (match e with | SynExpr.IfThenElse _ | SynExpr.Match _ when (ctx.Config.IndentSize <= operatorText.Length) -> - autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e) + (fun ctx -> + let ctxAfterMatch = genExpr astContext e ctx + + let barOnLastLine = + match List.tryHead ctxAfterMatch.WriterModel.Lines with + | Some line -> line.TrimStart().StartsWith("| ") + | None -> false + + if barOnLastLine then + ctxAfterMatch + else + autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e) ctx) | _ -> genExpr astContext e) +> sepNln +> col