diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index 04738bc2da..2514a05f4a 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -2110,6 +2110,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 c01bf59614..a0252cb7c9 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1601,7 +1601,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))) ) @@ -1614,7 +1614,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))) ) @@ -2305,7 +2305,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 @@ -2340,7 +2340,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 @@ -3406,7 +3406,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 @@ -3426,6 +3426,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 @@ -5507,7 +5512,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 @@ -5567,7 +5572,7 @@ and genKeepIdentIf let long = genKeywordStart - +> genExprInIfOrMatch astContext ifExpr + +> genExprInIfOrMatch astContext ifExpr false +> sepSpace +> !- "then"