From e83be9bc0e73dbe5682d6183284ba987b8295312 Mon Sep 17 00:00:00 2001 From: Retheesh Erathu Date: Tue, 26 Oct 2021 14:46:38 +0200 Subject: [PATCH] Avoid vanity alignment in long pattern match Fixes https://github.com/fsprojects/fantomas/issues/1926 Co-authored-by: ijanus --- src/Fantomas.Tests/PatternMatchingTests.fs | 53 +++++++++++++++++++--- src/Fantomas/CodePrinter.fs | 26 +++++++++-- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index d32e73296c..f88749e3f5 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -38,9 +38,13 @@ module Foo = return failwith "unreachable" else match baz with - | Some (AverageBetweenResponses (minimumNumberOfResponses, - averageFunc)) -> - return failwith "unreachable" + | Some + ( + AverageBetweenResponses + ( + minimumNumberOfResponses, averageFunc + ) + ) -> return failwith "unreachable" | _ -> return failwith "unreachable" } """ @@ -1217,8 +1221,10 @@ type Thing = | Foo of msg: string override this.ToString() = match this with - | Foo (ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) -> - "" + | Foo + ( + ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + ) -> "" """ [] @@ -1248,8 +1254,10 @@ type Thing = | Foo of msg : string override this.ToString() : string = match this with - | Foo (ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) -> - "" + | Foo + ( + ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + ) -> "" """ [] @@ -2165,6 +2173,37 @@ match! a with // foo | B b -> () """ +[] +let ``vanity alignment used when using long case in match block, 1926`` () = + formatSourceString + false + """ +match foo with +| SomeVeryLongMatchCase(1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890) -> + bar() +| _ -> () """ + config + |> prepend newline + |> should + equal + """ +match foo with +| SomeVeryLongMatchCase + ( + 1234567890, + 1234567890, + 1234567890, + 1234567890, + 1234567890, + 1234567890, + 1234567890, + 1234567890, + 1234567890, + 1234567890 + ) -> bar () +| _ -> () +""" + [] let ``comment after match keyword, 1851`` () = formatSourceString diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 1b233d1277..62d9d8d12d 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -5028,9 +5028,29 @@ and genPat astContext pat = | PatParen (_, PatUnitConst, _) -> !- "()" | PatParen (lpr, p, rpr) -> - genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT - +> genPat astContext p - +> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT + let shortExpr = + genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT + +> genPat astContext p + +> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT + + let longExpr = + ifElse + astContext.IsInsideMatchClausePattern + (indent + +> sepNln + +> indent + +> genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT + +> sepNln + +> genPat astContext p + +> unindent + +> sepNln + +> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT + +> unindent) + (genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT + +> genPat astContext p + +> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT) + + expressionFitsOnRestOfLine shortExpr longExpr | PatTuple ps -> expressionFitsOnRestOfLine (col sepComma ps (genPat astContext))