Skip to content

Commit

Permalink
Fix idempotency problem with multiline quotation expressions. (#2211)
Browse files Browse the repository at this point in the history
* Fix idempotency problem with multiline quotation expressions, 2203.
Extend Quote handling with expressionFitsOnRestOfLine.

* - Adjust formatting of quoted expressions to latest style guide
- Adjust tests
  • Loading branch information
dawedawe committed May 4, 2022
1 parent cbaa31c commit 0e036ac
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [Unreleased]

### Changed
* Adjusted formatting of quotation expressions to latest F# style guide. [#661](https://github.com/fsharp/fslang-design/issues/661)

### Fixed
* Idempotency problem with multiline quotation expressions. [#2203](https://github.com/fsprojects/fantomas/issues/2203)

## [4.7.9] - 2022-05-02

### Changed
Expand Down
6 changes: 4 additions & 2 deletions src/Fantomas.Tests/DotGetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,8 @@ let ``dotget inside a quotation, 2154`` () =
equal
"""
(fun (Singleton arg) ->
<@@ ((%%arg: Indicators) :> IIndicators)
.AsyncGetIndicator(indicatorIdVal) @@>)
<@@
((%%arg: Indicators) :> IIndicators)
.AsyncGetIndicator(indicatorIdVal)
@@>)
"""
41 changes: 39 additions & 2 deletions src/Fantomas.Tests/QuotationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ let ``typed quotations`` () =
|> should
equal
"""
<@ let f x = x + 10
f 20 @>
<@
let f x = x + 10
f 20
@>
"""

[<Test>]
Expand All @@ -40,3 +42,38 @@ let logger =
.Returns(())
.Create()
"""

[<Test>]
let ``should format multiline quotation expressions idempotent, 2203`` () =
formatSourceString
false
"""
let action =
<@
let msg = %httpRequestMessageWithPayload
RuntimeHelpers.fillHeaders msg %heads
async {
let! response = (%this).HttpClient.SendAsync(msg) |> Async.AwaitTask
return response.EnsureSuccessStatusCode().Content
}
@>
"""
config
|> prepend newline
|> should
equal
"""
let action =
<@
let msg = %httpRequestMessageWithPayload
RuntimeHelpers.fillHeaders msg %heads
async {
let! response =
(%this).HttpClient.SendAsync(msg)
|> Async.AwaitTask
return response.EnsureSuccessStatusCode().Content
}
@>
"""
15 changes: 11 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,11 +1268,18 @@ and genExpr astContext synExpr ctx =
// Not sure about the role of e1
| Quote (_, e2, isRaw) ->
let e =
match e2 with
| DotGetApp _ -> atCurrentColumnIndent (genExpr astContext e2)
| _ -> genExpr astContext e2
expressionFitsOnRestOfLine
(genExpr astContext e2)
(indent
+> sepNln
+> genExpr astContext e2
+> unindent
+> sepNln)

ifElse isRaw (!- "<@@ " +> e -- " @@>") (!- "<@ " +> e -- " @>")
ifElse
isRaw
(!- "<@@" +> sepSpace +> e +> sepSpace +> !- "@@>")
(!- "<@" +> sepSpace +> e +> sepSpace +> !- "@>")
| TypedExpr (TypeTest, e, t) ->
genExpr astContext e -- " :? "
+> genType astContext false t
Expand Down

0 comments on commit 0e036ac

Please sign in to comment.