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
34 gopls call_hierarchy flags are:
39 func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
41 return tool.CommandLineErrorf("call_hierarchy expects 1 argument (position)")
44 conn, err := c.app.connect(ctx)
48 defer conn.terminate(ctx)
50 from := span.Parse(args[0])
51 file := conn.AddFile(ctx, from.URI())
56 loc, err := file.mapper.Location(from)
61 p := protocol.CallHierarchyPrepareParams{
62 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
63 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
64 Position: loc.Range.Start,
68 callItems, err := conn.PrepareCallHierarchy(ctx, &p)
72 if len(callItems) == 0 {
73 return fmt.Errorf("function declaration identifier not found at %v", args[0])
76 for _, item := range callItems {
77 incomingCalls, err := conn.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item})
81 for i, call := range incomingCalls {
82 // From the spec: CallHierarchyIncomingCall.FromRanges is relative to
83 // the caller denoted by CallHierarchyIncomingCall.from.
84 printString, err := callItemPrintString(ctx, conn, call.From, call.From.URI, call.FromRanges)
88 fmt.Printf("caller[%d]: %s\n", i, printString)
91 printString, err := callItemPrintString(ctx, conn, item, "", nil)
95 fmt.Printf("identifier: %s\n", printString)
97 outgoingCalls, err := conn.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item})
101 for i, call := range outgoingCalls {
102 // From the spec: CallHierarchyOutgoingCall.FromRanges is the range
103 // relative to the caller, e.g the item passed to
104 printString, err := callItemPrintString(ctx, conn, call.To, item.URI, call.FromRanges)
108 fmt.Printf("callee[%d]: %s\n", i, printString)
115 // callItemPrintString returns a protocol.CallHierarchyItem object represented as a string.
116 // item and call ranges (protocol.Range) are converted to user friendly spans (1-indexed).
117 func callItemPrintString(ctx context.Context, conn *connection, item protocol.CallHierarchyItem, callsURI protocol.DocumentURI, calls []protocol.Range) (string, error) {
118 itemFile := conn.AddFile(ctx, item.URI.SpanURI())
119 if itemFile.err != nil {
120 return "", itemFile.err
122 itemSpan, err := itemFile.mapper.Span(protocol.Location{URI: item.URI, Range: item.Range})
127 callsFile := conn.AddFile(ctx, callsURI.SpanURI())
128 if callsURI != "" && callsFile.err != nil {
129 return "", callsFile.err
131 var callRanges []string
132 for _, rng := range calls {
133 callSpan, err := callsFile.mapper.Span(protocol.Location{URI: item.URI, Range: rng})
138 spn := fmt.Sprint(callSpan)
139 callRanges = append(callRanges, fmt.Sprint(spn[strings.Index(spn, ":")+1:]))
142 printString := fmt.Sprintf("function %s in %v", item.Name, itemSpan)
144 printString = fmt.Sprintf("ranges %s in %s from/to %s", strings.Join(callRanges, ", "), callsURI.SpanURI().Filename(), printString)
146 return printString, nil