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
2 people authored and knocte committed Apr 15, 2022
1 parent a1e4e4b commit 2a36ace
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
43 changes: 39 additions & 4 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,10 @@ type Thing =
| Foo of msg: string
override this.ToString() =
match this with
| Foo (ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) ->
""
| Foo
(
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
) -> ""
"""

[<Test>]
Expand Down Expand Up @@ -1248,8 +1250,10 @@ type Thing =
| Foo of msg : string
override this.ToString() : string =
match this with
| Foo (ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) ->
""
| Foo
(
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
) -> ""
"""

[<Test>]
Expand Down Expand Up @@ -2165,6 +2169,37 @@ match! a with // foo
| B b -> ()
"""

[<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 ()
| _ -> ()
"""

[<Test>]
let ``comment after match keyword, 1851`` () =
formatSourceString
Expand Down
25 changes: 22 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5026,9 +5026,28 @@ and genPat astContext pat =

| PatParen (_, PatUnitConst, _) -> !- "()"
| PatParen (lpr, p, rpr) ->
genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT
+> genPat astContext p
+> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT
let shortExpr =
genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT
+> genPat astContext p
+> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT

let longExpr =
ifElse
astContext.IsInsideMatchClausePattern
(indent
+> sepNln
+> genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT
+> sepNln
+> (fun ctx -> (rep ctx.Config.IndentSize (!- " ")) ctx)
+> genPat astContext p
+> sepNln
+> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT
+> unindent)
(genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT
+> genPat astContext p
+> genTriviaFor SynPat_Paren_ClosingParenthesis rpr sepCloseT)

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

0 comments on commit 2a36ace

Please sign in to comment.