1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/tools/internal/lsp/protocol"
14 "golang.org/x/tools/internal/span"
15 "golang.org/x/tools/internal/tool"
18 // callHierarchy implements the callHierarchy verb for gopls.
19 type callHierarchy struct {
23 func (c *callHierarchy) Name() string { return "call_hierarchy" }
24 func (c *callHierarchy) Usage() string { return "<position>" }
25 func (c *callHierarchy) ShortHelp() string { return "display selected identifier's call hierarchy" }
26 func (c *callHierarchy) DetailedHelp(f *flag.FlagSet) {
27 fmt.Fprint(f.Output(), `
30 $ # 1-indexed location (:line:column or :#offset) of the target identifier
31 $ gopls call_hierarchy helper/helper.go:8:6
32 $ gopls call_hierarchy helper/helper.go:#53
37 func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
39 return tool.CommandLineErrorf("call_hierarchy expects 1 argument (position)")
42 conn, err := c.app.connect(ctx)
46 defer conn.terminate(ctx)
48 from := span.Parse(args[0])
49 file := conn.AddFile(ctx, from.URI())
54 loc, err := file.mapper.Location(from)
59 p := protocol.CallHierarchyPrepareParams{
60 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
61 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
62 Position: loc.Range.Start,
66 callItems, err := conn.PrepareCallHierarchy(ctx, &p)
70 if len(callItems) == 0 {
71 return fmt.Errorf("function declaration identifier not found at %v", args[0])
74 for _, item := range callItems {
75 incomingCalls, err := conn.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item})
79 for i, call := range incomingCalls {
80 // From the spec: CallHierarchyIncomingCall.FromRanges is relative to
81 // the caller denoted by CallHierarchyIncomingCall.from.
82 printString, err := callItemPrintString(ctx, conn, call.From, call.From.URI, call.FromRanges)
86 fmt.Printf("caller[%d]: %s\n", i, printString)
89 printString, err := callItemPrintString(ctx, conn, item, "", nil)
93 fmt.Printf("identifier: %s\n", printString)
95 outgoingCalls, err := conn.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item})
99 for i, call := range outgoingCalls {
100 // From the spec: CallHierarchyOutgoingCall.FromRanges is the range
101 // relative to the caller, e.g the item passed to
102 printString, err := callItemPrintString(ctx, conn, call.To, item.URI, call.FromRanges)
106 fmt.Printf("callee[%d]: %s\n", i, printString)
113 // callItemPrintString returns a protocol.CallHierarchyItem object represented as a string.
114 // item and call ranges (protocol.Range) are converted to user friendly spans (1-indexed).
115 func callItemPrintString(ctx context.Context, conn *connection, item protocol.CallHierarchyItem, callsURI protocol.DocumentURI, calls []protocol.Range) (string, error) {
116 itemFile := conn.AddFile(ctx, item.URI.SpanURI())
117 if itemFile.err != nil {
118 return "", itemFile.err
120 itemSpan, err := itemFile.mapper.Span(protocol.Location{URI: item.URI, Range: item.Range})
125 callsFile := conn.AddFile(ctx, callsURI.SpanURI())
126 if callsURI != "" && callsFile.err != nil {
127 return "", callsFile.err
129 var callRanges []string
130 for _, rng := range calls {
131 callSpan, err := callsFile.mapper.Span(protocol.Location{URI: item.URI, Range: rng})
136 spn := fmt.Sprint(callSpan)
137 callRanges = append(callRanges, fmt.Sprint(spn[strings.Index(spn, ":")+1:]))
140 printString := fmt.Sprintf("function %s in %v", item.Name, itemSpan)
142 printString = fmt.Sprintf("ranges %s in %s from/to %s", strings.Join(callRanges, ", "), callsURI.SpanURI().Filename(), printString)
144 return printString, nil