Skip to content

Commit

Permalink
Use autoIndentNlnByFuture for let bang (#722)
Browse files Browse the repository at this point in the history
* Use autoIndentNlnByFuture for let bang. Fixes #615

* Apply the same behavior when formatting !and
  • Loading branch information
nojaf committed Mar 17, 2020
1 parent 25c9126 commit 7b73d9e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
77 changes: 77 additions & 0 deletions src/Fantomas.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,80 @@ observable {
and! b = bar
return a + b }
"""

[<Test>]
let ``let bang should be formatted as regular let, 615`` () =
formatSourceString false """
let f =
async {
// Without binding newline after assignment sign preserved, which is expected behavior
let r =
match 0 with
| _ -> ()
return r
}
let f2 =
async {
// When binding, newline force-removed, which makes the whole expression
// on the right side to be indented.
let! r = match 0 with
| _ -> () |> async.Return
return r
}
""" config
|> prepend newline
|> should equal """
let f =
async {
// Without binding newline after assignment sign preserved, which is expected behavior
let r =
match 0 with
| _ -> ()
return r
}
let f2 =
async {
// When binding, newline force-removed, which makes the whole expression
// on the right side to be indented.
let! r =
match 0 with
| _ -> () |> async.Return
return r
}
"""

[<Test>]
let ``let bang, and bang should be formatted as regular let`` () =
formatSourceString false """
let f2 =
async {
// When binding, newline force-removed, which makes the whole expression
// on the right side to be indented.
let! r = match 0 with
| _ -> () |> async.Return
and! s = match 0 with
| _ -> () |> async.Return
return r + s
}
""" config
|> prepend newline
|> should equal """
let f2 =
async {
// When binding, newline force-removed, which makes the whole expression
// on the right side to be indented.
let! r =
match 0 with
| _ -> () |> async.Return
and! s =
match 0 with
| _ -> () |> async.Return
return r + s
}
"""
13 changes: 6 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,17 +1431,16 @@ 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)
(fun (_,_,_,pat,expr,_) -> !- "and! " +> genPat astContext pat -- " = " +> autoIndentNlnByFuture (genExpr astContext expr))

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

| ParsingError r ->
raise <| FormatException (sprintf "Parsing error(s) between line %i column %i and line %i column %i"
Expand Down

0 comments on commit 7b73d9e

Please sign in to comment.