2020-08-03 12:16:44 -06:00
|
|
|
// Copyright 2020 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"
|
2020-08-07 15:11:10 -06:00
|
|
|
"fmt"
|
2020-08-03 12:16:44 -06:00
|
|
|
"go/ast"
|
2020-08-07 15:11:10 -06:00
|
|
|
"go/token"
|
2020-08-12 10:34:53 -06:00
|
|
|
"go/types"
|
2020-08-07 15:11:10 -06:00
|
|
|
"path/filepath"
|
2020-08-03 12:16:44 -06:00
|
|
|
|
2020-08-07 15:11:10 -06:00
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
2020-08-03 12:16:44 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2020-08-12 10:34:53 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2020-08-03 12:16:44 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
// PrepareCallHierarchy returns an array of CallHierarchyItem for a file and the position within the file.
|
2020-08-03 12:16:44 -06:00
|
|
|
func PrepareCallHierarchy(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protocol.Position) ([]protocol.CallHierarchyItem, error) {
|
2020-08-12 10:34:53 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.PrepareCallHierarchy")
|
2020-08-03 12:16:44 -06:00
|
|
|
defer done()
|
|
|
|
|
|
|
|
identifier, err := Identifier(ctx, snapshot, fh, pos)
|
|
|
|
if err != nil {
|
2020-08-12 10:34:53 -06:00
|
|
|
if errors.Is(err, ErrNoIdentFound) || errors.Is(err, errNoObjectFound) {
|
|
|
|
return nil, nil
|
2020-08-03 12:16:44 -06:00
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := identifier.Declaration.obj.Type().Underlying().(*types.Signature); !ok {
|
2020-08-03 12:16:44 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
if len(identifier.Declaration.MappedRange) == 0 {
|
2020-08-03 12:16:44 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
declMappedRange := identifier.Declaration.MappedRange[0]
|
|
|
|
rng, err := declMappedRange.Range()
|
2020-08-03 12:16:44 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
|
2020-08-03 12:16:44 -06:00
|
|
|
callHierarchyItem := protocol.CallHierarchyItem{
|
|
|
|
Name: identifier.Name,
|
|
|
|
Kind: protocol.Function,
|
|
|
|
Tags: []protocol.SymbolTag{},
|
2020-08-12 10:34:53 -06:00
|
|
|
Detail: fmt.Sprintf("%s • %s", identifier.Declaration.obj.Pkg().Path(), filepath.Base(declMappedRange.URI().Filename())),
|
|
|
|
URI: protocol.DocumentURI(declMappedRange.URI()),
|
2020-08-03 12:16:44 -06:00
|
|
|
Range: rng,
|
|
|
|
SelectionRange: rng,
|
|
|
|
}
|
|
|
|
return []protocol.CallHierarchyItem{callHierarchyItem}, nil
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
// IncomingCalls returns an array of CallHierarchyIncomingCall for a file and the position within the file.
|
2020-08-03 12:16:44 -06:00
|
|
|
func IncomingCalls(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protocol.Position) ([]protocol.CallHierarchyIncomingCall, error) {
|
2020-08-12 10:34:53 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.IncomingCalls")
|
2020-08-03 12:16:44 -06:00
|
|
|
defer done()
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
refs, err := References(ctx, snapshot, fh, pos, false)
|
2020-08-07 15:11:10 -06:00
|
|
|
if err != nil {
|
2020-08-12 10:34:53 -06:00
|
|
|
if errors.Is(err, ErrNoIdentFound) || errors.Is(err, errNoObjectFound) {
|
|
|
|
return nil, nil
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-08 09:15:22 -06:00
|
|
|
|
2020-08-07 15:11:10 -06:00
|
|
|
return toProtocolIncomingCalls(ctx, snapshot, refs)
|
2020-08-03 12:16:44 -06:00
|
|
|
}
|
|
|
|
|
2020-08-07 15:11:10 -06:00
|
|
|
// toProtocolIncomingCalls returns an array of protocol.CallHierarchyIncomingCall for ReferenceInfo's.
|
|
|
|
// References inside same enclosure are assigned to the same enclosing function.
|
|
|
|
func toProtocolIncomingCalls(ctx context.Context, snapshot Snapshot, refs []*ReferenceInfo) ([]protocol.CallHierarchyIncomingCall, error) {
|
|
|
|
// an enclosing node could have multiple calls to a reference, we only show the enclosure
|
|
|
|
// once in the result but highlight all calls using FromRanges (ranges at which the calls occur)
|
2020-08-12 10:34:53 -06:00
|
|
|
var incomingCalls = map[protocol.Location]*protocol.CallHierarchyIncomingCall{}
|
2020-08-07 15:11:10 -06:00
|
|
|
for _, ref := range refs {
|
|
|
|
refRange, err := ref.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
callItem, err := enclosingNodeCallItem(snapshot, ref.pkg, ref.URI(), ref.ident.NamePos)
|
2020-08-07 15:11:10 -06:00
|
|
|
if err != nil {
|
|
|
|
event.Error(ctx, "error getting enclosing node", err, tag.Method.Of(ref.Name))
|
|
|
|
continue
|
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
loc := protocol.Location{
|
|
|
|
URI: callItem.URI,
|
|
|
|
Range: callItem.Range,
|
|
|
|
}
|
2020-08-07 15:11:10 -06:00
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
if incomingCall, ok := incomingCalls[loc]; ok {
|
2020-08-07 15:11:10 -06:00
|
|
|
incomingCall.FromRanges = append(incomingCall.FromRanges, refRange)
|
|
|
|
continue
|
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
incomingCalls[loc] = &protocol.CallHierarchyIncomingCall{
|
|
|
|
From: callItem,
|
2020-08-07 15:11:10 -06:00
|
|
|
FromRanges: []protocol.Range{refRange},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
incomingCallItems := make([]protocol.CallHierarchyIncomingCall, 0, len(incomingCalls))
|
|
|
|
for _, callItem := range incomingCalls {
|
|
|
|
incomingCallItems = append(incomingCallItems, *callItem)
|
|
|
|
}
|
|
|
|
return incomingCallItems, nil
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
// enclosingNodeCallItem creates a CallHierarchyItem representing the function call at pos
|
|
|
|
func enclosingNodeCallItem(snapshot Snapshot, pkg Package, uri span.URI, pos token.Pos) (protocol.CallHierarchyItem, error) {
|
|
|
|
pgf, err := pkg.File(uri)
|
2020-08-07 15:11:10 -06:00
|
|
|
if err != nil {
|
2020-08-12 10:34:53 -06:00
|
|
|
return protocol.CallHierarchyItem{}, err
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var funcDecl *ast.FuncDecl
|
|
|
|
var funcLit *ast.FuncLit // innermost function literal
|
|
|
|
var litCount int
|
|
|
|
// Find the enclosing function, if any, and the number of func literals in between.
|
2020-08-12 10:34:53 -06:00
|
|
|
path, _ := astutil.PathEnclosingInterval(pgf.File, pos, pos)
|
2020-08-07 15:11:10 -06:00
|
|
|
outer:
|
|
|
|
for _, node := range path {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
funcDecl = n
|
|
|
|
break outer
|
|
|
|
case *ast.FuncLit:
|
|
|
|
litCount++
|
|
|
|
if litCount > 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
funcLit = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nameIdent := path[len(path)-1].(*ast.File).Name
|
2020-08-12 10:34:53 -06:00
|
|
|
kind := protocol.Package
|
2020-08-07 15:11:10 -06:00
|
|
|
if funcDecl != nil {
|
|
|
|
nameIdent = funcDecl.Name
|
2020-08-12 10:34:53 -06:00
|
|
|
kind = protocol.Function
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
nameStart, nameEnd := nameIdent.NamePos, nameIdent.NamePos+token.Pos(len(nameIdent.Name))
|
|
|
|
if funcLit != nil {
|
|
|
|
nameStart, nameEnd = funcLit.Type.Func, funcLit.Type.Params.Pos()
|
2020-08-12 10:34:53 -06:00
|
|
|
kind = protocol.Function
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
rng, err := newMappedRange(snapshot.FileSet(), pgf.Mapper, nameStart, nameEnd).Range()
|
2020-08-07 15:11:10 -06:00
|
|
|
if err != nil {
|
2020-08-12 10:34:53 -06:00
|
|
|
return protocol.CallHierarchyItem{}, err
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
name := nameIdent.Name
|
|
|
|
for i := 0; i < litCount; i++ {
|
|
|
|
name += ".func()"
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
return protocol.CallHierarchyItem{
|
|
|
|
Name: name,
|
|
|
|
Kind: kind,
|
|
|
|
Tags: []protocol.SymbolTag{},
|
|
|
|
Detail: fmt.Sprintf("%s • %s", pkg.PkgPath(), filepath.Base(uri.Filename())),
|
|
|
|
URI: protocol.DocumentURI(uri),
|
|
|
|
Range: rng,
|
|
|
|
SelectionRange: rng,
|
|
|
|
}, nil
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
|
|
|
|
2020-08-12 10:34:53 -06:00
|
|
|
// OutgoingCalls returns an array of CallHierarchyOutgoingCall for a file and the position within the file.
|
|
|
|
func OutgoingCalls(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protocol.Position) ([]protocol.CallHierarchyOutgoingCall, error) {
|
|
|
|
ctx, done := event.Start(ctx, "source.OutgoingCalls")
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
identifier, err := Identifier(ctx, snapshot, fh, pos)
|
2020-08-07 15:11:10 -06:00
|
|
|
if err != nil {
|
2020-08-12 10:34:53 -06:00
|
|
|
if errors.Is(err, ErrNoIdentFound) || errors.Is(err, errNoObjectFound) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
|
|
|
|
if _, ok := identifier.Declaration.obj.Type().Underlying().(*types.Signature); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(identifier.Declaration.MappedRange) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
declMappedRange := identifier.Declaration.MappedRange[0]
|
|
|
|
callExprs, err := collectCallExpressions(snapshot.FileSet(), declMappedRange.m, identifier.Declaration.node)
|
2020-08-07 15:11:10 -06:00
|
|
|
if err != nil {
|
2020-08-12 10:34:53 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toProtocolOutgoingCalls(ctx, snapshot, fh, callExprs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectCallExpressions collects call expression ranges inside a function.
|
|
|
|
func collectCallExpressions(fset *token.FileSet, mapper *protocol.ColumnMapper, node ast.Node) ([]protocol.Range, error) {
|
|
|
|
type callPos struct {
|
|
|
|
start, end token.Pos
|
|
|
|
}
|
|
|
|
callPositions := []callPos{}
|
|
|
|
|
|
|
|
ast.Inspect(node, func(n ast.Node) bool {
|
|
|
|
if call, ok := n.(*ast.CallExpr); ok {
|
|
|
|
var start, end token.Pos
|
|
|
|
switch n := call.Fun.(type) {
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
start, end = n.Sel.NamePos, call.Lparen
|
|
|
|
case *ast.Ident:
|
|
|
|
start, end = n.NamePos, call.Lparen
|
|
|
|
default:
|
|
|
|
// ignore any other kind of call expressions
|
|
|
|
// for ex: direct function literal calls since that's not an 'outgoing' call
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
callPositions = append(callPositions, callPos{start: start, end: end})
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
callRanges := []protocol.Range{}
|
|
|
|
for _, call := range callPositions {
|
|
|
|
callRange, err := newMappedRange(fset, mapper, call.start, call.end).Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
callRanges = append(callRanges, callRange)
|
|
|
|
}
|
|
|
|
return callRanges, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// toProtocolOutgoingCalls returns an array of protocol.CallHierarchyOutgoingCall for ast call expressions.
|
|
|
|
// Calls to the same function are assigned to the same declaration.
|
|
|
|
func toProtocolOutgoingCalls(ctx context.Context, snapshot Snapshot, fh FileHandle, callRanges []protocol.Range) ([]protocol.CallHierarchyOutgoingCall, error) {
|
|
|
|
// multiple calls could be made to the same function
|
|
|
|
var outgoingCalls = map[ast.Node]*protocol.CallHierarchyOutgoingCall{}
|
|
|
|
for _, callRange := range callRanges {
|
|
|
|
identifier, err := Identifier(ctx, snapshot, fh, callRange.Start)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrNoIdentFound) || errors.Is(err, errNoObjectFound) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore calls to builtin functions
|
|
|
|
if identifier.Declaration.obj.Pkg() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if outgoingCall, ok := outgoingCalls[identifier.Declaration.node]; ok {
|
|
|
|
outgoingCall.FromRanges = append(outgoingCall.FromRanges, callRange)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(identifier.Declaration.MappedRange) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
declMappedRange := identifier.Declaration.MappedRange[0]
|
|
|
|
rng, err := declMappedRange.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outgoingCalls[identifier.Declaration.node] = &protocol.CallHierarchyOutgoingCall{
|
|
|
|
To: protocol.CallHierarchyItem{
|
|
|
|
Name: identifier.Name,
|
|
|
|
Kind: protocol.Function,
|
|
|
|
Tags: []protocol.SymbolTag{},
|
|
|
|
Detail: fmt.Sprintf("%s • %s", identifier.Declaration.obj.Pkg().Path(), filepath.Base(declMappedRange.URI().Filename())),
|
|
|
|
URI: protocol.DocumentURI(declMappedRange.URI()),
|
|
|
|
Range: rng,
|
|
|
|
SelectionRange: rng,
|
|
|
|
},
|
|
|
|
FromRanges: []protocol.Range{callRange},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outgoingCallItems := make([]protocol.CallHierarchyOutgoingCall, 0, len(outgoingCalls))
|
|
|
|
for _, callItem := range outgoingCalls {
|
|
|
|
outgoingCallItems = append(outgoingCallItems, *callItem)
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|
2020-08-12 10:34:53 -06:00
|
|
|
return outgoingCallItems, nil
|
2020-08-07 15:11:10 -06:00
|
|
|
}
|