Skip to content

Commit

Permalink
Add parenthesis around multi line if/then/else that is piped. Fixes f…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Dec 23, 2020
1 parent 1719253 commit cce39ed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,3 +1620,41 @@ elif cccccccccccccccccccccccc then
else
f
"""

[<Test>]
let ``multiline if/then/else piped to function, 1324`` () =
formatSourceString
false
"""
let tryDecompile (ty: FSharpEntity) = async {
match ty.TryFullName with
| Some fullName ->
return decompile ty.Assembly.SimpleName externalSym
| None ->
// might be abbreviated type (like string)
return!
if ty.IsFSharpAbbreviation then Some ty.AbbreviatedType else None
|> tryGetTypeDef
|> tryGetSource
}
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let tryDecompile (ty: FSharpEntity) =
async {
match ty.TryFullName with
| Some fullName -> return decompile ty.Assembly.SimpleName externalSym
| None ->
// might be abbreviated type (like string)
return!
(if ty.IsFSharpAbbreviation then
Some ty.AbbreviatedType
else
None)
|> tryGetTypeDef
|> tryGetSource
}
"""
7 changes: 5 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,10 @@ and genExpr astContext synExpr ctx =
+> genExpr astContext e)

let multilineExpr =
genExpr astContext e
(match e with
| SynExpr.IfThenElse _ ->
expressionFitsOnRestOfLine (genExpr astContext e) (sepOpenT +> genExpr astContext e +> sepCloseT)
| _ -> genExpr astContext e)
+> sepNln
+> col
sepNln
Expand Down Expand Up @@ -2527,7 +2530,7 @@ and genExpr astContext synExpr ctx =

isShortExpression ctx.Config.MaxIfThenElseShortWidth shortExpression longExpression

atCurrentColumn expr ctx
atCurrentColumnIndent expr ctx

// At this stage, all symbolic operators have been handled.
| OptVar (s, isOpt, ranges) ->
Expand Down

0 comments on commit cce39ed

Please sign in to comment.