1
0
mirror of https://github.com/golang/go synced 2024-11-05 17:36:15 -07:00
go/internal/lsp/source/call_hierarchy.go
Danish Dua a5d4502270 internal: add call hierarchy cmd and lsp scaffolding
* adds gopls command line tool for call hierarchy
* adds lsp setup for call hierarchy
* adds handler for textDocument/prepareCallHierarchy to display selected
  identifier and get incoming/outgoing calls for it
* setup testing

Change-Id: I0a0904abdbe11273a56162b6e5be93b97ceb9c26
Reviewed-on: https://go-review.googlesource.com/c/tools/+/246521
Run-TryBot: Danish Dua <danishdua@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-08-07 20:22:52 +00:00

69 lines
2.3 KiB
Go

// 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"
"go/ast"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/lsp/debug/tag"
"golang.org/x/tools/internal/lsp/protocol"
errors "golang.org/x/xerrors"
)
// PrepareCallHierarchy returns an array of CallHierarchyItem for a file and the position within the file
func PrepareCallHierarchy(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protocol.Position) ([]protocol.CallHierarchyItem, error) {
ctx, done := event.Start(ctx, "source.prepareCallHierarchy")
defer done()
identifier, err := Identifier(ctx, snapshot, fh, pos)
if err != nil {
if errors.Is(err, ErrNoIdentFound) {
event.Log(ctx, err.Error(), tag.Position.Of(pos))
} else {
event.Error(ctx, "error getting identifier", err, tag.Position.Of(pos))
}
return nil, nil
}
// if identifier is not of type function
_, ok := identifier.Declaration.node.(*ast.FuncDecl)
if !ok {
event.Log(ctx, "invalid identifier type, expected funtion declaration", tag.Position.Of(pos))
return nil, nil
}
rng, err := identifier.Range()
if err != nil {
return nil, err
}
callHierarchyItem := protocol.CallHierarchyItem{
Name: identifier.Name,
Kind: protocol.Function,
Tags: []protocol.SymbolTag{},
Detail: "func()",
URI: protocol.DocumentURI(fh.URI()),
Range: rng,
SelectionRange: rng,
}
return []protocol.CallHierarchyItem{callHierarchyItem}, nil
}
// IncomingCalls returns an array of CallHierarchyIncomingCall for a file and the position within the file
func IncomingCalls(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protocol.Position) ([]protocol.CallHierarchyIncomingCall, error) {
ctx, done := event.Start(ctx, "source.incomingCalls")
defer done()
return []protocol.CallHierarchyIncomingCall{}, nil
}
// 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()
return []protocol.CallHierarchyOutgoingCall{}, nil
}