Skip to content

Commit

Permalink
Avoid vanity alignment in multiline match clause
Browse files Browse the repository at this point in the history
  • Loading branch information
janus authored and knocte committed Apr 15, 2022
1 parent 265e517 commit 5a42da2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
43 changes: 43 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,49 @@ match! // foo
| B b -> ()
"""

[<Test>]
let ``vanity alignment removed from multiline match expression`` () =
formatSourceString
false
"""
match directoryRouter.GetIdentity()
|> self.ServerDescriptors.TryFind with
| None -> CircuitNodeDetail.FastCreate
| Some serverDescriptor ->
self.ConvertToCircuitNodeDetail serverDescriptor
"""
config
|> prepend newline
|> should
equal
"""
match
directoryRouter.GetIdentity()
|> self.ServerDescriptors.TryFind
with
| None -> CircuitNodeDetail.FastCreate
| Some serverDescriptor -> self.ConvertToCircuitNodeDetail serverDescriptor
"""

[<Test>]
let ``match expression covering one line`` () =
formatSourceString
false
"""
match directoryRouter.GetIdentity() with
| None -> CircuitNodeDetail.FastCreate
| Some serverDescriptor ->
self.ConvertToCircuitNodeDetail serverDescriptor"""
config
|> prepend newline
|> should
equal
"""
match directoryRouter.GetIdentity() with
| None -> CircuitNodeDetail.FastCreate
| Some serverDescriptor -> self.ConvertToCircuitNodeDetail serverDescriptor
"""

[<Test>]
let ``comment after with keyword in match bang`` () =
formatSourceString
Expand Down
19 changes: 12 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ and genExpr astContext synExpr ctx =
expressionFitsOnRestOfLine
(genExpr astContext e
+> genWithAfterMatch SynExpr_Match_With withRange)
(genExprInIfOrMatch astContext e
(genExprInIfOrMatch astContext e true
+> (sepNlnUnlessLastEventIsNewline
+> (genWithAfterMatch SynExpr_Match_With withRange)))
)
Expand All @@ -1618,7 +1618,7 @@ and genExpr astContext synExpr ctx =
expressionFitsOnRestOfLine
(genExpr astContext e
+> genWithAfterMatch SynExpr_MatchBang_With withRange)
(genExprInIfOrMatch astContext e
(genExprInIfOrMatch astContext e true
+> (sepNlnUnlessLastEventIsNewline
+> (genWithAfterMatch SynExpr_MatchBang_With withRange)))
)
Expand Down Expand Up @@ -2309,7 +2309,7 @@ and genExpr astContext synExpr ctx =
optSingle genElse elseKw
+> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace
+> genIf ifKw isElif
+> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (genExprInIfOrMatch astContext e1)
+> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (genExprInIfOrMatch astContext e1 false)
+> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace
+> genThen thenKw
+> indent
Expand Down Expand Up @@ -2344,7 +2344,7 @@ and genExpr astContext synExpr ctx =
// x
// bool expr x should be indented
+> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (
genExprInIfOrMatch astContext e1
genExprInIfOrMatch astContext e1 false
+> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace
)
+> genThen thenKw
Expand Down Expand Up @@ -3415,7 +3415,7 @@ and genAppWithSingleParenthesisArgument (e, lpr, a, rpr, _pr) astContext =
+> (genExpr astContext a)
+> sepCloseTFor rpr

and genExprInIfOrMatch astContext (e: SynExpr) (ctx: Context) : Context =
and genExprInIfOrMatch astContext (e: SynExpr) (shouldBeSplitToNextLine: bool) (ctx: Context) : Context =
let short =
sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace
+> genExpr astContext e
Expand All @@ -3435,6 +3435,11 @@ and genExprInIfOrMatch astContext (e: SynExpr) (ctx: Context) : Context =
let fallback =
if hasCommentBeforeExpr e then
genExpr astContext e |> indentNlnUnindentNln
elif shouldBeSplitToNextLine then
indent
+> sepNln
+> genExpr astContext e
+> unindent
else
sepNlnWhenWriteBeforeNewlineNotEmpty sepNone
+> genExpr astContext e
Expand Down Expand Up @@ -5528,7 +5533,7 @@ and genKeepIndentMatch
(genTriviaFor SynExpr_MatchBang_Match matchKeyword !- "match! ")
(genTriviaFor SynExpr_Match_Match matchKeyword !- "match ")
+> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (
genExprInIfOrMatch astContext e
genExprInIfOrMatch astContext e false
+> genWithAfterMatch
(if isMatchBang then
SynExpr_MatchBang_With
Expand Down Expand Up @@ -5588,7 +5593,7 @@ and genKeepIdentIf

let long =
genKeywordStart
+> genExprInIfOrMatch astContext ifExpr
+> genExprInIfOrMatch astContext ifExpr false
+> sepSpace
+> !- "then"

Expand Down

0 comments on commit 5a42da2

Please sign in to comment.