Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add space before type provider named argument. #1298

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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