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

Add braces when dealing with a lazy keyword. #348

Merged
merged 3 commits into from
Oct 31, 2018
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
1 change: 1 addition & 0 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Compile Include="FormatAstTests.fs" />
<Compile Include="LetBindingTests.fs" />
<Compile Include="NoTrailingSpacesTests.fs" />
<Compile Include="LazyTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas\Fantomas.fsproj" />
Expand Down
34 changes: 34 additions & 0 deletions src/Fantomas.Tests/LazyTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Fantomas.Tests.LazyTests

open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelper

[<Test>]
let ``lazy should wrap with ()``() =
formatSourceString false """
let v = // <- Lazy "1"
lazy
1 |> string""" config
|> prepend newline
|> should equal """
let v = // <- Lazy "1"
lazy (1 |> string)
"""

[<Test>]
let ``lazy should not wrap with () for multiline``() =
formatSourceString false """
let v = // <- Lazy "1"
lazy
1
|> id
|> string""" config
|> prepend newline
|> should equal """
let v = // <- Lazy "1"
lazy
1
|> id
|> string
"""
8 changes: 3 additions & 5 deletions src/Fantomas/CodeFormatter.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,9 @@ let list1 = ["1"]
let list2 = ["2"]

"""
let list =
list1
@ list2
@ ["a"; "b"]
@ List.map id ["a"; "b"]
let v = // <- Lazy "1"
lazy
1 |> string
"""
|> formatSrc

Expand Down
7 changes: 7 additions & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,13 @@ and genTuple astContext es =
))

and genExpr astContext = function
| SingleExpr(Lazy, e) ->
// Always add braces when dealing with lazy
let addParens = hasParenthesis e || multiline e
str "lazy "
+> ifElse addParens id sepOpenT
+> breakNln astContext (multiline e) e
+> ifElse addParens id sepCloseT
| SingleExpr(kind, e) -> str kind +> genExpr astContext e
| ConstExpr(c) -> genConst c
| NullExpr -> !- "null"
Expand Down