Skip to content

Commit

Permalink
internal/lsp: fix references for transitive dependencies
Browse files Browse the repository at this point in the history
We need to search all transitive dependencies of a package, not just its
immediate imports.

Fixes golang/go#38100

Change-Id: I15b4dbe226ba851691ca0c95460c3648ede32f04
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227030
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Rohan Challa <rohan@golang.org>
  • Loading branch information
stamblerre committed Apr 2, 2020
1 parent 72cf467 commit 226fa68
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
13 changes: 10 additions & 3 deletions internal/lsp/source/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,18 @@ func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, fh FileHandle,
}
objs = append(objs, obj)
}
// Get all of the transitive dependencies of the search package.
pkgs := make(map[*types.Package]Package)
pkgs[searchpkg.GetTypes()] = searchpkg
for _, imp := range searchpkg.Imports() {
pkgs[imp.GetTypes()] = imp
var addPkg func(pkg Package)
addPkg = func(pkg Package) {
pkgs[pkg.GetTypes()] = pkg
for _, imp := range pkg.Imports() {
if _, ok := pkgs[imp.GetTypes()]; !ok {
addPkg(imp)
}
}
}
addPkg(searchpkg)
for _, obj := range objs {
if obj.Parent() == types.Universe {
return nil, fmt.Errorf("%w %q", errBuiltin, obj.Name())
Expand Down
13 changes: 13 additions & 0 deletions internal/lsp/testdata/lsp/primarymod/references/another/another.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package another has another type.
package another

import (
other "golang.org/x/tools/internal/lsp/references/other"
)

func _() {
xes := other.GetXes()
for _, x := range xes {
_ = x.Y //@mark(anotherXY, "Y"),refs("Y", typeXY, anotherXY, GetXesY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import (
references "golang.org/x/tools/internal/lsp/references"
)

func GetXes() []references.X {
return []references.X{
{
Y: 1, //@mark(GetXesY, "Y"),refs("Y", typeXY, GetXesY, anotherXY)
},
}
}

func _() {
references.Q = "hello" //@mark(assignExpQ, "Q")
bob := func(_ string) {}
Expand Down
4 changes: 4 additions & 0 deletions internal/lsp/testdata/lsp/primarymod/references/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package refs

type i int //@mark(typeI, "i"),refs("i", typeI, argI, returnI, embeddedI)

type X struct {
Y int //@mark(typeXY, "Y")
}

func _(_ i) []bool { //@mark(argI, "i")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/testdata/lsp/summary.txt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SuggestedFixCount = 6
DefinitionsCount = 45
TypeDefinitionsCount = 2
HighlightsCount = 52
ReferencesCount = 9
ReferencesCount = 11
RenamesCount = 24
PrepareRenamesCount = 7
SymbolsCount = 3
Expand Down

0 comments on commit 226fa68

Please sign in to comment.