Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always make list with ifThenElse with YieldReturn multiline #981

Merged
merged 3 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Fantomas.Tests/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,3 +1567,37 @@ type T = { A: (unit -> unit) array }
module F =
let f (a: T) = a.A.[0]()
"""

[<Test>]
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" ]
"""

[<Test>]
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" ]
]
"""
3 changes: 2 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,4 +1450,12 @@ let (|ElmishReactWithChildren|_|) e =
Some(ident, attributes, (isArray, []))

| _ ->
None
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