| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package xrefs |
| 10 |
|
| 11 |
import ( |
| 12 |
"go/ast" |
| 13 |
"go/types" |
| 14 |
"sort" |
| 15 |
|
| 16 |
"golang.org/x/tools/go/types/objectpath" |
| 17 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 18 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol" |
| 20 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 21 |
"golang.org/x/tools/gopls/internal/util/frob" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func NewIndex(files []*parsego.File, pkg *types.Package, info *types.Info) *Index { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
pkgObjects := make(map[*types.Package]map[types.Object]*gobObject) |
| 33 |
|
| 34 |
|
| 35 |
getObjects := func(pkg *types.Package) map[types.Object]*gobObject { |
| 36 |
objects, ok := pkgObjects[pkg] |
| 37 |
if !ok { |
| 38 |
objects = make(map[types.Object]*gobObject) |
| 39 |
pkgObjects[pkg] = objects |
| 40 |
} |
| 41 |
return objects |
| 42 |
} |
| 43 |
|
| 44 |
objectpathFor := new(objectpath.Encoder).For |
| 45 |
|
| 46 |
for fileIndex, pgf := range files { |
| 47 |
for cur := range pgf.Cursor().Preorder((*ast.Ident)(nil), (*ast.ImportSpec)(nil)) { |
| 48 |
switch n := cur.Node().(type) { |
| 49 |
case *ast.Ident: |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if n.IsExported() { |
| 54 |
if obj, ok := info.Uses[n]; ok && |
| 55 |
obj.Pkg() != nil && |
| 56 |
obj.Pkg() != pkg { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
if fn, ok := obj.(*types.Func); ok { |
| 61 |
obj = fn.Origin() |
| 62 |
} |
| 63 |
|
| 64 |
objects := getObjects(obj.Pkg()) |
| 65 |
gobObj, ok := objects[obj] |
| 66 |
if !ok { |
| 67 |
path, err := objectpathFor(obj) |
| 68 |
if err != nil { |
| 69 |
|
| 70 |
|
| 71 |
continue |
| 72 |
} |
| 73 |
gobObj = &gobObject{Path: path} |
| 74 |
objects[obj] = gobObj |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
if rng, err := pgf.NodeRange(n); err == nil { |
| 81 |
gobObj.Refs = append(gobObj.Refs, gobRef{ |
| 82 |
FileIndex: fileIndex, |
| 83 |
Range: rng, |
| 84 |
}) |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
case *ast.ImportSpec: |
| 90 |
|
| 91 |
|
| 92 |
pkgname := info.PkgNameOf(n) |
| 93 |
if pkgname == nil { |
| 94 |
continue |
| 95 |
} |
| 96 |
objects := getObjects(pkgname.Imported()) |
| 97 |
gobObj, ok := objects[nil] |
| 98 |
if !ok { |
| 99 |
gobObj = &gobObject{Path: ""} |
| 100 |
objects[nil] = gobObj |
| 101 |
} |
| 102 |
|
| 103 |
if rng, err := pgf.NodeRange(n.Path); err == nil { |
| 104 |
gobObj.Refs = append(gobObj.Refs, gobRef{ |
| 105 |
FileIndex: fileIndex, |
| 106 |
Range: rng, |
| 107 |
}) |
| 108 |
} else { |
| 109 |
bug.Reportf("out of bounds import spec %+v", n.Path) |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
var packages []*gobPackage |
| 117 |
for p := range pkgObjects { |
| 118 |
objects := pkgObjects[p] |
| 119 |
gp := &gobPackage{ |
| 120 |
PkgPath: metadata.PackagePath(p.Path()), |
| 121 |
Objects: make([]*gobObject, 0, len(objects)), |
| 122 |
} |
| 123 |
for _, gobObj := range objects { |
| 124 |
gp.Objects = append(gp.Objects, gobObj) |
| 125 |
} |
| 126 |
sort.Slice(gp.Objects, func(i, j int) bool { |
| 127 |
return gp.Objects[i].Path < gp.Objects[j].Path |
| 128 |
}) |
| 129 |
packages = append(packages, gp) |
| 130 |
} |
| 131 |
sort.Slice(packages, func(i, j int) bool { |
| 132 |
return packages[i].PkgPath < packages[j].PkgPath |
| 133 |
}) |
| 134 |
|
| 135 |
return &Index{packages: packages} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
type Index struct { |
| 141 |
packages []*gobPackage |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
func (idx *Index) Encode() []byte { |
| 146 |
return packageCodec.Encode(idx.packages) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
func Decode(data []byte) *Index { |
| 152 |
return &Index{packages: packageCodec.Decode(data)} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
func (idx *Index) Lookup(mp *metadata.Package, targets map[metadata.PackagePath]map[objectpath.Path]struct{}) (locs []protocol.Location) { |
| 159 |
for _, gp := range idx.packages { |
| 160 |
if objectSet, ok := targets[gp.PkgPath]; ok { |
| 161 |
for _, gobObj := range gp.Objects { |
| 162 |
if _, ok := objectSet[gobObj.Path]; ok { |
| 163 |
for _, ref := range gobObj.Refs { |
| 164 |
uri := mp.CompiledGoFiles[ref.FileIndex] |
| 165 |
locs = append(locs, protocol.Location{ |
| 166 |
URI: uri, |
| 167 |
Range: ref.Range, |
| 168 |
}) |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return locs |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
var packageCodec = frob.CodecFor[[]*gobPackage]() |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
type gobPackage struct { |
| 195 |
PkgPath metadata.PackagePath |
| 196 |
Objects []*gobObject |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
type gobObject struct { |
| 201 |
Path objectpath.Path |
| 202 |
Refs []gobRef |
| 203 |
} |
| 204 |
|
| 205 |
type gobRef struct { |
| 206 |
FileIndex int |
| 207 |
Range protocol.Range |
| 208 |
} |