Skip to content

Commit

Permalink
Fix 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 Jan 3, 2022
1 parent 63a7eb5 commit 6aaad3f
Show file tree
Hide file tree
Showing 2 changed files with 57 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 @@ -2087,3 +2087,46 @@ match foo with
) -> bar ()
| _ -> ()
"""

[<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
"""
21 changes: 14 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ and genExpr astContext synExpr ctx =
+> expressionFitsOnRestOfLine
(genExpr astContext e
+> genWithAfterMatch withRange)
(genExprInIfOrMatch astContext e
(genExprInIfOrMatch astContext e true
+> (sepNlnUnlessLastEventIsNewline
+> (genWithAfterMatch withRange)))

Expand All @@ -1628,7 +1628,7 @@ and genExpr astContext synExpr ctx =
+> expressionFitsOnRestOfLine
(genExpr astContext e
+> genWithAfterMatch withRange)
(genExprInIfOrMatch astContext e
(genExprInIfOrMatch astContext e true
+> (sepNlnUnlessLastEventIsNewline
+> (genWithAfterMatch withRange)))

Expand Down Expand Up @@ -2170,7 +2170,9 @@ and genExpr astContext synExpr ctx =

let genElifMultiLine (elf1: SynExpr, elf2, elifKeywordRange, thenKeywordRange) =
(TriviaContext.``else if / elif`` elifKeywordRange)
+> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (genExprInIfOrMatch astContext (cleanIfExpr elf1))
+> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (
genExprInIfOrMatch astContext (cleanIfExpr elf1) false
)
+> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace
+> genThen thenKeywordRange
+> indent
Expand Down Expand Up @@ -2205,7 +2207,7 @@ and genExpr astContext synExpr ctx =
// x
// bool expr x should be indented
+> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty (
genExprInIfOrMatch astContext (cleanIfExpr e1)
genExprInIfOrMatch astContext (cleanIfExpr e1) false
+> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace
)
+> genThen synExpr.Range
Expand Down Expand Up @@ -3300,7 +3302,7 @@ and genAppWithSingleParenthesisArgument (e, lpr, a, rpr, pr) astContext =
+> (genExpr astContext a)
+> tokN (Option.defaultValue pr rpr) RPAREN sepCloseT

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 @@ -3320,6 +3322,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 @@ -5537,7 +5544,7 @@ and genKeepIndentMatch
let lastClauseIndex = clauses.Length - 1

ifElse (triviaType = SynExpr_MatchBang) !- "match! " !- "match "
+> genExprInIfOrMatch astContext e
+> genExprInIfOrMatch astContext e false
+> (fun ctx -> genWithAfterMatch (withRange ctx) ctx)
+> sepNln
+> coli
Expand Down Expand Up @@ -5582,7 +5589,7 @@ and genKeepIdentIf

let long =
ifElse (idx = 0) (!- "if ") (!- "elif ")
+> genExprInIfOrMatch astContext ifExpr
+> genExprInIfOrMatch astContext ifExpr false
+> sepSpace
+> !- "then"

Expand Down

0 comments on commit 6aaad3f

Please sign in to comment.