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"
|
2019-06-07 08:04:22 -06:00
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
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) {
|
2019-06-26 20:46:12 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.References")
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
|
|
|
var (
|
|
|
|
references []*ReferenceInfo
|
|
|
|
seen = make(map[token.Position]bool)
|
|
|
|
fset = s.View().Session().Cache().FileSet()
|
|
|
|
)
|
|
|
|
|
2020-01-16 09:45:02 -07:00
|
|
|
// Make sure declaration is the first item in the response.
|
|
|
|
if includeDeclaration {
|
|
|
|
rng, err := objToMappedRange(s.View(), qualifiedObjs[0].pkg, qualifiedObjs[0].obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ident, _ := qualifiedObjs[0].node.(*ast.Ident)
|
|
|
|
references = append(references, &ReferenceInfo{
|
|
|
|
mappedRange: rng,
|
|
|
|
Name: qualifiedObjs[0].obj.Name(),
|
|
|
|
ident: ident,
|
|
|
|
obj: qualifiedObjs[0].obj,
|
|
|
|
pkg: qualifiedObjs[0].pkg,
|
|
|
|
isDeclaration: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
for _, qo := range qualifiedObjs {
|
|
|
|
var searchPkgs []Package
|
|
|
|
|
|
|
|
// Only search dependents if the object is exported.
|
|
|
|
if qo.obj.Exported() {
|
|
|
|
reverseDeps, err := s.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
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
|
|
|
for _, ph := range reverseDeps {
|
|
|
|
pkg, err := ph.Check(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
searchPkgs = append(searchPkgs, pkg)
|
2019-11-12 18:16:00 -07:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pos := fset.Position(ident.Pos())
|
|
|
|
if seen[pos] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[pos] = true
|
|
|
|
|
|
|
|
rng, err := posToMappedRange(s.View(), 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,
|
|
|
|
})
|
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-07-08 18:14:33 -06:00
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
return references, nil
|
2019-07-08 18:14:33 -06:00
|
|
|
}
|