Skip to content

Commit

Permalink
Add parenthesis around multi line if/then/else inside tuple. Fixes fs…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Dec 23, 2020
1 parent cce39ed commit 97c3b31
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/Fantomas.Tests/TupleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ let y =
equal
"""
let y =
if String.IsNullOrWhiteSpace(args) then
""
elif args.StartsWith("(") then
args
elif v.CurriedParameterGroups.Count > 1
&& (not verboseMode) then
" " + args
else
sprintf "(%s)" args
, namesWithIndices
(if String.IsNullOrWhiteSpace(args) then
""
elif args.StartsWith("(") then
args
elif v.CurriedParameterGroups.Count > 1
&& (not verboseMode) then
" " + args
else
sprintf "(%s)" args),
namesWithIndices
"""
18 changes: 12 additions & 6 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,24 +1151,31 @@ and genTuple astContext es =
col sepComma es (genShortExpr astContext)

let longExpression =
let shouldAddCommaUpFront =
let containsLambdaOrMatchExpr =
es
|> List.pairwise
|> List.exists
(function
| SynExpr.Match _, _
| SynExpr.Lambda _, _
| SynExpr.IfThenElse _, _
| InfixApp (_, _, _, SynExpr.Lambda _), _ -> true
| _ -> false)

let sep =
if shouldAddCommaUpFront then
if containsLambdaOrMatchExpr then
(sepNln +> sepComma)
else
(sepComma +> sepNln)

col sep es (genExpr astContext)
let lastIndex = List.length es - 1

let genExpr astContext idx e =
match e with
| SynExpr.IfThenElse _ when (idx < lastIndex) ->
autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e)
| _ -> genExpr astContext e

coli sep es (genExpr astContext)

atCurrentColumn (expressionFitsOnRestOfLine shortExpression longExpression)

Expand Down Expand Up @@ -1877,8 +1884,7 @@ and genExpr astContext synExpr ctx =

let multilineExpr =
(match e with
| SynExpr.IfThenElse _ ->
expressionFitsOnRestOfLine (genExpr astContext e) (sepOpenT +> genExpr astContext e +> sepCloseT)
| SynExpr.IfThenElse _ -> autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e)
| _ -> genExpr astContext e)
+> sepNln
+> col
Expand Down
3 changes: 3 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,9 @@ let internal autoNlnIfExpressionExceedsPageWidth expr (ctx: Context) =
expr
ctx

let internal autoParenthesisIfExpressionExceedsPageWidth expr (ctx: Context) =
expressionFitsOnRestOfLine expr (sepOpenT +> expr +> sepCloseT) ctx

let internal futureNlnCheckMem (f, ctx) =
if ctx.WriterModel.IsDummy || not ctx.BreakLines then
(false, false)
Expand Down

0 comments on commit 97c3b31

Please sign in to comment.