From 7178d359c94d2dff8ca6ef4912d0883984bbe273 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 14 Aug 2020 16:24:32 +0200 Subject: [PATCH] Print trivia before and after with keyword. Fixes #976. --- src/Fantomas.Tests/PatternMatchingTests.fs | 73 ++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 19 +++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index 7b5079f4fb..1c3b64ad27 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -492,4 +492,77 @@ let f x = | C -> None | _ -> None +""" + +[] +let ``very long match clause with many lambdas`` () = + formatSourceString false """ +let MethInfoIsUnseen g m ty minfo = + let isUnseenByObsoleteAttrib () = + match BindMethInfoAttributes m minfo + (fun ilAttribs -> Some foo) + (fun fsAttribs -> Some bar) + (fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m)) + (fun _provAttribs -> None) + with + | Some res -> res + | None -> false + + () +""" config + |> prepend newline + |> should equal """ +let MethInfoIsUnseen g m ty minfo = + let isUnseenByObsoleteAttrib () = + match BindMethInfoAttributes + m + minfo + (fun ilAttribs -> Some foo) + (fun fsAttribs -> Some bar) + (fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m)) + (fun _provAttribs -> None) with + | Some res -> res + | None -> false + + () +""" + +[] +let ``very long match clause with many lambdas mixed with defines, 976`` () = + formatSourceString false """ +let MethInfoIsUnseen g m ty minfo = + let isUnseenByObsoleteAttrib () = + match BindMethInfoAttributes m minfo + (fun ilAttribs -> Some foo) + (fun fsAttribs -> Some bar) +#if !NO_EXTENSIONTYPING + (fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m)) +#else + (fun _provAttribs -> None) +#endif + with + | Some res -> res + | None -> false + + () +""" config + |> prepend newline + |> should equal """ +let MethInfoIsUnseen g m ty minfo = + let isUnseenByObsoleteAttrib () = + match BindMethInfoAttributes + m + minfo + (fun ilAttribs -> Some foo) + (fun fsAttribs -> Some bar) +#if !NO_EXTENSIONTYPING + (fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m)) +#else + (fun _provAttribs -> None) +#endif + with + | Some res -> res + | None -> false + + () """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 0c3775a1b8..7308753daa 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1282,7 +1282,15 @@ and genExpr astContext synExpr = +> autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)) | MatchLambda(sp, _) -> !- "function " +> colPre sepNln sepNln sp (genClause astContext true) | Match(e, cs) -> - atCurrentColumn (!- "match " +> genExpr astContext e -- " with" +> colPre sepNln sepNln cs (genClause astContext true)) + atCurrentColumn + (!- "match " + +> genExpr astContext e + +> enterNodeTokenByName synExpr.Range "WITH" + // indent 'with' further if trivia was printed so that is appear after the match keyword. + +> ifElseCtx lastWriteEventIsNewline (rep 5 !- " ") sepNone + -- " with" + +> leaveNodeTokenByName synExpr.Range "WITH" + +> colPre sepNln sepNln cs (genClause astContext true)) | MatchBang(e, cs) -> atCurrentColumn (!- "match! " +> genExpr astContext e -- " with" +> colPre sepNln sepNln cs (genClause astContext true)) | TraitCall(tps, msg, e) -> @@ -1503,6 +1511,13 @@ and genExpr astContext synExpr = +> genExpr astContext e +> unindent)))) + let hasThreeOrMoreLamdbas = + List.filter (function + | Paren (Lambda (_)) -> true + | _ -> false) es + |> List.length + |> fun l -> l >= 3 + if List.exists (function | Lambda _ | MatchLambda _ @@ -1510,7 +1525,7 @@ and genExpr astContext synExpr = | Paren (MatchLambda (_)) | MultilineString _ | CompExpr _ -> true - | _ -> false) es then + | _ -> false) es && not hasThreeOrMoreLamdbas then shortExpression else expressionFitsOnRestOfLine shortExpression longExpression