From c0e56ec82a259229654bcd0112f108b05c82da17 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 --- src/Fantomas.Tests/PatternMatchingTests.fs | 31 ++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 23 +++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index ed05a9f262..f0050737e2 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -2056,3 +2056,34 @@ match! | None -> false | Some balance -> someRetrievedBalance = balance """ + +[] +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 () +| _ -> () +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 79e956f2b7..b9bcc1ae00 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -4971,10 +4971,25 @@ and genPat astContext pat = | _ -> true | _ -> true - ifElse isParenNecessary sepOpenT sepSpace - +> genPat astContext p - +> enterNodeTokenByName pat.Range RPAREN - +> ifElse isParenNecessary sepCloseT sepNone + let shortExpr = + ifElse isParenNecessary sepOpenT sepSpace + +> genPat astContext p + +> enterNodeTokenByName pat.Range RPAREN + +> ifElse isParenNecessary sepCloseT sepNone + + let longExpr = + ifElse + astContext.IsInsideMatchClausePattern + (ifElse isParenNecessary ((indent +> sepNln +> sepOpenT +> indent +> sepNln)) sepNone) + (ifElse isParenNecessary sepOpenT sepSpace) + +> genPat astContext p + +> enterNodeTokenByName pat.Range RPAREN + +> (ifElse + astContext.IsInsideMatchClausePattern + (ifElse isParenNecessary (unindent +> sepNln +> sepCloseT +> unindent) sepNone) + (ifElse isParenNecessary sepCloseT sepNone)) + + expressionFitsOnRestOfLine shortExpr longExpr | PatTuple ps -> expressionFitsOnRestOfLine (col sepComma ps (genPat astContext))