Skip to content

Commit

Permalink
Only indent match clause if setting IndentOnTryWith is active. Fixes f…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 16, 2021
1 parent 4d0bd2b commit cc38f3e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 4 deletions.
135 changes: 132 additions & 3 deletions src/Fantomas.Tests/ControlStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ let ``line comment inside short `with` block (of a try-with), 1219`` () =
ex ->
MakeSureToCleanup(someParam)
"""
config
{ config with IndentOnTryWith = true }
|> prepend newline
|> should
equal
Expand Down Expand Up @@ -784,7 +784,7 @@ let ``line comment inside `with` block (of a try-with), 1219`` () =
return None
}
"""
config
{ config with IndentOnTryWith = true }
|> prepend newline
|> should
equal
Expand Down Expand Up @@ -823,7 +823,7 @@ let ``line comment inside nested `with` block (of a try-with), 1219`` () =
Infrastructure.ReportWarning ex
return None
"""
config
{ config with IndentOnTryWith = true }
|> prepend newline
|> should
equal
Expand Down Expand Up @@ -901,3 +901,132 @@ things
| Foo _
| Bar _ as e when true -> None)
"""

[<Test>]
let ``comment above pipe of try/with`` () =
formatSourceString
false
"""
try
let defaultTime = (DateTime.FromFileTimeUtc 0L).ToLocalTime ()
foo.CreationTime <> defaultTime
with
// hmm
| :? FileNotFoundException -> false
"""
config
|> prepend newline
|> should
equal
"""
try
let defaultTime =
(DateTime.FromFileTimeUtc 0L).ToLocalTime()
foo.CreationTime <> defaultTime
with
// hmm
:? FileNotFoundException -> false
"""

[<Test>]
let ``comment above pipe of try/with, idempotent`` () =
formatSourceString
false
"""
try
let defaultTime =
(DateTime.FromFileTimeUtc 0L).ToLocalTime()
foo.CreationTime <> defaultTime
with
// hmm
:? FileNotFoundException -> false
"""
config
|> prepend newline
|> should
equal
"""
try
let defaultTime =
(DateTime.FromFileTimeUtc 0L).ToLocalTime()
foo.CreationTime <> defaultTime
with
// hmm
:? FileNotFoundException -> false
"""

[<Test>]
let ``respect IndentOnTryWith setting when there is trivia before SynMatchClause_Clause, 1647`` () =
formatSourceString
false
"""
module Foo =
let blah () =
match foo with
| Thing crate ->
crate.Apply
{ new Evaluator<_, _> with
member __.Eval inner teq =
let foo =
// blah
let exists =
try
let defaultTime =
(DateTime.FromFileTimeUtc 0L).ToLocalTime ()
foo.CreationTime <> defaultTime
with
// hmm
:? FileNotFoundException -> false
exists
()
}
"""
{ config with
SpaceBeforeUppercaseInvocation = true
SpaceBeforeClassConstructor = true
SpaceBeforeMember = true
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
MultilineBlockBracketsOnSameColumn = true
NewlineBetweenTypeDefinitionAndMembers = true
KeepIfThenInSameLine = true
AlignFunctionSignatureToIndentation = true
AlternativeLongMemberDefinitions = true
MultiLineLambdaClosingNewline = true
KeepIndentInBranch = true }
|> prepend newline
|> should
equal
"""
module Foo =
let blah () =
match foo with
| Thing crate ->
crate.Apply
{ new Evaluator<_, _> with
member __.Eval inner teq =
let foo =
// blah
let exists =
try
let defaultTime =
(DateTime.FromFileTimeUtc 0L).ToLocalTime ()
foo.CreationTime <> defaultTime
with
// hmm
:? FileNotFoundException -> false
exists
()
}
"""
4 changes: 3 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,9 @@ and genExpr astContext synExpr ctx =
let genClause (astContext: ASTContext) (b: bool) (c: SynMatchClause) =
ifElse
(hasCommentBeforeClause c)
(indent +> genClause astContext b c +> unindent)
(indentOnWith
+> genClause astContext b c
+> unindentOnWith)
(genClause astContext b c)

match cs with
Expand Down

0 comments on commit cc38f3e

Please sign in to comment.