Skip to content

Commit

Permalink
Process nested dotget app paren in multiline if expression. Fixes fsp…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 21, 2021
1 parent aaf2b00 commit 6e2120c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 52 deletions.
28 changes: 28 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2417,3 +2417,31 @@ if List.exists
else
expressionFitsOnRestOfLine shortExpression longExpression ctx
"""

[<Test>]
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"
"""
98 changes: 46 additions & 52 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2145,76 +2145,70 @@ 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) ->
sepOpenTFor lpr
+> 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
Expand Down

0 comments on commit 6e2120c

Please sign in to comment.