1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:34:41 -07:00

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 <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-11-24 13:04:54 -05:00
parent 89d49d945a
commit a911d9008d
2 changed files with 18 additions and 6 deletions

View File

@ -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
}

View File

@ -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)) {