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 Jan 18, 2022
1 parent 86fb434 commit 6089d2e
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 @@ -1635,7 +1635,7 @@ and genExpr astContext synExpr ctx =
+> expressionFitsOnRestOfLine
(genExpr astContext e
+> genWithAfterMatch withRange)
(genExprInIfOrMatch astContext e
(genExprInIfOrMatch astContext e true
+> (sepNlnUnlessLastEventIsNewline
+> (genWithAfterMatch withRange)))

Expand All @@ -1649,7 +1649,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 @@ -2191,7 +2191,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 @@ -2226,7 +2228,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 @@ -3321,7 +3323,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 @@ -3341,6 +3343,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 @@ -5572,7 +5579,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 @@ -5617,7 +5624,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 6089d2e

Please sign in to comment.