From 6089d2e6f73ee00592b35ac695869050b0fd9baf Mon Sep 17 00:00:00 2001 From: ijanus Date: Wed, 22 Dec 2021 17:19:31 +0100 Subject: [PATCH] Avoid vanity alignment in multiline match clause Fixes https://github.com/fsharp/fslang-design/issues/646 --- src/Fantomas.Tests/PatternMatchingTests.fs | 43 ++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 21 +++++++---- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index f0050737e2..baa202b70c 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -2087,3 +2087,46 @@ match foo with ) -> bar () | _ -> () """ + +[] +let ``vanity alignment removed from multiline match expression`` () = + formatSourceString + false + """ +match directoryRouter.GetIdentity() + |> self.ServerDescriptors.TryFind with +| None -> CircuitNodeDetail.FastCreate +| Some serverDescriptor -> + self.ConvertToCircuitNodeDetail serverDescriptor +""" + config + |> prepend newline + |> should + equal + """ +match + directoryRouter.GetIdentity() + |> self.ServerDescriptors.TryFind + with +| None -> CircuitNodeDetail.FastCreate +| Some serverDescriptor -> self.ConvertToCircuitNodeDetail serverDescriptor +""" + +[] +let ``match expression covering one line`` () = + formatSourceString + false + """ +match directoryRouter.GetIdentity() with +| None -> CircuitNodeDetail.FastCreate +| Some serverDescriptor -> + self.ConvertToCircuitNodeDetail serverDescriptor""" + config + |> prepend newline + |> should + equal + """ +match directoryRouter.GetIdentity() with +| None -> CircuitNodeDetail.FastCreate +| Some serverDescriptor -> self.ConvertToCircuitNodeDetail serverDescriptor +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index ab32ea3bd0..636a436eda 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1635,7 +1635,7 @@ and genExpr astContext synExpr ctx = +> expressionFitsOnRestOfLine (genExpr astContext e +> genWithAfterMatch withRange) - (genExprInIfOrMatch astContext e + (genExprInIfOrMatch astContext e true +> (sepNlnUnlessLastEventIsNewline +> (genWithAfterMatch withRange))) @@ -1649,7 +1649,7 @@ and genExpr astContext synExpr ctx = +> expressionFitsOnRestOfLine (genExpr astContext e +> genWithAfterMatch withRange) - (genExprInIfOrMatch astContext e + (genExprInIfOrMatch astContext e true +> (sepNlnUnlessLastEventIsNewline +> (genWithAfterMatch withRange))) @@ -2191,7 +2191,9 @@ and genExpr astContext synExpr ctx = let genElifMultiLine (elf1: SynExpr, elf2, elifKeywordRange, thenKeywordRange) = (TriviaContext.``else if / elif`` elifKeywordRange) - +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (genExprInIfOrMatch astContext (cleanIfExpr elf1)) + +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty ( + genExprInIfOrMatch astContext (cleanIfExpr elf1) false + ) +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace +> genThen thenKeywordRange +> indent @@ -2226,7 +2228,7 @@ and genExpr astContext synExpr ctx = // x // bool expr x should be indented +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty ( - genExprInIfOrMatch astContext (cleanIfExpr e1) + genExprInIfOrMatch astContext (cleanIfExpr e1) false +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace ) +> genThen synExpr.Range @@ -3321,7 +3323,7 @@ and genAppWithSingleParenthesisArgument (e, lpr, a, rpr, pr) astContext = +> (genExpr astContext a) +> tokN (Option.defaultValue pr rpr) RPAREN sepCloseT -and genExprInIfOrMatch astContext (e: SynExpr) (ctx: Context) : Context = +and genExprInIfOrMatch astContext (e: SynExpr) (shouldBeSplitToNextLine: bool) (ctx: Context) : Context = let short = sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace +> genExpr astContext e @@ -3341,6 +3343,11 @@ and genExprInIfOrMatch astContext (e: SynExpr) (ctx: Context) : Context = let fallback = if hasCommentBeforeExpr e then genExpr astContext e |> indentNlnUnindentNln + elif shouldBeSplitToNextLine then + indent + +> sepNln + +> genExpr astContext e + +> unindent else sepNlnWhenWriteBeforeNewlineNotEmpty sepNone +> genExpr astContext e @@ -5572,7 +5579,7 @@ and genKeepIndentMatch let lastClauseIndex = clauses.Length - 1 ifElse (triviaType = SynExpr_MatchBang) !- "match! " !- "match " - +> genExprInIfOrMatch astContext e + +> genExprInIfOrMatch astContext e false +> (fun ctx -> genWithAfterMatch (withRange ctx) ctx) +> sepNln +> coli @@ -5617,7 +5624,7 @@ and genKeepIdentIf let long = ifElse (idx = 0) (!- "if ") (!- "elif ") - +> genExprInIfOrMatch astContext ifExpr + +> genExprInIfOrMatch astContext ifExpr false +> sepSpace +> !- "then"