Skip to content

Commit

Permalink
Merge pull request #2785 from theimowski/fix-2784
Browse files Browse the repository at this point in the history
add a double indent when the indent_size is lower than the default 4 spaces
  • Loading branch information
dawedawe authored Mar 6, 2023
2 parents 05b24cd + 23a92c8 commit 45c569d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
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

0 comments on commit 45c569d

Please sign in to comment.