diff --git a/src/Fantomas.Tests/ListTests.fs b/src/Fantomas.Tests/ListTests.fs index 2e9cf65e02..2081998b91 100644 --- a/src/Fantomas.Tests/ListTests.fs +++ b/src/Fantomas.Tests/ListTests.fs @@ -1567,3 +1567,37 @@ type T = { A: (unit -> unit) array } module F = let f (a: T) = a.A.[0]() """ + +[] +let ``list with if/then/else yield expressions should always be multiline, 931`` () = + formatSourceString false """ +let original_input = [ + if true then yield "value1" + if false then yield "value2" + if true then yield "value3" +] +""" { config with MaxIfThenElseShortWidth = 120; MaxArrayOrListWidth = 120 } + |> prepend newline + |> should equal """ +let original_input = + [ if true then yield "value1" + if false then yield "value2" + if true then yield "value3" ] +""" + +[] +let ``list with if/then/else yield bang expressions should always be multiline`` () = + formatSourceString false """ +let value = [ + if foo then yield! ["a";"b"] else yield "c" + if bar then yield "d" else yield! ["e";"f"] +] +""" { config with MaxIfThenElseShortWidth = 120; MaxArrayOrListWidth = 120; MultilineBlockBracketsOnSameColumn = true } + |> prepend newline + |> should equal """ +let value = + [ + if foo then yield! [ "a"; "b" ] else yield "c" + if bar then yield "d" else yield! [ "e"; "f" ] + ] +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 7574c93c64..3d32659f3d 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1093,7 +1093,8 @@ and genExpr astContext synExpr = fun ctx -> // If an array or list has any form of line comments inside them, they cannot fit on a single line // check for any comments inside the range of the node - if (TriviaHelpers.``has line comments inside`` alNode.Range ctx.Trivia) then + if (TriviaHelpers.``has line comments inside`` alNode.Range ctx.Trivia) + || List.exists isIfThenElseWithYieldReturn xs then multilineExpression ctx else isShortExpression ctx.Config.MaxArrayOrListWidth shortExpression multilineExpression ctx diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index 6d31d278dc..7f58cec64e 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -1450,4 +1450,12 @@ let (|ElmishReactWithChildren|_|) e = Some(ident, attributes, (isArray, [])) | _ -> - None \ No newline at end of file + None + +let isIfThenElseWithYieldReturn e = + match e with + | SynExpr.IfThenElse(_, SynExpr.YieldOrReturn _, None, _ ,_, _,_) + | SynExpr.IfThenElse(_, SynExpr.YieldOrReturn _, Some (SynExpr.YieldOrReturn _), _ ,_, _,_) + | SynExpr.IfThenElse(_, SynExpr.YieldOrReturnFrom _, None, _ ,_, _,_) + | SynExpr.IfThenElse(_, SynExpr.YieldOrReturn _, Some (SynExpr.YieldOrReturnFrom _), _ ,_, _,_) -> true + | _ -> false