diff --git a/src/Fantomas.Tests/KeepIndentInBranchTests.fs b/src/Fantomas.Tests/KeepIndentInBranchTests.fs index a9e2f2fac5..f407259cc3 100644 --- a/src/Fantomas.Tests/KeepIndentInBranchTests.fs +++ b/src/Fantomas.Tests/KeepIndentInBranchTests.fs @@ -1493,3 +1493,50 @@ module Foo = 0 """ + +[] +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 +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index ad02d6fa61..2beb447a22 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -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