Skip to content

Commit

Permalink
Only add parenthesis when operator length is equal or greater than in…
Browse files Browse the repository at this point in the history
…dent size. Fixes #1532 (#1535)
  • Loading branch information
nojaf committed Mar 25, 2021
1 parent 5110176 commit a4257ea
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
32 changes: 28 additions & 4 deletions src/Fantomas.Tests/DynamicOperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,37 @@ let ``keep () when dynamic operator inside boolean expr, #476`` () =
equal
"""
let fieldColor (fieldNameX: string) =
(if f.errors?(fieldNameY) && f.touched?(fieldNameZ) then
IsDanger
else
NoColor)
if f.errors?(fieldNameY) && f.touched?(fieldNameZ) then
IsDanger
else
NoColor
|> Input.Color
"""

[<Test>]
let ``keep () when dynamic operator inside boolean expr, 2 spaces indent`` () =
formatSourceString
false
"""let fieldColor (fieldNameX: string) =
if f.errors?(fieldNameY) && f.touched?(fieldNameZ) then
IsDanger
else
NoColor
|> Input.Color
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let fieldColor (fieldNameX: string) =
(if f.errors?(fieldNameY) && f.touched?(fieldNameZ) then
IsDanger
else
NoColor)
|> Input.Color
"""

[<Test>]
let ``preserve back ticks from checked keyword, 937`` () =
formatSourceString false "let toggle = unbox<bool> (e.target?``checked``)" config
Expand Down
70 changes: 70 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,3 +1529,73 @@ let args =
| LongPatIndentifierThree |] -> args
| _ -> failwith "meh"
"""

[<Test>]
let ``match followed by pipe, 1532`` () =
formatSourceString
false
"""
match x with
| Foo f -> []
| Bar x ->
"\n"
+ columnHeadersText
+ "\n"
+ seprator
+ "\n"
+ itemsText
|> Some
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
(match x with
| Foo f -> []
| Bar x ->
"\n"
+ columnHeadersText
+ "\n"
+ seprator
+ "\n"
+ itemsText)
|> Some
"""

[<Test>]
let ``match followed by pipe, 4 spaces indent`` () =
formatSourceString
false
"""
match x with
| Foo f ->
"\n"
+ columnHeadersText
+ "\n"
+ seprator
+ "\n"
+ itemsText
| Bar x ->
// comment
""
|||> Some
"""
config
|> prepend newline
|> should
equal
"""
(match x with
| Foo f ->
"\n"
+ columnHeadersText
+ "\n"
+ seprator
+ "\n"
+ itemsText
| Bar x ->
// comment
"")
|||> Some
"""
14 changes: 10 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1731,8 +1731,12 @@ and genExpr astContext synExpr ctx =
+> genExpr astContext e)

let multilineExpr =
let operatorText = List.head es |> fun (s, _, _) -> s

(match e with
| SynExpr.IfThenElse _ -> autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e)
| SynExpr.IfThenElse _
| SynExpr.Match _ when (ctx.Config.IndentSize <= operatorText.Length) ->
autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e)
| _ -> genExpr astContext e)
+> sepNln
+> col
Expand Down Expand Up @@ -2707,10 +2711,12 @@ and genMultilineInfixExpr astContext e1 operatorText operatorExpr e2 =
if noBreakInfixOps.Contains(operatorText) then
genOnelinerInfixExpr astContext e1 operatorText operatorExpr e2
else
let genE1 =
let genE1 (ctx: Context) =
match e1 with
| SynExpr.IfThenElse _ -> autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e1)
| _ -> genExpr astContext e1
| SynExpr.IfThenElse _
| SynExpr.Match _ when (ctx.Config.IndentSize <= operatorText.Length) ->
autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e1) ctx
| _ -> genExpr astContext e1 ctx

atCurrentColumn (
genE1
Expand Down

0 comments on commit a4257ea

Please sign in to comment.