Skip to content

Commit

Permalink
Add spaces around custom operator definition starting with a star.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Aug 19, 2022
1 parent e29a890 commit 9d29f57
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/Fantomas.Core.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Fantomas.Core.Tests.OperatorTests

open System.Net.Http.Headers
open NUnit.Framework
open FsUnit
open Fantomas.Core.Tests.TestHelper
Expand Down Expand Up @@ -1349,3 +1348,18 @@ type Test =
{ WorkHoursPerWeek: uint<hr * (staff weeks)> }
static member create = { WorkHoursPerWeek = 40u<hr * (staff weeks)> }
"""

[<Test>]
let ``custom operator starting with *, 2434`` () =
formatSourceString
false
"""
let ( */) = (+)
"""
config
|> prepend newline
|> should
equal
"""
let ( */ ) = (+)
"""
7 changes: 7 additions & 0 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4450,6 +4450,13 @@ and genPat astContext pat =
+> sepColon
+> atCurrentColumnIndent (genType astContext false t))

| PatNamed (ao, SynIdent (_, Some (ParenStarSynIdent (lpr, op, rpr)))) ->
opt sepSpace ao genAccess
+> sepOpenTFor lpr
+> sepSpace
+> !-op
+> sepSpace
+> sepCloseTFor (Some rpr)
| PatNamed (ao, si) -> opt sepSpace ao genAccess +> genSynIdent false si
| PatAs (p1, p2, r) ->
genPat astContext p1 +> !- " as " +> genPat astContext p2
Expand Down
15 changes: 10 additions & 5 deletions src/Fantomas.Core/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -725,15 +725,20 @@ let (|DynamicExpr|_|) =
| SynExpr.Dynamic (funcExpr, _, argExpr, _) -> Some(funcExpr, argExpr)
| _ -> None

let (|ParenStarSynIdent|_|) =
function
| IdentTrivia.OriginalNotationWithParen (lpr, originalNotation, rpr) ->
if originalNotation.Length > 1 && originalNotation.StartsWith("*") then
Some(lpr, originalNotation, rpr)
else
None
| _ -> None

// Example: ( *** ) a b
// (*) a b is ok though
let (|ParenFunctionNameWithStar|_|) =
function
| LongIdentExpr (SynLongIdent ([ _ ],
[],
[ Some (IdentTrivia.OriginalNotationWithParen (lpr, originalNotation, rpr)) ])) when
(originalNotation.Length > 1 && originalNotation.StartsWith("*"))
->
| LongIdentExpr (SynLongIdent ([ _ ], [], [ Some (ParenStarSynIdent (lpr, originalNotation, rpr)) ])) ->
Some(lpr, originalNotation, rpr)
| _ -> None

Expand Down

0 comments on commit 9d29f57

Please sign in to comment.