Skip to content

Commit

Permalink
Avoid vanity alignment in long pattern match
Browse files Browse the repository at this point in the history
  • Loading branch information
su8898 authored and knocte committed Nov 11, 2021
1 parent 3a85ccb commit 892ae2d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2056,3 +2056,34 @@ match!
| None -> false
| Some balance -> someRetrievedBalance = balance
"""

[<Test>]
let ``vanity alignment used when using long case in match block, 1926`` () =
formatSourceString
false
"""
match foo with
| SomeVeryLongMatchCase(1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890, 1234567890) ->
bar()
| _ -> () """
config
|> prepend newline
|> should
equal
"""
match foo with
| SomeVeryLongMatchCase
(
1234567890,
1234567890,
1234567890,
1234567890,
1234567890,
1234567890,
1234567890,
1234567890,
1234567890,
1234567890
) -> bar ()
| _ -> ()
"""
23 changes: 19 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4971,10 +4971,25 @@ and genPat astContext pat =
| _ -> true
| _ -> true

ifElse isParenNecessary sepOpenT sepSpace
+> genPat astContext p
+> enterNodeTokenByName pat.Range RPAREN
+> ifElse isParenNecessary sepCloseT sepNone
let shortExpr =
ifElse isParenNecessary sepOpenT sepSpace
+> genPat astContext p
+> enterNodeTokenByName pat.Range RPAREN
+> ifElse isParenNecessary sepCloseT sepNone

let longExpr =
ifElse
astContext.IsInsideMatchClausePattern
(ifElse isParenNecessary ((indent +> sepNln +> sepOpenT +> indent +> sepNln)) sepNone)
(ifElse isParenNecessary sepOpenT sepSpace)
+> genPat astContext p
+> enterNodeTokenByName pat.Range RPAREN
+> (ifElse
astContext.IsInsideMatchClausePattern
(ifElse isParenNecessary (unindent +> sepNln +> sepCloseT +> unindent) sepNone)
(ifElse isParenNecessary sepCloseT sepNone))

expressionFitsOnRestOfLine shortExpr longExpr
| PatTuple ps ->
expressionFitsOnRestOfLine
(col sepComma ps (genPat astContext))
Expand Down

0 comments on commit 892ae2d

Please sign in to comment.