Skip to content

Commit

Permalink
fsprojects#1219 bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
humberto-javier.cortes-benavides committed Jan 13, 2021
1 parent f0b3cb5 commit 41e3cc9
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 72 deletions.
101 changes: 101 additions & 0 deletions src/Fantomas.Tests/ControlStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -735,3 +735,104 @@ let foldi (folder: 'State -> int -> 'T -> 'State) (state: 'State) (array: 'T [])
state
"""

[<Test>]
let ``line comment inside short `with` block (of a try-with), 1219`` () =
formatSourceString
false
"""
try
//comment1
TrySomething(someParam)
with
//comment2
ex ->
MakeSureToCleanup(someParam)
"""
config
|> prepend newline
|> should
equal
"""
try
//comment1
TrySomething(someParam)
with
//comment2
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
}
"""

[<Test>]
let ``line comment inside nested `with` block (of a try-with), 1219`` () =
formatSourceString
false
"""
try
//comment1
try
//comment2
TrySomething(someParam)
with
//comment3
ex ->
MakeSureToCleanup(someParam)
with
ex ->
Infrastructure.ReportWarning ex
return None
"""
config
|> prepend newline
|> should
equal
"""
try
//comment1
try
//comment2
TrySomething(someParam)
with
//comment3
ex -> MakeSureToCleanup(someParam)
with ex ->
Infrastructure.ReportWarning ex
return None
"""
62 changes: 0 additions & 62 deletions src/Fantomas.Tests/TriviaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -490,65 +490,3 @@ 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`` () =
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
}
"""
14 changes: 13 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,17 @@ and genExpr astContext synExpr ctx =
+> unindent
+> kw WITH !+~ "with"

let hasCommentBeforeClause (c: SynMatchClause) =
Map.tryFindOrEmptyList SynMatchClause_Clause ctx.TriviaMainNodes
|> List.filter (fun node -> RangeHelpers.rangeStartEq node.Range c.Range)
|> TriviaHelpers.``has sigle line comments before``

let genClause (astContext: ASTContext) (b: bool) (c: SynMatchClause) =
ifElse
(hasCommentBeforeClause c)
(indent +> genClause astContext b c +> unindent)
(genClause astContext b c)

match cs with
| [ SynMatchClause.Clause (SynPat.Or _, _, _, _, _) ] ->
atCurrentColumn (
Expand Down Expand Up @@ -4092,6 +4103,7 @@ and genClause astContext hasBar (Clause (p, e, eo)) =
{ astContext with
IsInsideMatchClausePattern = true }


let pat =
genPat astCtx p
+> optPre
Expand All @@ -4101,9 +4113,9 @@ and genClause astContext hasBar (Clause (p, e, eo)) =
(fun e -> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e))
+> sepArrow
+> leaveNodeTokenByName arrowRange RARROW
|> genTriviaFor SynMatchClause_Clause p.Range

genTriviaBeforeClausePipe p.Range
+> genTriviaMainNodesBeforeClausePipe p.Range
+> ifElse hasBar (sepBar +> atCurrentColumnWithPrepend pat body) (pat +> body)

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

let internal hasLineCommentAfterInfix (rangePlusInfix: range) (ctx: Context) =
match Map.tryFind SynExpr_Ident ctx.TriviaMainNodes with
| Some triviaNodes ->
Expand Down
12 changes: 12 additions & 0 deletions src/Fantomas/TriviaHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ module internal TriviaHelpers =
|> Option.map (fun t -> t.ContentAfter |> List.exists contentAfter)
|> Option.defaultValue false

let ``has sigle line comments before`` (triviaNodes: TriviaNode List) =
triviaNodes
|> List.exists
(fun nodes ->
nodes.ContentBefore
|> List.filter
(fun tn ->
match tn with
| Comment (LineCommentOnSingleLine _) -> true
| _ -> false)
|> (List.isEmpty >> not))

let ``has content after that ends with``
(findTrivia: TriviaNode -> bool)
(contentAfterEnd: TriviaContent -> bool)
Expand Down

0 comments on commit 41e3cc9

Please sign in to comment.