Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\EditorTests.fs">
<Link>EditorTests.fs</Link>
</Compile>
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\Symbols.fs">
<Link>Symbols.fs</Link>
</Compile>
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\FileSystemTests.fs">
<Link>FileSystemTests.fs</Link>
</Compile>
Expand Down
6 changes: 5 additions & 1 deletion src/fsharp/symbols/Symbols.fs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ and FSharpActivePatternCase(cenv, apinfo: PrettyNaming.ActivePatternInfo, typ, n

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

member __.Index = n

member __.DeclarationLocation = snd apinfo.ActiveTagsWithRanges.[n]

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

and FSharpActivePatternGroup(cenv, apinfo:PrettyNaming.ActivePatternInfo, typ, valOpt) =


member __.Name = valOpt |> Option.map (fun vref -> vref.LogicalName)

member __.Names = makeReadOnlyCollection apinfo.Names

member __.IsTotal = apinfo.IsTotal
Expand Down
9 changes: 8 additions & 1 deletion src/fsharp/symbols/Symbols.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,10 @@ and [<Class>] public FSharpActivePatternCase =
inherit FSharpSymbol

/// The name of the active pattern case
member Name: string
member Name: string

/// Index of the case in the pattern group
member Index: int

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

/// Represents all cases within an active pattern
and [<Class>] public FSharpActivePatternGroup =

/// The whole group name
member Name: string option

/// The names of the active pattern cases
member Names: IList<string>

Expand Down
57 changes: 57 additions & 0 deletions tests/service/Symbols.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#if INTERACTIVE
#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
#r "../../packages/NUnit.3.5.0/lib/net45/nunit.framework.dll"
#load "FsUnit.fs"
#load "Common.fs"
#else
module Tests.Service.Symbols
#endif

open FSharp.Compiler.Service.Tests.Common
open FsUnit
open NUnit.Framework
open Microsoft.FSharp.Compiler.SourceCodeServices

module ActivePatterns =

let completePatternInput = """
let (|True|False|) = function
| true -> True
| false -> False

match true with
| True | False -> ()
"""

let partialPatternInput = """
let (|String|_|) = function
| :? String -> Some ()
| _ -> None

match "foo" with
| String
| _ -> ()
"""

let getCaseUsages source line =
let fileName, options = mkTestFileAndOptions source [| |]
let _, checkResults = parseAndCheckFile fileName source options

checkResults.GetAllUsesOfAllSymbolsInFile()
|> Async.RunSynchronously
|> Array.filter (fun su -> su.RangeAlternate.StartLine = line && su.Symbol :? FSharpActivePatternCase)
|> Array.map (fun su -> su.Symbol :?> FSharpActivePatternCase)

[<Test>]
let ``Active pattern case indices`` () =
let getIndices = Array.map (fun (case: FSharpActivePatternCase) -> case.Index)

getCaseUsages completePatternInput 7 |> getIndices |> shouldEqual [| 0; 1 |]
getCaseUsages partialPatternInput 7 |> getIndices |> shouldEqual [| 0 |]

[<Test>]
let ``Active pattern group names`` () =
let getGroupName (case: FSharpActivePatternCase) = case.Group.Name.Value

getCaseUsages completePatternInput 7 |> Array.head |> getGroupName |> shouldEqual "|True|False|"
getCaseUsages partialPatternInput 7 |> Array.head |> getGroupName |> shouldEqual "|String|_|"
5 changes: 4 additions & 1 deletion vsintegration/tests/unittests/VisualFSharp.UnitTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
<Compile Include="..\..\..\tests\service\Common.fs">
<Link>Common.fs</Link>
</Compile>
<Compile Include="..\..\..\tests\service\Symbols.fs">
<Link>ServiceAnalysis\Symbols.fs</Link>
</Compile>
<Compile Include="..\..\..\tests\service\EditorTests.fs">
<Link>EditorTests.fs</Link>
</Compile>
Expand Down Expand Up @@ -376,7 +379,7 @@
<HintPath>$(FSharpSourcesRoot)\..\packages\System.ValueTuple.4.3.1\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
<Private>true</Private>
</Reference>
<ProjectReference Include="..\Salsa\VisualFSharp.Salsa.fsproj">
<ProjectReference Include="..\Salsa\VisualFSharp.Salsa.fsproj">
<Name>VisualFSharp.Salsa</Name>
<Project>{fbd4b354-dc6e-4032-8ec7-c81d8dfb1af7}</Project>
<Private>True</Private>
Expand Down