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 20, 2022
1 parent e83be9b commit ce4fc0e
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 @@ -2155,6 +2155,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 @@ -2311,7 +2311,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 @@ -2346,7 +2346,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 @@ -3417,7 +3417,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 @@ -3437,6 +3437,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 @@ -5531,7 +5536,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 @@ -5591,7 +5596,7 @@ and genKeepIdentIf

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

Expand Down

0 comments on commit ce4fc0e

Please sign in to comment.