Skip to content

Commit

Permalink
add support for writing the 'and!' expressions and patterns (#693)
Browse files Browse the repository at this point in the history
* add support for writing the 'and!' expressions and patterns

* add more tests
  • Loading branch information
baronfel authored Feb 19, 2020
1 parent 8fdf428 commit 3d2e39e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
56 changes: 55 additions & 1 deletion src/Fantomas.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,58 @@ let y = async {
|> should equal """
let x = { 3 .. 7 }
let y = async { return { 0 .. 1 } }
"""
"""

[<Test>]
let ``and! is supported`` () =
formatSourceString false """
async {
let! x = Async.Sleep 1.
and! y = Async.Sleep 2.
return 10
}
""" config
|> prepend newline
|> should equal """
async {
let! x = Async.Sleep 1.
and! y = Async.Sleep 2.
return 10 }
"""
[<Test>]
let ``multiple and! is supported`` () =
formatSourceString false """
// Reads the values of x, y and z concurrently, then applies f to them
parallel {
let! x = slowRequestX()
and! y = slowRequestY()
and! z = slowRequestZ()
return f x y z
}
""" config
|> prepend newline
|> should equal """
// Reads the values of x, y and z concurrently, then applies f to them
``parallel`` {
let! x = slowRequestX()
and! y = slowRequestY()
and! z = slowRequestZ()
return f x y z }
"""

[<Test>]
let ``and! sample number 3`` () =
formatSourceString false """
observable {
let! a = foo
and! b = bar
return a + b
}
""" config
|> prepend newline
|> should equal """
observable {
let! a = foo
and! b = bar
return a + b }
"""
8 changes: 7 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,9 +1439,15 @@ and genExpr astContext synExpr =
addParenIfAutoNln e1 (genExpr astContext) -- sprintf " <- " +> genExpr astContext e2

| LetOrUseBang(isUse, p, e1, ands, e2) ->

let genAndList astContext (ands: list<SequencePointInfoForBinding * bool * bool * SynPat * SynExpr * range>) =
colPost sepNln sepNln
ands
(fun (_,_,_,pat,expr,_) -> !- "and! " +> genPat astContext pat -- " = " +> genExpr astContext expr)

atCurrentColumn (ifElse isUse (!- "use! ") (!- "let! ")
+> genPat astContext p -- " = " +> genExpr astContext e1 +> sepNln
// TODO: use ands here
+> genAndList astContext ands
+> genExpr astContext e2
)

Expand Down

0 comments on commit 3d2e39e

Please sign in to comment.