Skip to content

Commit

Permalink
Indent multiline if condition for KeepIndentInBranch. Fixes #1729. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored May 18, 2021
1 parent 76ffe36 commit 8507701
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/Fantomas.Tests/KeepIndentInBranchTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,3 +1493,50 @@ module Foo =
0
"""

[<Test>]
let ``multiline if condition is indented, 1729`` () =
formatSourceString
false
"""
let x y =
if not (
result.HasResultsFor(
[ "label"
"ipv4"
"macAddress"
"medium"
"manufacturer" ]
)
) then
None
else
let label = string result.["label"]
let ipv4 = string result.["ipv4"]
None
"""
config
|> prepend newline
|> should
equal
"""
let x y =
if
not (
result.HasResultsFor(
[ "label"
"ipv4"
"macAddress"
"medium"
"manufacturer" ]
)
)
then
None
else
let label = string result.["label"]
let ipv4 = string result.["ipv4"]
None
"""
21 changes: 18 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5401,9 +5401,24 @@ and genKeepIdentIf
sepNln
branches
(fun idx (ifExpr, thenExpr, _r, _fullRange, _node) ->
ifElse (idx = 0) (!- "if ") (!- "elif ")
+> genExpr astContext ifExpr
+> !- " then"
let genIf =
let short =
ifElse (idx = 0) (!- "if ") (!- "elif ")
+> genExpr astContext ifExpr
+> !- " then"

let long =
ifElse (idx = 0) (!- "if ") (!- "elif ")
+> indent
+> sepNln
+> genExpr astContext ifExpr
+> unindent
+> sepNln
+> !- "then"

expressionFitsOnRestOfLine short long

genIf
+> indent
+> sepNln
+> genExpr astContext thenExpr
Expand Down

0 comments on commit 8507701

Please sign in to comment.