From a911d9008d1f732040244007778232b02ebb2b84 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Sun, 24 Nov 2019 13:04:54 -0500 Subject: [PATCH] internal/lsp: only search for references in reverse dependencies Updates golang/go#35597 Change-Id: I78e83ad0ee1ae3c59a7452c467b3abd34587a845 Reviewed-on: https://go-review.googlesource.com/c/tools/+/208657 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/cache/snapshot.go | 2 +- internal/lsp/source/references.go | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 17d15d3015f..4ae68bb4e1e 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -94,7 +94,7 @@ func (s *snapshot) PackageHandles(ctx context.Context, f source.File) ([]source. cphs = results } if len(cphs) == 0 { - return nil, errors.Errorf("no CheckPackageHandles for %s", f) + return nil, errors.Errorf("no CheckPackageHandles for %s", f.URI()) } return cphs, nil } diff --git a/internal/lsp/source/references.go b/internal/lsp/source/references.go index 921a7564450..88ecf113c6d 100644 --- a/internal/lsp/source/references.go +++ b/internal/lsp/source/references.go @@ -10,6 +10,8 @@ import ( "go/types" "golang.org/x/tools/go/types/objectpath" + "golang.org/x/tools/internal/lsp/telemetry" + "golang.org/x/tools/internal/telemetry/log" "golang.org/x/tools/internal/telemetry/trace" errors "golang.org/x/xerrors" ) @@ -73,12 +75,22 @@ func (i *IdentifierInfo) References(ctx context.Context) ([]*ReferenceInfo, erro var searchpkgs []Package if i.Declaration.obj.Exported() { // Only search all packages if the identifier is exported. - // TODO(matloob): This only needs to look into reverse-dependencies. - // Avoid checking types of other packages. - searchpkgs = i.Snapshot.KnownPackages(ctx) - } else { - searchpkgs = []Package{i.pkg} + for _, id := range i.Snapshot.GetReverseDependencies(i.pkg.ID()) { + cph, err := i.Snapshot.PackageHandle(ctx, id) + if err != nil { + log.Error(ctx, "References: no CheckPackageHandle", err, telemetry.Package.Of(id)) + continue + } + pkg, err := cph.Check(ctx) + if err != nil { + log.Error(ctx, "References: no Package", err, telemetry.Package.Of(id)) + continue + } + searchpkgs = append(searchpkgs, pkg) + } } + // Add the package in which the identifier is declared. + searchpkgs = append(searchpkgs, i.pkg) for _, pkg := range searchpkgs { for ident, obj := range pkg.GetTypesInfo().Uses { if obj == nil || !(sameObj(obj, i.Declaration.obj)) {