Skip to content

Commit a8115c1

Browse files
auduchinokKevinRansom
authored andcommitted
Symbols API: add Index to active pattern case, Name to pattern group (#4222)
* Symbols API: add Index to active pattern case, Name to pattern group * Symbols API: add active pattern case use tests
1 parent 452332f commit a8115c1

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

fcs/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.Tests.fsproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\EditorTests.fs">
4747
<Link>EditorTests.fs</Link>
4848
</Compile>
49+
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\Symbols.fs">
50+
<Link>Symbols.fs</Link>
51+
</Compile>
4952
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\FileSystemTests.fs">
5053
<Link>FileSystemTests.fs</Link>
5154
</Compile>

src/fsharp/symbols/Symbols.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,8 @@ and FSharpActivePatternCase(cenv, apinfo: PrettyNaming.ActivePatternInfo, typ, n
908908

909909
member __.Name = apinfo.ActiveTags.[n]
910910

911+
member __.Index = n
912+
911913
member __.DeclarationLocation = snd apinfo.ActiveTagsWithRanges.[n]
912914

913915
member __.Group = FSharpActivePatternGroup(cenv, apinfo, typ, valOpt)
@@ -926,7 +928,9 @@ and FSharpActivePatternCase(cenv, apinfo: PrettyNaming.ActivePatternInfo, typ, n
926928
| _ -> ""
927929

928930
and FSharpActivePatternGroup(cenv, apinfo:PrettyNaming.ActivePatternInfo, typ, valOpt) =
929-
931+
932+
member __.Name = valOpt |> Option.map (fun vref -> vref.LogicalName)
933+
930934
member __.Names = makeReadOnlyCollection apinfo.Names
931935

932936
member __.IsTotal = apinfo.IsTotal

src/fsharp/symbols/Symbols.fsi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,10 @@ and [<Class>] public FSharpActivePatternCase =
850850
inherit FSharpSymbol
851851

852852
/// The name of the active pattern case
853-
member Name: string
853+
member Name: string
854+
855+
/// Index of the case in the pattern group
856+
member Index: int
854857

855858
/// The location of declaration of the active pattern case
856859
member DeclarationLocation : range
@@ -866,6 +869,10 @@ and [<Class>] public FSharpActivePatternCase =
866869

867870
/// Represents all cases within an active pattern
868871
and [<Class>] public FSharpActivePatternGroup =
872+
873+
/// The whole group name
874+
member Name: string option
875+
869876
/// The names of the active pattern cases
870877
member Names: IList<string>
871878

tests/service/Symbols.fs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#if INTERACTIVE
2+
#r "../../Debug/fcs/net45/FSharp.Compiler.Service.dll" // note, run 'build fcs debug' to generate this, this DLL has a public API so can be used from F# Interactive
3+
#r "../../packages/NUnit.3.5.0/lib/net45/nunit.framework.dll"
4+
#load "FsUnit.fs"
5+
#load "Common.fs"
6+
#else
7+
module Tests.Service.Symbols
8+
#endif
9+
10+
open FSharp.Compiler.Service.Tests.Common
11+
open FsUnit
12+
open NUnit.Framework
13+
open Microsoft.FSharp.Compiler.SourceCodeServices
14+
15+
module ActivePatterns =
16+
17+
let completePatternInput = """
18+
let (|True|False|) = function
19+
| true -> True
20+
| false -> False
21+
22+
match true with
23+
| True | False -> ()
24+
"""
25+
26+
let partialPatternInput = """
27+
let (|String|_|) = function
28+
| :? String -> Some ()
29+
| _ -> None
30+
31+
match "foo" with
32+
| String
33+
| _ -> ()
34+
"""
35+
36+
let getCaseUsages source line =
37+
let fileName, options = mkTestFileAndOptions source [| |]
38+
let _, checkResults = parseAndCheckFile fileName source options
39+
40+
checkResults.GetAllUsesOfAllSymbolsInFile()
41+
|> Async.RunSynchronously
42+
|> Array.filter (fun su -> su.RangeAlternate.StartLine = line && su.Symbol :? FSharpActivePatternCase)
43+
|> Array.map (fun su -> su.Symbol :?> FSharpActivePatternCase)
44+
45+
[<Test>]
46+
let ``Active pattern case indices`` () =
47+
let getIndices = Array.map (fun (case: FSharpActivePatternCase) -> case.Index)
48+
49+
getCaseUsages completePatternInput 7 |> getIndices |> shouldEqual [| 0; 1 |]
50+
getCaseUsages partialPatternInput 7 |> getIndices |> shouldEqual [| 0 |]
51+
52+
[<Test>]
53+
let ``Active pattern group names`` () =
54+
let getGroupName (case: FSharpActivePatternCase) = case.Group.Name.Value
55+
56+
getCaseUsages completePatternInput 7 |> Array.head |> getGroupName |> shouldEqual "|True|False|"
57+
getCaseUsages partialPatternInput 7 |> Array.head |> getGroupName |> shouldEqual "|String|_|"

vsintegration/tests/unittests/VisualFSharp.UnitTests.fsproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
<Compile Include="..\..\..\tests\service\Common.fs">
6565
<Link>Common.fs</Link>
6666
</Compile>
67+
<Compile Include="..\..\..\tests\service\Symbols.fs">
68+
<Link>ServiceAnalysis\Symbols.fs</Link>
69+
</Compile>
6770
<Compile Include="..\..\..\tests\service\EditorTests.fs">
6871
<Link>EditorTests.fs</Link>
6972
</Compile>
@@ -376,7 +379,7 @@
376379
<HintPath>$(FSharpSourcesRoot)\..\packages\System.ValueTuple.4.3.1\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
377380
<Private>true</Private>
378381
</Reference>
379-
<ProjectReference Include="..\Salsa\VisualFSharp.Salsa.fsproj">
382+
<ProjectReference Include="..\Salsa\VisualFSharp.Salsa.fsproj">
380383
<Name>VisualFSharp.Salsa</Name>
381384
<Project>{fbd4b354-dc6e-4032-8ec7-c81d8dfb1af7}</Project>
382385
<Private>True</Private>

0 commit comments

Comments
 (0)