Skip to content

Commit

Permalink
Trivia before bar of try with clause should indent. Fixes fsprojects#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 30, 2021
1 parent da572ef commit d0099f5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
69 changes: 69 additions & 0 deletions src/Fantomas.Tests/ControlStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,75 @@ with
:? FileNotFoundException -> false
"""

[<Test>]
let ``comment above pipe of try/with named clause, 1686`` () =
formatSourceString
false
"""
namespace Foo
module Foo =
let a =
try
failwith ""
with
// hi!
| :? Exception as e ->
failwith ""
"""
{ config with
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
IndentOnTryWith = true }
|> prepend newline
|> should
equal
"""
namespace Foo
module Foo =
let a =
try
failwith ""
with
// hi!
:? Exception as e -> failwith ""
"""

[<Test>]
let ``comment above pipe of try/with named clause, idempotent`` () =
formatSourceString
false
"""
namespace Foo
module Foo =
let a =
try
failwith ""
with
// hi!
:? Exception as e -> failwith ""
"""
{ config with
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
IndentOnTryWith = true }
|> prepend newline
|> should
equal
"""
namespace Foo
module Foo =
let a =
try
failwith ""
with
// hi!
:? Exception as e -> failwith ""
"""

[<Test>]
let ``respect IndentOnTryWith setting when there is trivia before SynMatchClause_Clause, 1647`` () =
formatSourceString
Expand Down
29 changes: 22 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2026,15 +2026,30 @@ and genExpr astContext synExpr ctx =
+> unindent
+> kw WITH !+~ "with"

let hasCommentBeforeClause (c: SynMatchClause) =
Map.tryFindOrEmptyList SynMatchClause_Clause ctx.TriviaMainNodes
|> List.exists
(fun node ->
RangeHelpers.rangeEq node.Range c.Range
&& TriviaHelpers.``has single line comment before`` node)
let hasCommentBeforeClause (c: SynMatchClause) (ctx: Context) : bool =
let barRange = ctx.MkRange e.Range.End c.Range.Start

let nodes =
(Map.tryFindOrEmptyList SynMatchClause_Clause ctx.TriviaMainNodes
|> List.choose
(fun node ->
if RangeHelpers.rangeEq node.Range c.Range then
Some node
else
None))
@ (Map.tryFindOrEmptyList BAR ctx.TriviaTokenNodes
|> List.choose
(fun node ->
if RangeHelpers.``range contains`` barRange node.Range then
Some node
else
None))

List.exists TriviaHelpers.``has single line comment before`` nodes


let genClause (astContext: ASTContext) (b: bool) (c: SynMatchClause) =
ifElse
ifElseCtx
(hasCommentBeforeClause c)
(indentOnWith
+> genClause astContext b c
Expand Down

0 comments on commit d0099f5

Please sign in to comment.