Skip to content

Commit

Permalink
Don't add parenthesis if last clause is single line. Fixes fsprojects…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored and Florian Verdonck committed May 7, 2021
1 parent 2960f9c commit 4309f90
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 4309f90

Please sign in to comment.