Skip to content

Commit 84e2968

Browse files
authored
Clarify deduplicating references and respect includeDeclaration param (#4138)
1 parent bbc1076 commit 84e2968

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

private/buf/buflsp/server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,14 @@ func (s *server) References(
412412
if symbol == nil {
413413
return nil, nil
414414
}
415-
// TODO: Determine why we have duplicate references here, and fix that instead of deduplicating at
416-
// this level.
417-
return xslices.Deduplicate(symbol.References()), nil
415+
// We deduplicate the references here in the case where a file's symbols have not yet
416+
// been refreshed, but a new file with references to symbols in said file is opened. This
417+
// can cause duplicate references to be appended and not all clients deduplicate the
418+
// returned references.
419+
//
420+
// We also do not want to refresh all symbols in the workspace when a single file is
421+
// interacted with, since that could be detrimental to performance.
422+
return xslices.Deduplicate(symbol.References(params.Context.IncludeDeclaration)), nil
418423
}
419424

420425
// Completion is the entry point for code completion.

private/buf/buflsp/symbol.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ func (s *symbol) Definition() protocol.Location {
124124

125125
// References returns the locations of references to the symbol (including the definition), if
126126
// applicable. Otherwise, it just returns the location of the symbol itself.
127-
func (s *symbol) References() []protocol.Location {
127+
// It also accepts the includeDeclaration param from the client - if true, the declaration
128+
// of the symbol is included as a reference.
129+
func (s *symbol) References(includeDeclaration bool) []protocol.Location {
128130
var references []protocol.Location
129131
referenceableKind, ok := s.kind.(*referenceable)
130132
if !ok && s.def != nil {
@@ -146,12 +148,14 @@ func (s *symbol) References() []protocol.Location {
146148
Range: s.Range(),
147149
})
148150
}
149-
// Add the definition of the symbol to the list of references.
150-
if s.def != nil {
151-
references = append(references, protocol.Location{
152-
URI: s.def.file.uri,
153-
Range: s.def.Range(),
154-
})
151+
if includeDeclaration {
152+
// Add the definition of the symbol to the list of references.
153+
if s.def != nil {
154+
references = append(references, protocol.Location{
155+
URI: s.def.file.uri,
156+
Range: s.def.Range(),
157+
})
158+
}
155159
}
156160
return references
157161
}

0 commit comments

Comments
 (0)