mirror of
https://github.com/golang/go
synced 2024-11-18 16:04:44 -07:00
1365742343
* Adds incoming calls hierarchy to gopls. Returns function declarations/function literals/files enclosing the call/s to the function being inpected. * Updates cmd to show ranges where calls to function in consideration are made by the caller. * Added tests for incoming calls. Example: This example shows call hierarchy for PathEnclosingInterval in tools/go/ast/astutil.go Show Call Hierarchy View: https://imgur.com/a/9VhspgA Peek Call Hierarchy View: https://imgur.com/a/XlKubFk Note: * Function literals show up as <scope>.func() in call hierarchy since they don't have a name. Here scope is either the function enclosing the literal or a file for top level declarations * Top level calls (calls not inside a function, ex: to initialize exported variables) show up as the file name * Clicking on an item shows the the range where a call is made in the scope Change-Id: I56426139e4e550dfabe43c9e9f1838efd1e43e38 Reviewed-on: https://go-review.googlesource.com/c/tools/+/247699 Run-TryBot: Danish Dua <danishdua@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
// 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"
|
|
"go/ast"
|
|
"go/token"
|
|
"go/types"
|
|
|
|
"golang.org/x/tools/internal/event"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/span"
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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()
|
|
|
|
qualifiedObjs, err := qualifiedObjsAtProtocolPos(ctx, s, f, pp)
|
|
// Don't return references for builtin types.
|
|
if xerrors.Is(err, errBuiltin) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return references(ctx, s, qualifiedObjs, includeDeclaration)
|
|
}
|
|
|
|
// references is a helper function to avoid recomputing qualifiedObjsAtProtocolPos.
|
|
func references(ctx context.Context, snapshot Snapshot, qos []qualifiedObject, includeDeclaration bool) ([]*ReferenceInfo, error) {
|
|
var (
|
|
references []*ReferenceInfo
|
|
seen = make(map[token.Position]bool)
|
|
)
|
|
|
|
// Make sure declaration is the first item in the response.
|
|
if includeDeclaration {
|
|
filename := snapshot.FileSet().Position(qos[0].obj.Pos()).Filename
|
|
pgf, err := qos[0].pkg.File(span.URIFromPath(filename))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ident, err := findIdentifier(ctx, snapshot, qos[0].pkg, pgf.File, qos[0].obj.Pos())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
references = append(references, &ReferenceInfo{
|
|
mappedRange: ident.mappedRange,
|
|
Name: qos[0].obj.Name(),
|
|
ident: ident.ident,
|
|
obj: qos[0].obj,
|
|
pkg: ident.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 {
|
|
if obj != qo.obj {
|
|
continue
|
|
}
|
|
pos := snapshot.FileSet().Position(ident.Pos())
|
|
if seen[pos] {
|
|
continue
|
|
}
|
|
seen[pos] = true
|
|
rng, err := posToMappedRange(snapshot, 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,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
return references, nil
|
|
}
|