Skip to content

Commit 5ef3193

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/lsp/source/typerefs: reexpress tests wrt ExternalRefs
This change introduces a new API function that (in effect) computes the path through intra-package edges to imported symbols, and re-expresses all the tests in terms of it. A follow-up change will implement SCC-based graph optimizations within the Refs operation, but this bridge allows us to keep the tests unchanged during that transition, for increased confidence. Change-Id: I6735bee2ae8b9b940514bfd7145ad69cd442f117 Reviewed-on: https://go-review.googlesource.com/c/tools/+/481783 Run-TryBot: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent c5f768a commit 5ef3193

File tree

2 files changed

+209
-113
lines changed

2 files changed

+209
-113
lines changed

gopls/internal/lsp/source/typerefs/pkgrefs.go

+34
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,40 @@ func (g *PackageGraph) buildPackage(ctx context.Context, id source.PackageID) (*
177177
return p, nil
178178
}
179179

180+
// ExternalRefs returns a new map whose keys are the exported symbols
181+
// of the package (of the specified id, pkgIndex, and refs). The
182+
// corresponding value of each key is the set of exported symbols
183+
// indirectly referenced by it.
184+
//
185+
// TODO(adonovan): simplify the API once the SCC-based optimization lands.
186+
func ExternalRefs(pkgIndex *PackageIndex, id source.PackageID, refs map[string][]Ref) map[string]map[Ref]bool {
187+
// (This intrapackage recursion will go away in a follow-up CL.)
188+
var visit func(name string, res map[Ref]bool, seen map[string]bool)
189+
visit = func(name string, res map[Ref]bool, seen map[string]bool) {
190+
if !seen[name] {
191+
seen[name] = true
192+
for _, ref := range refs[name] {
193+
if pkgIndex.id(ref.pkg) == id {
194+
visit(ref.name, res, seen) // intrapackage recursion
195+
} else {
196+
res[ref] = true // cross-package ref
197+
}
198+
}
199+
}
200+
}
201+
202+
results := make(map[string]map[Ref]bool)
203+
for name := range refs {
204+
if token.IsExported(name) {
205+
res := make(map[Ref]bool)
206+
seen := make(map[string]bool)
207+
visit(name, res, seen)
208+
results[name] = res
209+
}
210+
}
211+
return results
212+
}
213+
180214
// reachableByName computes the set of packages that are reachable through
181215
// references, starting with the declaration for name in package p.
182216
func (g *PackageGraph) reachableByName(ctx context.Context, p *Package, name string, set *PackageSet, seen map[string]bool) error {

0 commit comments

Comments
 (0)