Skip to content

Commit

Permalink
Fix vanity alignment from 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 6791970
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2087,3 +2087,45 @@ 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
"""
13 changes: 12 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,10 @@ and genExpr astContext synExpr ctx =
+> expressionFitsOnRestOfLine
(genExpr astContext e
+> genWithAfterMatch withRange)
(genExprInIfOrMatch astContext e
(genExprInIfOrMatch
{ astContext with
IsInsideMatchClausePattern = true }
e
+> (sepNlnUnlessLastEventIsNewline
+> (genWithAfterMatch withRange)))

Expand Down Expand Up @@ -3320,6 +3323,14 @@ and genExprInIfOrMatch astContext (e: SynExpr) (ctx: Context) : Context =
let fallback =
if hasCommentBeforeExpr e then
genExpr astContext e |> indentNlnUnindentNln
elif astContext.IsInsideMatchClausePattern then
indent
+> sepNln
+> genExpr
{ astContext with
IsInsideMatchClausePattern = false }
e
+> unindent
else
sepNlnWhenWriteBeforeNewlineNotEmpty sepNone
+> genExpr astContext e
Expand Down

0 comments on commit 6791970

Please sign in to comment.