Skip to content

Commit

Permalink
Respect Context.IncludeDeclaration in handler for "textDocument/findR…
Browse files Browse the repository at this point in the history
…eferences"
  • Loading branch information
razzmatazz committed Nov 15, 2024
1 parent bd7f06c commit 364c876
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/CSharpLanguageServer/Handlers/CodeLens.fs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ module CodeLens =
match! context.FindSymbol lensData.DocumentUri lensData.Position with
| None -> return p |> success
| Some symbol ->
let! refs = context.FindReferences symbol
let! locations = context.FindReferences symbol false
// FIXME: refNum is wrong. There are lots of false positive even if we distinct locations by
// (l.Location.SourceTree.FilePath, l.Location.SourceSpan)
// (l.SourceTree.FilePath, l.SourceSpan)
let refNum =
refs
|> Seq.collect (fun r -> r.Locations)
|> Seq.distinctBy (fun l -> (l.Location.SourceTree.FilePath, l.Location.SourceSpan))
locations
|> Seq.distinctBy (fun l -> (l.SourceTree.FilePath, l.SourceSpan))
|> Seq.length

let title = sprintf "%d Reference(s)" refNum
Expand Down
8 changes: 4 additions & 4 deletions src/CSharpLanguageServer/Handlers/References.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ module References =
match! context.FindSymbol p.TextDocument.Uri p.Position with
| None -> return None |> success
| Some symbol ->
let! refs = context.FindReferences symbol
let! locations = context.FindReferences symbol p.Context.IncludeDeclaration

return
refs
|> Seq.collect (fun r -> r.Locations)
|> Seq.map (fun rl -> Location.fromRoslynLocation rl.Location)
locations
|> Seq.map Location.fromRoslynLocation
|> Seq.distinct
|> Seq.toArray
|> Some
Expand Down
16 changes: 13 additions & 3 deletions src/CSharpLanguageServer/State/ServerRequestContext.fs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ type ServerRequestContext (requestId: int, state: ServerState, emitServerEvent)
return aggregatedLspLocations
}

member this.FindSymbols (pattern: string option): Async<ISymbol seq> = async {
member this.FindSymbols (pattern: string option): Async<Microsoft.CodeAnalysis.ISymbol seq> = async {
let findTask ct =
match pattern with
| Some pat ->
Expand All @@ -209,14 +209,24 @@ type ServerRequestContext (requestId: int, state: ServerState, emitServerEvent)
return! findTask ct solution |> Async.AwaitTask
}

member this.FindReferences (symbol: ISymbol): Async<ReferencedSymbol seq> = async {
member this.FindReferences (symbol: ISymbol) (withDefinition: bool): Async<Microsoft.CodeAnalysis.Location seq> = async {
match this.State.Solution with
| None -> return []
| Some solution ->
let! ct = Async.CancellationToken
return!

let locationsFromReferencedSym (r: ReferencedSymbol) =
let locations = r.Locations |> Seq.map (fun rl -> rl.Location)

match withDefinition with
| true -> locations |> Seq.append r.Definition.Locations
| false -> locations

let! refs =
SymbolFinder.FindReferencesAsync(symbol, solution, cancellationToken=ct)
|> Async.AwaitTask

return refs |> Seq.collect locationsFromReferencedSym
}

member this.GetDocumentVersion (uri: DocumentUri): int option =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

<ItemGroup>
<Compile Include="Tooling.fs" />
<!--
<Compile Include="CodeActionTests.fs" />
<Compile Include="DiagnosticTests.fs" />
<Compile Include="DocumentationTests.fs" />
<Compile Include="HoverTests.fs" />
<Compile Include="InitializationTests.fs" />
-->
<Compile Include="ReferenceTests.fs" />
</ItemGroup>

Expand Down
33 changes: 33 additions & 0 deletions tests/CSharpLanguageServer.Tests/ReferenceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,36 @@ let testReferenceWorks() =
|]

Assert.AreEqual(expectedLocations1, locations1.Value)

//
// try references request at MethodA declaration on line 2
// (with IncludeDeclaration=true)
//
let referenceParams2: ReferenceParams =
{ TextDocument = { Uri = classFile.Uri }
Position = { Line = 2u; Character = 16u }
WorkDoneToken = None
PartialResultToken = None
Context = { IncludeDeclaration = true }
}

let locations2: Location[] option = classFile.Request("textDocument/references", referenceParams2)

let expectedLocations2: Location array =
[|
{ Uri = classFile.Uri.Substring(7)
Range = {
Start = { Line = 2u; Character = 16u }
End = { Line = 2u; Character = 23u }
}
}

{ Uri = classFile.Uri.Substring(7)
Range = {
Start = { Line = 8u; Character = 8u }
End = { Line = 8u; Character = 15u }
}
}
|]

Assert.AreEqual(expectedLocations2, locations2.Value)

0 comments on commit 364c876

Please sign in to comment.