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 20, 2022
1 parent 4f64028 commit e83be9b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
53 changes: 46 additions & 7 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ module Foo =
return failwith "unreachable"
else
match baz with
| Some (AverageBetweenResponses (minimumNumberOfResponses,
averageFunc)) ->
return failwith "unreachable"
| Some
(
AverageBetweenResponses
(
minimumNumberOfResponses, averageFunc
)
) -> return failwith "unreachable"
| _ -> return failwith "unreachable"
}
"""
Expand Down Expand Up @@ -1217,8 +1221,10 @@ type Thing =
| Foo of msg: string
override this.ToString() =
match this with
| Foo (ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) ->
""
| Foo
(
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
) -> ""
"""

[<Test>]
Expand Down Expand Up @@ -1248,8 +1254,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 +2173,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
26 changes: 23 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5028,9 +5028,29 @@ 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
+> indent
+> genTriviaFor SynPat_Paren_OpeningParenthesis lpr sepOpenT
+> sepNln
+> genPat astContext p
+> unindent
+> 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 e83be9b

Please sign in to comment.