未验证 提交 26b32189 编写于 作者: P Petr Pokorny 提交者: GitHub

Fix finding references to active pattern cases (#14966)

* active pattern find references - only search for the one provided, not all . And works also for .fsi files.
上级 fd534673
......@@ -15,6 +15,7 @@ open FSharp.Compiler.Text
open FSharp.Compiler.Text.Range
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeBasics
open FSharp.Compiler.Syntax.PrettyNaming
#nowarn "9"
#nowarn "51"
......@@ -301,6 +302,19 @@ and [<Sealed>] ItemKeyStoreBuilder() =
| ParentNone -> writeChar '%'
| Parent eref -> writeEntityRef eref
let writeActivePatternCase (apInfo: ActivePatternInfo) index =
writeString ItemKeyTags.itemActivePattern
match apInfo.ActiveTagsWithRanges with
| (_, m) :: _ -> m.FileName |> Path.GetFileNameWithoutExtension |> writeString
| _ -> ()
for tag in apInfo.ActiveTags do
writeChar '|'
writeString tag
writeInt32 index
member _.Write(m: range, item: Item) =
writeRange m
......@@ -325,13 +339,9 @@ and [<Sealed>] ItemKeyStoreBuilder() =
writeEntityRef info.TyconRef
writeString info.LogicalName
| Item.ActivePatternResult (info, _, _, _) ->
writeString ItemKeyTags.itemActivePattern
info.ActiveTags |> List.iter writeString
| Item.ActivePatternResult (info, _, index, _) -> writeActivePatternCase info index
| Item.ActivePatternCase elemRef ->
writeString ItemKeyTags.itemActivePattern
elemRef.ActivePatternInfo.ActiveTags |> List.iter writeString
| Item.ActivePatternCase elemRef -> writeActivePatternCase elemRef.ActivePatternInfo elemRef.CaseIndex
| Item.ExnCase tcref ->
writeString ItemKeyTags.itemExnCase
......
......@@ -301,3 +301,120 @@ let x = MyType()
"FileProgram.fs", 6, 8, 14
])
}
module ActivePatterns =
/// https://github.com/dotnet/fsharp/issues/14206
[<Fact>]
let ``Finding references to an active pattern case shouldn't find other cases`` () =
let source = """
let (|Even|Odd|) v =
if v % 2 = 0 then Even else Odd
match 2 with
| Even -> ()
| Odd -> ()
"""
let fileName, options, checker = singleFileChecker source
let symbolUse = getSymbolUse fileName source "Even" options checker |> Async.RunSynchronously
checker.FindBackgroundReferencesInFile(fileName, options, symbolUse.Symbol, fastCheck = true)
|> Async.RunSynchronously
|> expectToFind [
fileName, 2, 6, 10
fileName, 3, 22, 26
fileName, 5, 2, 6
]
[<Fact>]
let ``We don't find references to cases from other active patterns with the same name`` () =
let source = """
module One =
let (|Even|Odd|) v =
if v % 2 = 0 then Even else Odd
match 2 with
| Even -> ()
| Odd -> ()
module Two =
let (|Even|Steven|) v =
if v % 3 = 0 then Steven else Even
match 2 with
| Even -> ()
| Steven -> ()
"""
let fileName, options, checker = singleFileChecker source
let symbolUse = getSymbolUse fileName source "Even" options checker |> Async.RunSynchronously
checker.FindBackgroundReferencesInFile(fileName, options, symbolUse.Symbol, fastCheck = true)
|> Async.RunSynchronously
|> expectToFind [
fileName, 4, 10, 14
fileName, 5, 26, 30
fileName, 7, 6, 10
]
[<Fact>]
let ``We don't find references to cases the same active pattern defined in a different file`` () =
let source = """
let (|Even|Odd|) v =
if v % 2 = 0 then Even else Odd
match 2 with
| Even -> ()
| Odd -> ()
"""
SyntheticProject.Create(
{ sourceFile "First" [] with Source = source },
{ sourceFile "Second" [] with Source = source }
).Workflow {
placeCursor "First" "Even"
findAllReferences (expectToFind [
"FileFirst.fs", 3, 6, 10
"FileFirst.fs", 4, 22, 26
"FileFirst.fs", 6, 2, 6
])
}
[<Fact>]
let ``We find active patterns in other files when there are signature files`` () =
SyntheticProject.Create(
{ sourceFile "First" [] with
Source = "let (|Even|Odd|) v = if v % 2 = 0 then Even else Odd"
SignatureFile = AutoGenerated },
{ sourceFile "Second" [] with
Source = """
open ModuleFirst
match 2 with | Even -> () | Odd -> ()
""" }
).Workflow {
placeCursor "Second" "Even"
findAllReferences (expectToFind [
"FileFirst.fs", 2, 6, 10
"FileFirst.fs", 2, 39, 43
"FileSecond.fs", 4, 15, 19
])
}
/// Bug: https://github.com/dotnet/fsharp/issues/14969
[<Fact>]
let ``We DON'T find active patterns in signature files`` () =
SyntheticProject.Create(
{ sourceFile "First" [] with
Source = "let (|Even|Odd|) v = if v % 2 = 0 then Even else Odd"
SignatureFile = AutoGenerated }
).Workflow {
placeCursor "First" "Even"
findAllReferences (expectToFind [
"FileFirst.fs", 2, 6, 10
"FileFirst.fs", 2, 39, 43
//"FileFirst.fsi", 4, 6, 10 <-- this should also be found
])
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册