From 5a42da2585671c6b27ebeefe01e8fa32c4074e39 Mon Sep 17 00:00:00 2001 From: ijanus Date: Thu, 10 Mar 2022 11:39:19 +0800 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 | 19 ++++++---- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index 89f4923d5e..4ca3e674af 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -2152,6 +2152,49 @@ match! // foo | B b -> () """ +[] +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 +""" + [] let ``comment after with keyword in match bang`` () = formatSourceString diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index de8cddcea9..aa7648e6ea 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1605,7 +1605,7 @@ and genExpr astContext synExpr ctx = expressionFitsOnRestOfLine (genExpr astContext e +> genWithAfterMatch SynExpr_Match_With withRange) - (genExprInIfOrMatch astContext e + (genExprInIfOrMatch astContext e true +> (sepNlnUnlessLastEventIsNewline +> (genWithAfterMatch SynExpr_Match_With withRange))) ) @@ -1618,7 +1618,7 @@ and genExpr astContext synExpr ctx = expressionFitsOnRestOfLine (genExpr astContext e +> genWithAfterMatch SynExpr_MatchBang_With withRange) - (genExprInIfOrMatch astContext e + (genExprInIfOrMatch astContext e true +> (sepNlnUnlessLastEventIsNewline +> (genWithAfterMatch SynExpr_MatchBang_With withRange))) ) @@ -2309,7 +2309,7 @@ and genExpr astContext synExpr ctx = optSingle genElse elseKw +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace +> genIf ifKw isElif - +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (genExprInIfOrMatch astContext e1) + +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (genExprInIfOrMatch astContext e1 false) +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace +> genThen thenKw +> indent @@ -2344,7 +2344,7 @@ and genExpr astContext synExpr ctx = // x // bool expr x should be indented +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty ( - genExprInIfOrMatch astContext e1 + genExprInIfOrMatch astContext e1 false +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace ) +> genThen thenKw @@ -3415,7 +3415,7 @@ and genAppWithSingleParenthesisArgument (e, lpr, a, rpr, _pr) astContext = +> (genExpr astContext a) +> sepCloseTFor rpr -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 @@ -3435,6 +3435,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 @@ -5528,7 +5533,7 @@ and genKeepIndentMatch (genTriviaFor SynExpr_MatchBang_Match matchKeyword !- "match! ") (genTriviaFor SynExpr_Match_Match matchKeyword !- "match ") +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty ( - genExprInIfOrMatch astContext e + genExprInIfOrMatch astContext e false +> genWithAfterMatch (if isMatchBang then SynExpr_MatchBang_With @@ -5588,7 +5593,7 @@ and genKeepIdentIf let long = genKeywordStart - +> genExprInIfOrMatch astContext ifExpr + +> genExprInIfOrMatch astContext ifExpr false +> sepSpace +> !- "then"