-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
references.go
280 lines (254 loc) · 8.39 KB
/
references.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package source
import (
"context"
"errors"
"fmt"
"go/ast"
"go/token"
"go/types"
"sort"
"strconv"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/span"
"golang.org/x/tools/internal/bug"
"golang.org/x/tools/internal/event"
)
// ReferenceInfo holds information about reference to an identifier in Go source.
type ReferenceInfo struct {
Name string
MappedRange
ident *ast.Ident
obj types.Object
pkg Package
isDeclaration bool
}
// isInPackageName reports whether the file's package name surrounds the
// given position pp (e.g. "foo" surrounds the cursor in "package foo").
func isInPackageName(ctx context.Context, s Snapshot, f FileHandle, pgf *ParsedGoFile, pp protocol.Position) (bool, error) {
// Find position of the package name declaration
cursorPos, err := pgf.Mapper.Pos(pp)
if err != nil {
return false, err
}
return pgf.File.Name.Pos() <= cursorPos && cursorPos <= pgf.File.Name.End(), nil
}
// References returns a list of references for a given identifier within the packages
// containing i.File. Declarations appear first in the result.
func References(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position, includeDeclaration bool) ([]*ReferenceInfo, error) {
ctx, done := event.Start(ctx, "source.References")
defer done()
// Find position of the package name declaration
pgf, err := s.ParseGo(ctx, f, ParseFull)
if err != nil {
return nil, err
}
packageName := pgf.File.Name.Name // from package decl
inPackageName, err := isInPackageName(ctx, s, f, pgf, pp)
if err != nil {
return nil, err
}
if inPackageName {
// TODO(rfindley): this is inaccurate, excluding test variants, and
// redundant with package renaming. Refactor to share logic.
renamingPkg, err := s.PackageForFile(ctx, f.URI(), TypecheckWorkspace, NarrowestPackage)
if err != nil {
return nil, err
}
// Find external references to the package.
rdeps, err := s.GetReverseDependencies(ctx, renamingPkg.ID())
if err != nil {
return nil, err
}
var refs []*ReferenceInfo
for _, dep := range rdeps {
for _, f := range dep.CompiledGoFiles() {
for _, imp := range f.File.Imports {
if path, err := strconv.Unquote(imp.Path.Value); err == nil && path == renamingPkg.PkgPath() {
refs = append(refs, &ReferenceInfo{
Name: packageName,
MappedRange: NewMappedRange(f.Tok, f.Mapper, imp.Pos(), imp.End()),
})
}
}
}
}
// Find internal references to the package within the package itself
for _, f := range renamingPkg.CompiledGoFiles() {
refs = append(refs, &ReferenceInfo{
Name: packageName,
MappedRange: NewMappedRange(f.Tok, f.Mapper, f.File.Name.Pos(), f.File.Name.End()),
})
}
return refs, nil
}
qualifiedObjs, err := qualifiedObjsAtProtocolPos(ctx, s, f.URI(), pp)
// Don't return references for builtin types.
if errors.Is(err, errBuiltin) {
return nil, nil
}
if err != nil {
return nil, err
}
refs, err := references(ctx, s, qualifiedObjs, includeDeclaration, true, false)
if err != nil {
return nil, err
}
toSort := refs
if includeDeclaration {
toSort = refs[1:]
}
sort.Slice(toSort, func(i, j int) bool {
x := CompareURI(toSort[i].URI(), toSort[j].URI())
if x == 0 {
return toSort[i].ident.Pos() < toSort[j].ident.Pos()
}
return x < 0
})
return refs, nil
}
// references is a helper function to avoid recomputing qualifiedObjsAtProtocolPos.
// The first element of qos is considered to be the declaration;
// if isDeclaration, the first result is an extra item for it.
// Only the definition-related fields of qualifiedObject are used.
// (Arguably it should accept a smaller data type.)
func references(ctx context.Context, snapshot Snapshot, qos []qualifiedObject, includeDeclaration, includeInterfaceRefs, includeEmbeddedRefs bool) ([]*ReferenceInfo, error) {
var (
references []*ReferenceInfo
seen = make(map[positionKey]bool)
)
pos := qos[0].obj.Pos()
if pos == token.NoPos {
return nil, fmt.Errorf("no position for %s", qos[0].obj) // e.g. error.Error
}
// Inv: qos[0].pkg != nil, since Pos is valid.
// Inv: qos[*].pkg != nil, since all qos are logically the same declaration.
filename := snapshot.FileSet().Position(pos).Filename
pgf, err := qos[0].pkg.File(span.URIFromPath(filename))
if err != nil {
return nil, err
}
declIdent, err := findIdentifier(ctx, snapshot, qos[0].pkg, pgf, qos[0].obj.Pos())
if err != nil {
return nil, err
}
// Make sure declaration is the first item in the response.
if includeDeclaration {
references = append(references, &ReferenceInfo{
MappedRange: declIdent.MappedRange,
Name: qos[0].obj.Name(),
ident: declIdent.ident,
obj: qos[0].obj,
pkg: declIdent.pkg,
isDeclaration: true,
})
}
for _, qo := range qos {
var searchPkgs []Package
// Only search dependents if the object is exported.
if qo.obj.Exported() {
reverseDeps, err := snapshot.GetReverseDependencies(ctx, qo.pkg.ID())
if err != nil {
return nil, err
}
searchPkgs = append(searchPkgs, reverseDeps...)
}
// Add the package in which the identifier is declared.
searchPkgs = append(searchPkgs, qo.pkg)
for _, pkg := range searchPkgs {
for ident, obj := range pkg.GetTypesInfo().Uses {
// For instantiated objects (as in methods or fields on instantiated
// types), we may not have pointer-identical objects but still want to
// consider them references.
if !equalOrigin(obj, qo.obj) {
// If ident is not a use of qo.obj, skip it, with one exception:
// uses of an embedded field can be considered references of the
// embedded type name
if !includeEmbeddedRefs {
continue
}
v, ok := obj.(*types.Var)
if !ok || !v.Embedded() {
continue
}
named, ok := v.Type().(*types.Named)
if !ok || named.Obj() != qo.obj {
continue
}
}
key, found := packagePositionKey(pkg, ident.Pos())
if !found {
bug.Reportf("ident %v (pos: %v) not found in package %v", ident.Name, ident.Pos(), pkg.Name())
continue
}
if seen[key] {
continue
}
seen[key] = true
rng, err := posToMappedRange(snapshot.FileSet(), pkg, ident.Pos(), ident.End())
if err != nil {
return nil, err
}
references = append(references, &ReferenceInfo{
Name: ident.Name,
ident: ident,
pkg: pkg,
obj: obj,
MappedRange: rng,
})
}
}
}
// When searching on type name, don't include interface references -- they
// would be things like all references to Stringer for any type that
// happened to have a String method.
_, isType := declIdent.Declaration.obj.(*types.TypeName)
if includeInterfaceRefs && !isType {
// TODO(adonovan): opt: don't go back into the position domain:
// we have complete type information already.
declRange, err := declIdent.Range()
if err != nil {
return nil, err
}
fh, err := snapshot.GetFile(ctx, declIdent.URI())
if err != nil {
return nil, err
}
interfaceRefs, err := interfaceReferences(ctx, snapshot, fh, declRange.Start)
if err != nil {
return nil, err
}
references = append(references, interfaceRefs...)
}
return references, nil
}
// equalOrigin reports whether obj1 and obj2 have equivalent origin object.
// This may be the case even if obj1 != obj2, if one or both of them is
// instantiated.
func equalOrigin(obj1, obj2 types.Object) bool {
return obj1.Pkg() == obj2.Pkg() && obj1.Pos() == obj2.Pos() && obj1.Name() == obj2.Name()
}
// interfaceReferences returns the references to the interfaces implemented by
// the type or method at the given position.
func interfaceReferences(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position) ([]*ReferenceInfo, error) {
implementations, err := implementations(ctx, s, f, pp)
if err != nil {
if errors.Is(err, ErrNotAType) {
return nil, nil
}
return nil, err
}
// Make a separate call to references() for each element
// since it treats the first qualifiedObject as a definition.
var refs []*ReferenceInfo
for _, impl := range implementations {
implRefs, err := references(ctx, s, []qualifiedObject{impl}, false, false, false)
if err != nil {
return nil, err
}
refs = append(refs, implRefs...)
}
return refs, nil
}