Skip to content

Commit

Permalink
Don't add parenthesis if last clause is single line. Fixes #1698
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 5, 2021
1 parent d8e76d4 commit 0237e8f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,3 +1754,30 @@ match x with
match x with
| :? (int) as i -> ()
"""

[<Test>]
let ``don't add parenthesis if last clause is single line, 1698`` () =
formatSourceString
false
"""
let select px =
match px with
| Shared.Foo _ -> "foo"
| Shared.LongerFoobarFoo -> "lf"
| Shared.Barry -> "barry"
|> List.singleton
|> instr "ziggy"
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let select px =
match px with
| Shared.Foo _ -> "foo"
| Shared.LongerFoobarFoo -> "lf"
| Shared.Barry -> "barry"
|> List.singleton
|> instr "ziggy"
"""
13 changes: 12 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,18 @@ and genExpr astContext synExpr ctx =
(match e with
| SynExpr.IfThenElse _
| SynExpr.Match _ when (ctx.Config.IndentSize <= operatorText.Length) ->
autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e)
(fun ctx ->
let ctxAfterMatch = genExpr astContext e ctx

let barOnLastLine =
match List.tryHead ctxAfterMatch.WriterModel.Lines with
| Some line -> line.TrimStart().StartsWith("| ")
| None -> false

if barOnLastLine then
ctxAfterMatch
else
autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e) ctx)
| _ -> genExpr astContext e)
+> sepNln
+> col
Expand Down

0 comments on commit 0237e8f

Please sign in to comment.