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

add a double indent when the indent_size is lower than the default 4 spaces #2785

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
* Duplicate newline in else if. [#2752](https://github.com/fsprojects/fantomas/issues/2752)
* Try-with expression with long when guard - when breaking line, add a double indent when the indent_size is lower than the default 4 spaces. [#2784](https://github.com/fsprojects/fantomas/issues/2784)

## [5.2.2] - 2023-02-18

Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<Compile Include="DallasTests.fs" />
<Compile Include="InlineTests.fs" />
<Compile Include="ChainTests.fs" />
<Compile Include="TryWithTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Core\Fantomas.Core.fsproj" />
Expand Down
31 changes: 31 additions & 0 deletions src/Fantomas.Core.Tests/TryWithTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Fantomas.Core.Tests.TryWithTests

open NUnit.Framework
open FsUnit
open Fantomas.Core.Tests.TestHelper

[<Test>]
let ``try-with expression with long when guard - when breaking line, add a double indent when the indent_size is lower than the default 4 spaces, 2784``
()
=
formatSourceString
false
"""
try
c ()
with
| :? WebSocketException as e when e.WebSocketErrorCode = WebSocketError.ConnectionClosedPrematurely && sourceParty = Agent ->
()
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
try
c ()
with :? WebSocketException as e when
e.WebSocketErrorCode = WebSocketError.ConnectionClosedPrematurely
&& sourceParty = Agent ->
()
"""
14 changes: 13 additions & 1 deletion src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,19 @@ let genExpr (e: Expr) =
onlyIfCtx (fun ctx -> linesAfter > linesBefore || hasWriteBeforeNewlineContent ctx) sepBar)
+> genPatInClause clauseNode.Pattern
+> optSingle
(fun e -> !- " when" +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr e))
(fun e ->
!- " when"
+> expressionFitsOnRestOfLine (sepSpace +> genExpr e) (fun ctx ->
// See https://github.com/fsprojects/fantomas/issues/2784
let doubleIndent = ctx.Config.IndentSize < 4

(indent
+> onlyIf doubleIndent indent
+> sepNln
+> genExpr e
+> unindent
+> onlyIf doubleIndent unindent)
ctx))
clauseNode.WhenExpr
+> sepSpace
+> genSingleTextNodeWithSpaceSuffix sepSpace clauseNode.Arrow
Expand Down