2019-06-11 13:09:43 -06:00
|
|
|
// 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.
|
|
|
|
|
2019-06-07 08:04:22 -06:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go/ast"
|
2019-12-17 22:06:31 -07:00
|
|
|
"go/token"
|
2019-06-11 13:09:43 -06:00
|
|
|
"go/types"
|
2020-08-25 22:57:24 -06:00
|
|
|
"sort"
|
2019-06-07 08:04:22 -06:00
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2019-12-17 22:06:31 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2020-07-29 22:12:55 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2020-04-02 15:27:48 -06:00
|
|
|
"golang.org/x/xerrors"
|
2019-06-07 08:04:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// ReferenceInfo holds information about reference to an identifier in Go source.
|
|
|
|
type ReferenceInfo struct {
|
2019-08-26 22:26:45 -06:00
|
|
|
Name string
|
|
|
|
mappedRange
|
2019-06-20 13:24:17 -06:00
|
|
|
ident *ast.Ident
|
|
|
|
obj types.Object
|
2019-07-08 19:53:01 -06:00
|
|
|
pkg Package
|
2019-06-20 13:24:17 -06:00
|
|
|
isDeclaration bool
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:05:29 -06:00
|
|
|
// References returns a list of references for a given identifier within the packages
|
2019-07-02 15:41:09 -06:00
|
|
|
// containing i.File. Declarations appear first in the result.
|
2019-12-17 22:06:31 -07:00
|
|
|
func References(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position, includeDeclaration bool) ([]*ReferenceInfo, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.References")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-09-09 17:26:26 -06:00
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
qualifiedObjs, err := qualifiedObjsAtProtocolPos(ctx, s, f, pp)
|
2020-02-06 12:27:47 -07:00
|
|
|
// Don't return references for builtin types.
|
2020-04-02 15:27:48 -06:00
|
|
|
if xerrors.Is(err, errBuiltin) {
|
2020-02-06 12:27:47 -07:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-26 21:25:15 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-25 22:57:24 -06:00
|
|
|
refs, err := references(ctx, s, qualifiedObjs, includeDeclaration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
toSort := refs
|
|
|
|
if includeDeclaration {
|
|
|
|
toSort = refs[1:]
|
|
|
|
}
|
|
|
|
sort.Slice(toSort, func(i, j int) bool {
|
|
|
|
x := span.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
|
2020-03-26 21:25:15 -06:00
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
2020-08-07 15:11:10 -06:00
|
|
|
// references is a helper function to avoid recomputing qualifiedObjsAtProtocolPos.
|
2020-07-28 15:00:10 -06:00
|
|
|
func references(ctx context.Context, snapshot Snapshot, qos []qualifiedObject, includeDeclaration bool) ([]*ReferenceInfo, error) {
|
2019-12-17 22:06:31 -07:00
|
|
|
var (
|
|
|
|
references []*ReferenceInfo
|
|
|
|
seen = make(map[token.Position]bool)
|
|
|
|
)
|
|
|
|
|
2020-01-16 09:45:02 -07:00
|
|
|
// Make sure declaration is the first item in the response.
|
|
|
|
if includeDeclaration {
|
2020-07-28 15:00:10 -06:00
|
|
|
filename := snapshot.FileSet().Position(qos[0].obj.Pos()).Filename
|
2020-07-29 22:12:55 -06:00
|
|
|
pgf, err := qos[0].pkg.File(span.URIFromPath(filename))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
ident, err := findIdentifier(ctx, snapshot, qos[0].pkg, pgf.File, qos[0].obj.Pos())
|
2020-01-16 09:45:02 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
references = append(references, &ReferenceInfo{
|
2020-07-29 22:12:55 -06:00
|
|
|
mappedRange: ident.mappedRange,
|
2020-03-26 21:25:15 -06:00
|
|
|
Name: qos[0].obj.Name(),
|
2020-07-29 22:12:55 -06:00
|
|
|
ident: ident.ident,
|
2020-03-26 21:25:15 -06:00
|
|
|
obj: qos[0].obj,
|
2020-07-29 22:12:55 -06:00
|
|
|
pkg: ident.pkg,
|
2020-01-16 09:45:02 -07:00
|
|
|
isDeclaration: true,
|
|
|
|
})
|
|
|
|
}
|
2020-03-26 21:25:15 -06:00
|
|
|
for _, qo := range qos {
|
2019-12-17 22:06:31 -07:00
|
|
|
var searchPkgs []Package
|
|
|
|
|
|
|
|
// Only search dependents if the object is exported.
|
|
|
|
if qo.obj.Exported() {
|
2020-07-28 15:00:10 -06:00
|
|
|
reverseDeps, err := snapshot.GetReverseDependencies(ctx, qo.pkg.ID())
|
2019-11-24 11:04:54 -07:00
|
|
|
if err != nil {
|
2020-01-08 11:17:22 -07:00
|
|
|
return nil, err
|
2019-11-24 11:04:54 -07:00
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
searchPkgs = append(searchPkgs, reverseDeps...)
|
2019-12-17 22:06:31 -07:00
|
|
|
}
|
|
|
|
// 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 {
|
|
|
|
if obj != qo.obj {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
pos := snapshot.FileSet().Position(ident.Pos())
|
2019-12-17 22:06:31 -07:00
|
|
|
if seen[pos] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[pos] = true
|
2020-07-28 15:00:10 -06:00
|
|
|
rng, err := posToMappedRange(snapshot, pkg, ident.Pos(), ident.End())
|
2019-12-17 22:06:31 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
references = append(references, &ReferenceInfo{
|
|
|
|
Name: ident.Name,
|
|
|
|
ident: ident,
|
|
|
|
pkg: pkg,
|
|
|
|
obj: obj,
|
|
|
|
mappedRange: rng,
|
|
|
|
})
|
2019-11-12 18:16:00 -07:00
|
|
|
}
|
2019-06-26 16:05:29 -06:00
|
|
|
}
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
return references, nil
|
2019-07-08 18:14:33 -06:00
|
|
|
}
|