Skip to content

Commit

Permalink
Add space before type provider named argument. Fixes #1209. (#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Dec 17, 2020
1 parent 28212db commit a212bf0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/Fantomas.Tests/TypeProviderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ type IntegerRegex = FSharpx.Regex< @"(?<value>\d+)" >"""
type IntegerRegex = FSharpx.Regex< @"(?<value>\d+)" >
"""

[<Test>]
let ``should add space before type provider named argument, 1209`` () =
formatSourceString
false
"""
type Graphml = XmlProvider<Schema= @"http://graphml.graphdrawing.org/xmlns/1.0/graphml-structure.xsd">
"""
config
|> prepend newline
|> should
equal
"""
type Graphml = XmlProvider<Schema= @"http://graphml.graphdrawing.org/xmlns/1.0/graphml-structure.xsd">
"""

[<Test>]
let ``should throw FormatException on unparsed input`` () =
Assert.Throws<Fantomas.FormatConfig.FormatException>
Expand Down
42 changes: 24 additions & 18 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3967,7 +3967,10 @@ and genType astContext outerBracket t =
| TMeasureDivide (t1, t2) -> loop t1 -- " / " +> loop t2
| TStaticConstant (c, r) -> genConst c r
| TStaticConstantExpr (e) -> genExpr astContext e
| TStaticConstantNamed (t1, t2) -> loop t1 -- "=" +> loop t2
| TStaticConstantNamed (t1, t2) ->
loop t1 -- "="
+> addSpaceIfSynTypeStaticConstantHasAtSignBeforeString t2
+> loop t2
| TArray (t, n) -> loop t -- " [" +> rep (n - 1) (!- ",") -- "]"
| TAnon -> sepWild
| TVar tp -> genTypar astContext tp
Expand Down Expand Up @@ -4063,6 +4066,24 @@ and genType astContext outerBracket t =
| TTuple ts -> ifElse outerBracket (sepOpenT +> loopTTupleList ts +> sepCloseT) (loopTTupleList ts)
| _ -> loop t

// for example: FSharpx.Regex< @"(?<value>\d+)" >
and addSpaceIfSynTypeStaticConstantHasAtSignBeforeString (t: SynType) (ctx: Context) =
let hasAtSign =
match t with
| TStaticConstant (_, r) ->
TriviaHelpers.``has content itself that matches``
(function
| StringContent sc -> sc.StartsWith("@")
| _ -> false)
r
(TriviaHelpers.getNodesForTypes
[ SynExpr_Const
SynType_StaticConstant ]
ctx.TriviaMainNodes)
| _ -> false

onlyIf hasAtSign sepSpace ctx

and genAnonRecordFieldType astContext (AnonRecordFieldType (s, t)) =
!-s +> sepColon +> (genType astContext false t)

Expand All @@ -4076,25 +4097,10 @@ and genPrefixTypes astContext node ctx =
-- " >")
ctx
| t :: _ ->
// for example: FSharpx.Regex< @"(?<value>\d+)" >
let firstItemHasAtSignBeforeString =
match t with
| TStaticConstant (_, r) ->
TriviaHelpers.``has content itself that matches``
(function
| StringContent sc -> sc.StartsWith("@")
| _ -> false)
r
(TriviaHelpers.getNodesForTypes
[ SynExpr_Const
SynType_StaticConstant ]
ctx.TriviaMainNodes)
| _ -> false

(!- "<"
+> onlyIf firstItemHasAtSignBeforeString sepSpace
+> addSpaceIfSynTypeStaticConstantHasAtSignBeforeString t
+> col sepComma node (genType astContext false)
+> onlyIf firstItemHasAtSignBeforeString sepSpace
+> addSpaceIfSynTypeStaticConstantHasAtSignBeforeString t
-- ">")
ctx

Expand Down

0 comments on commit a212bf0

Please sign in to comment.