Skip to content

Commit

Permalink
Print trivia for SynExpr.ComputationExpr in named computation express…
Browse files Browse the repository at this point in the history
…ion. (#2467)
  • Loading branch information
nojaf committed Aug 31, 2022
1 parent d962305 commit 0981dfe
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
* --check should fail if the number of newlines is different. [#2461](https://github.com/fsprojects/fantomas/issues/2461)
* Comment after closing brace of computation expression is lost. [#2466](https://github.com/fsprojects/fantomas/issues/2466)

## [5.0.0-beta-008] - 2022-08-30

Expand All @@ -14,7 +15,7 @@
* Idempotency problem when using a list with interpolated strings. [#2242](https://github.com/fsprojects/fantomas/issues/2242)
* Idempotency problem when using set and spread syntax. [#2392](https://github.com/fsprojects/fantomas/issues/2392)
* Formatting (*) returns ( * ). [#2452](https://github.com/fsprojects/fantomas/issues/2452)
* Comment after last case of function with operator is removed.[#2454](https://github.com/fsprojects/fantomas/issues/2454)
* Comment after last case of function with operator is removed. [#2454](https://github.com/fsprojects/fantomas/issues/2454)
* Trailing whitespace in block comments should be preserved. [#2450](https://github.com/fsprojects/fantomas/issues/2450)

## [5.0.0-beta-007] - 2022-08-19
Expand Down
54 changes: 54 additions & 0 deletions src/Fantomas.Core.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,3 +2467,57 @@ odata<Person> {
skip 10
}
"""

[<Test>]
let ``trivia after computation expression, 2466`` () =
formatSourceString
false
"""
let errs =
(*[omit:(copying of errors omitted)]*)
seq {
for e in res.Errors ->
{ StartColumn = e.StartColumn
StartLine = e.StartLine
Message = e.Message
IsError = e.Severity = Error
EndColumn = e.EndColumn
EndLine = e.EndLine }
} (*[/omit]*)
"""
config
|> prepend newline
|> should
equal
"""
let errs =
(*[omit:(copying of errors omitted)]*)
seq {
for e in res.Errors ->
{ StartColumn = e.StartColumn
StartLine = e.StartLine
Message = e.Message
IsError = e.Severity = Error
EndColumn = e.EndColumn
EndLine = e.EndLine }
} (*[/omit]*)
"""

[<Test>]
let ``trivia after short computation expression`` () =
formatSourceString
false
"""
let zero =
async { () } // foo
|> ignore
"""
config
|> prepend newline
|> should
equal
"""
let zero =
async { () } // foo
|> ignore
"""
24 changes: 13 additions & 11 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,25 +1147,27 @@ and genExpr astContext synExpr ctx =
(!- " do" +> indent +> sepNln +> genExpr astContext e2 +> unindent)
)

| NamedComputationExpr (nameExpr, openingBrace, bodyExpr, closingBrace) ->
| NamedComputationExpr (nameExpr, openingBrace, bodyExpr, closingBrace, computationExprRange) ->
fun ctx ->
let short =
genExpr astContext nameExpr
+> sepSpace
+> genTriviaFor SynExpr_ComputationExpr_OpeningBrace openingBrace sepOpenS
+> genExpr astContext bodyExpr
+> genTriviaFor SynExpr_ComputationExpr_ClosingBrace closingBrace sepCloseS
+> (genTriviaFor SynExpr_ComputationExpr_OpeningBrace openingBrace sepOpenS
+> genExpr astContext bodyExpr
+> genTriviaFor SynExpr_ComputationExpr_ClosingBrace closingBrace sepCloseS
|> genTriviaFor SynExpr_ComputationExpr computationExprRange)

let long =
genExpr astContext nameExpr
+> sepSpace
+> genTriviaFor SynExpr_ComputationExpr_OpeningBrace openingBrace sepOpenS
+> indent
+> sepNln
+> genExpr astContext bodyExpr
+> unindent
+> sepNln
+> genTriviaFor SynExpr_ComputationExpr_ClosingBrace closingBrace sepCloseSFixed
+> (genTriviaFor SynExpr_ComputationExpr_OpeningBrace openingBrace sepOpenS
+> indent
+> sepNln
+> genExpr astContext bodyExpr
+> unindent
+> sepNln
+> genTriviaFor SynExpr_ComputationExpr_ClosingBrace closingBrace sepCloseSFixed
|> genTriviaFor SynExpr_ComputationExpr computationExprRange)

expressionFitsOnRestOfLine short long ctx
| ComputationExpr (openingBrace, e, closingBrace) ->
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Core/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ let (|NamedComputationExpr|_|) =
function
| SynExpr.App (ExprAtomicFlag.NonAtomic,
false,
((SynExpr.App _ | SynExpr.TypeApp _) as nameExpr | SimpleExpr nameExpr),
ComputationExpr (openingBrace, compExpr, closingBrace),
_) -> Some(nameExpr, openingBrace, compExpr, closingBrace)
(SynExpr.App _ | SynExpr.TypeApp _ as nameExpr | SimpleExpr nameExpr),
(ComputationExpr (openingBrace, bodyExpr, closingBrace) as compExpr),
_) -> Some(nameExpr, openingBrace, bodyExpr, closingBrace, compExpr.Range)
| _ -> None

/// Combines all ArrayOrList patterns
Expand Down

0 comments on commit 0981dfe

Please sign in to comment.