Skip to content

Commit

Permalink
Add space before at verbatim string in type abbreviation. Fixes fspro…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jul 9, 2020
1 parent 5923143 commit ed44a04
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/TypeProviderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let ``should add space before type provider params``() =
type IntegerRegex = FSharpx.Regex< @"(?<value>\d+)" >""" config
|> prepend newline
|> should equal """
type IntegerRegex = FSharpx.Regex<@"(?<value>\d+)">
type IntegerRegex = FSharpx.Regex< @"(?<value>\d+)" >
"""

[<Test>]
Expand Down
24 changes: 19 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2578,14 +2578,28 @@ and genType astContext outerBracket t =
and genAnonRecordFieldType astContext (AnonRecordFieldType(s, t)) =
!- s +> sepColon +> (genType astContext false t)

and genPrefixTypes astContext node =
and genPrefixTypes astContext node ctx =
match node with
| [] -> sepNone
| [] -> ctx
// Where < and ^ meet, we need an extra space. For example: seq< ^a >
| (TVar(Typar(_, true)) as t)::ts ->
!- "< " +> col sepComma (t::ts) (genType astContext false) -- " >"
| ts ->
!- "<" +> col sepComma ts (genType astContext false) -- ">"
(!- "< " +> col sepComma (t::ts) (genType astContext false) -- " >") ctx
| t::_ ->
// for example: FSharpx.Regex< @"(?<value>\d+)" >
let firstItemHasAtSignBeforeString =
match t with
| SourceParser.TStaticConstant(_,r) ->
TriviaHelpers.``has content itself that matches``
(function | StringContent sc -> sc.StartsWith("@") | _ -> false)
r
ctx.Trivia
| _ -> false

(!- "<"
+> onlyIf firstItemHasAtSignBeforeString sepSpace
+> col sepComma node (genType astContext false)
+> onlyIf firstItemHasAtSignBeforeString sepSpace
-- ">") ctx
// |> genTrivia node

and genTypeList astContext node =
Expand Down
7 changes: 7 additions & 0 deletions src/Fantomas/TriviaHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ module internal TriviaHelpers =
| Some(CharContent c) -> Some c
| _ -> None)

let ``has content itself that matches`` (predicate: TriviaContent -> bool) range (triviaNodes: TriviaNode list) =
triviaNodes
|> List.exists (fun tn ->
match tn.Range = range, tn.ContentItself with
| true, Some(t) -> predicate t
| _ -> false)

let ``has content itself is ident between ticks`` range (triviaNodes: TriviaNode list) =
triviaNodes
|> List.choose (fun tn ->
Expand Down

0 comments on commit ed44a04

Please sign in to comment.