From 0237e8fc240d6713e82cb615f5cc9a0c50937d1f Mon Sep 17 00:00:00 2001 From: nojaf Date: Wed, 5 May 2021 21:45:48 +0200 Subject: [PATCH] Don't add parenthesis if last clause is single line. Fixes #1698 --- src/Fantomas.Tests/PatternMatchingTests.fs | 27 ++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 13 ++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) 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 10421ce481..634b5be2d0 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