From 6e2120c2815e09fe571b7fedef90fac08dddbf14 Mon Sep 17 00:00:00 2001 From: nojaf Date: Thu, 20 May 2021 20:30:31 +0200 Subject: [PATCH] Process nested dotget app paren in multiline if expression. Fixes #1712. --- src/Fantomas.Tests/IfThenElseTests.fs | 28 ++++++++ src/Fantomas/CodePrinter.fs | 98 +++++++++++++-------------- 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/src/Fantomas.Tests/IfThenElseTests.fs b/src/Fantomas.Tests/IfThenElseTests.fs index 66f3c779bb..499c671cba 100644 --- a/src/Fantomas.Tests/IfThenElseTests.fs +++ b/src/Fantomas.Tests/IfThenElseTests.fs @@ -2417,3 +2417,31 @@ if List.exists else expressionFitsOnRestOfLine shortExpression longExpression ctx """ + +[] +let ``multiline dotget chain in if expression, 1712`` () = + formatSourceString + false + """ +module Foo = + let bar = + if Regex("long long long long long long long long long").Match(s).Success then + None + else Some "hi" +""" + config + |> prepend newline + |> should + equal + """ +module Foo = + let bar = + if + Regex("long long long long long long long long long") + .Match(s) + .Success + then + None + else + Some "hi" +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 6465b5a9cd..4edaa184cf 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -2145,6 +2145,9 @@ and genExpr astContext synExpr ctx = | _ -> false) (Map.tryFindOrEmptyList (synExprToFsAstType e |> fst) ctx.TriviaMainNodes) + let indentNlnUnindentNln f = + indent +> sepNln +> f +> unindent +> sepNln + match e with | App (SynExpr.DotGet _, [ (Paren _) ]) -> atCurrentColumn (genExpr astContext e) | Paren (lpr, (AppSingleParenArg _ as ate), rpr, pr) -> @@ -2152,69 +2155,60 @@ and genExpr astContext synExpr ctx = +> atCurrentColumnIndent (genExpr astContext ate) +> sepCloseTFor rpr pr | AppParenArg app -> - indent - +> sepNln - +> genAlternativeAppWithParenthesis app astContext - +> unindent - +> sepNln + genAlternativeAppWithParenthesis app astContext + |> indentNlnUnindentNln | InfixApp (s, e, AppParenArg app, e2) -> - indent - +> sepNln - +> genAlternativeAppWithParenthesis app astContext - +> ifElse (noBreakInfixOps.Contains(s)) sepSpace sepNln - +> genInfixOperator s e - +> sepSpace - +> genExpr astContext e2 - +> unindent - +> sepNln + (genAlternativeAppWithParenthesis app astContext + +> ifElse (noBreakInfixOps.Contains(s)) sepSpace sepNln + +> genInfixOperator s e + +> sepSpace + +> genExpr astContext e2) + |> indentNlnUnindentNln | InfixApp (s, e, e1, AppParenArg app) -> - indent - +> sepNln - +> genExpr astContext e1 - +> sepNln - +> genInfixOperator s e - +> sepSpace - +> genAlternativeAppWithParenthesis app astContext - +> unindent - +> sepNln + (genExpr astContext e1 + +> sepNln + +> genInfixOperator s e + +> sepSpace + +> genAlternativeAppWithParenthesis app astContext) + |> indentNlnUnindentNln // very specific fix for 1380 | SameInfixApps (Paren (lpr, AppParenArg e, rpr, pr), es) -> - indent - +> sepNln - +> sepOpenTFor lpr - +> genAlternativeAppWithParenthesis e astContext - +> sepCloseTFor rpr pr - +> sepNln - +> col - sepNln - es - (fun (opText, opExpr, e) -> - genInfixOperator opText opExpr - +> sepSpace - +> (match e with - | Paren (lpr, AppParenArg app, rpr, pr) -> - sepOpenTFor lpr - +> genAlternativeAppWithParenthesis app astContext - +> sepCloseTFor rpr pr - | _ -> genExpr astContext e)) - +> unindent - +> sepNln + (sepOpenTFor lpr + +> genAlternativeAppWithParenthesis e astContext + +> sepCloseTFor rpr pr + +> sepNln + +> col + sepNln + es + (fun (opText, opExpr, e) -> + genInfixOperator opText opExpr + +> sepSpace + +> (match e with + | Paren (lpr, AppParenArg app, rpr, pr) -> + sepOpenTFor lpr + +> genAlternativeAppWithParenthesis app astContext + +> sepCloseTFor rpr pr + | _ -> genExpr astContext e))) + |> indentNlnUnindentNln | SynExpr.Match _ | SynExpr.MatchBang _ | SynExpr.TryWith _ - | SynExpr.TryFinally _ -> - indent + | SynExpr.TryFinally _ -> genExpr astContext e |> indentNlnUnindentNln + | DotGetAppParen (DotGetAppParen (e1, px1, lids1), px2, lids2) -> + genExpr astContext e1 + +> genExpr astContext px1 + +> indent +> sepNln - +> genExpr astContext e - +> unindent + +> genLidsWithDotsAndNewlines lids1 + +> genExpr astContext px2 +> sepNln + +> genLidsWithDotsAndNewlines lids2 + +> unindent + |> genTriviaFor SynExpr_DotGet e.Range + |> indentNlnUnindentNln | _ -> if hasCommentBeforeExpr () then - indent - +> sepNln - +> genExpr astContext e - +> unindent - +> sepNln + genExpr astContext e |> indentNlnUnindentNln else sepNlnWhenWriteBeforeNewlineNotEmpty sepNone +> genExpr astContext e