Skip to content

Commit

Permalink
Don't add extra newline when no else expression is present. Fixes fsp…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Nov 2, 2020
1 parent 496c72c commit 8f82f2c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
69 changes: 69 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,72 @@ let foo result total =
then total // and another one
else result
"""

[<Test>]
let ``if/then/elif without else, 1211`` () =
formatSourceString false """
let a =
// check if the current # char is part of an define expression
// if so add to defines
let captureHashDefine idx =
if trimmed.StartsWith("#if")
then defines.Add(processLine "#if" trimmed lineNumber offset)
elif trimmed.StartsWith("#elseif")
then defines.Add(processLine "#elseif" trimmed lineNumber offset)
elif trimmed.StartsWith("#else")
then defines.Add(processLine "#else" trimmed lineNumber offset)
elif trimmed.StartsWith("#endif")
then defines.Add(processLine "#endif" trimmed lineNumber offset)
for idx in [ 0 .. lastIndex ] do
let zero = sourceCode.[idx]
let plusOne = sourceCode.[idx + 1]
let plusTwo = sourceCode.[idx + 2]
()
""" config
|> prepend newline
|> should equal """
let a =
// check if the current # char is part of an define expression
// if so add to defines
let captureHashDefine idx =
if trimmed.StartsWith("#if")
then defines.Add(processLine "#if" trimmed lineNumber offset)
elif trimmed.StartsWith("#elseif")
then defines.Add(processLine "#elseif" trimmed lineNumber offset)
elif trimmed.StartsWith("#else")
then defines.Add(processLine "#else" trimmed lineNumber offset)
elif trimmed.StartsWith("#endif")
then defines.Add(processLine "#endif" trimmed lineNumber offset)
for idx in [ 0 .. lastIndex ] do
let zero = sourceCode.[idx]
let plusOne = sourceCode.[idx + 1]
let plusTwo = sourceCode.[idx + 2]
()
"""

[<Test>]
let ``multiline if/then/elif without else, 1187`` () =
formatSourceString false """
let fn () =
if true then
DoSomething ()
DoSomethingElse()
elif shouldWe then
Whatever()
let nextfunc () =
printf "Hi"
""" config
|> prepend newline
|> should equal """
let fn () =
if true then
DoSomething()
DoSomethingElse()
elif shouldWe then
Whatever()
let nextfunc () = printf "Hi"
"""
12 changes: 8 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2428,8 +2428,11 @@ and genExpr astContext synExpr ctx =
+> genThen synExpr.Range
+> genExpr astContext e2
+> sepNln
+> colPost sepNln sepNln elfis genElifTwoLiner
+> opt id enOpt (fun e4 -> genElse synExpr.Range +> genExpr astContext e4)
+> col sepNln elfis genElifTwoLiner
+> opt id enOpt (fun e4 ->
onlyIf (List.isNotEmpty elfis) sepNln
+> genElse synExpr.Range
+> genExpr astContext e4)

elif hasElfis
&& not isAnyExpressionIsMultiline
Expand Down Expand Up @@ -2518,14 +2521,15 @@ and genExpr astContext synExpr ctx =
+> genExpr astContext e2
+> unindent
+> sepNln
+> colPost sepNln sepNln elfis genElifMultiLine
+> col sepNln elfis genElifMultiLine
+> opt id enOpt (fun e4 ->
let correctedElseRange =
match List.tryLast elfis with
| Some (_, te, _) -> mkRange "correctedElseRange" te.Range.End synExpr.Range.End
| None -> synExpr.Range

genElse correctedElseRange
onlyIf (List.isNotEmpty elfis) sepNln
+> genElse correctedElseRange
+> indent
+> sepNln
+> genExpr astContext e4
Expand Down

0 comments on commit 8f82f2c

Please sign in to comment.