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

Print trivia of SynMatchClause #1357

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
63 changes: 63 additions & 0 deletions src/Fantomas.Tests/TriviaTests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Fantomas.Tests.TriviaTests

open NUnit.Framework
open FsUnit
open Fantomas
open Fantomas.Tests.TestHelper
open Fantomas.TriviaTypes
Expand Down Expand Up @@ -489,3 +490,65 @@ let ``number expression`` () =
| [ { ContentItself = Some (Number (n))
Type = TriviaNodeType.MainNode (SynExpr_Const) } ] -> n == "2.0m"
| _ -> fail ()

[<Test>]
let ``line comment inside short `with` block (of a try-with), 1219`` () =
HumbertoCortes marked this conversation as resolved.
Show resolved Hide resolved
formatSourceString
false
"""
try
//humberto
TrySomething(someParam)
with
//comentario
ex ->
MakeSureToCleanup(someParam)
"""
config
|> prepend newline
|> should
equal
"""
try
//humberto
TrySomething(someParam)
with
//comentario
ex -> MakeSureToCleanup(someParam)
"""

[<Test>]
let ``line comment inside `with` block (of a try-with), 1219`` () =
formatSourceString
false
"""module Foo =
let Bar () =
async {
try
let! content = tryDownloadFile url
return Some content
with
// should we specify HttpRequestException?
ex ->
Infrastructure.ReportWarning ex
return None
}
"""
config
|> prepend newline
|> should
equal
"""
module Foo =
let Bar () =
async {
try
let! content = tryDownloadFile url
return Some content
with
// should we specify HttpRequestException?
ex ->
Infrastructure.ReportWarning ex
return None
}
"""
1 change: 1 addition & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4103,6 +4103,7 @@ and genClause astContext hasBar (Clause (p, e, eo)) =
+> leaveNodeTokenByName arrowRange RARROW

genTriviaBeforeClausePipe p.Range
+> genTriviaMainNodesBeforeClausePipe p.Range
HumbertoCortes marked this conversation as resolved.
Show resolved Hide resolved
+> ifElse hasBar (sepBar +> atCurrentColumnWithPrepend pat body) (pat +> body)

/// Each multiline member definition has a pre and post new line.
Expand Down
9 changes: 9 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,15 @@ let internal genTriviaBeforeClausePipe (rangeOfClause: range) ctx =
| None -> id
<| ctx

let internal genTriviaMainNodesBeforeClausePipe (rangeOfClause: range) ctx =
(Map.tryFindOrEmptyList SynMatchClause_Clause ctx.TriviaMainNodes)
|> List.tryFind (fun t -> RangeHelpers.rangeEq t.Range rangeOfClause)
|> fun trivia ->
match trivia with
| Some trivia -> indent +> (printContentBefore trivia)
HumbertoCortes marked this conversation as resolved.
Show resolved Hide resolved
| None -> id
<| ctx

let internal hasLineCommentAfterInfix (rangePlusInfix: range) (ctx: Context) =
match Map.tryFind SynExpr_Ident ctx.TriviaMainNodes with
| Some triviaNodes ->
Expand Down