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.
4
5package cmd
6
7import (
8	"context"
9	"flag"
10	"fmt"
11	"strings"
12
13	"golang.org/x/tools/internal/lsp/protocol"
14	"golang.org/x/tools/internal/span"
15	"golang.org/x/tools/internal/tool"
16)
17
18// callHierarchy implements the callHierarchy verb for gopls.
19type callHierarchy struct {
20	app *Application
21}
22
23func (c *callHierarchy) Name() string      { return "call_hierarchy" }
24func (c *callHierarchy) Usage() string     { return "<position>" }
25func (c *callHierarchy) ShortHelp() string { return "display selected identifier's call hierarchy" }
26func (c *callHierarchy) DetailedHelp(f *flag.FlagSet) {
27	fmt.Fprint(f.Output(), `
28Example:
29
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
33`)
34	f.PrintDefaults()
35}
36
37func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
38	if len(args) != 1 {
39		return tool.CommandLineErrorf("call_hierarchy expects 1 argument (position)")
40	}
41
42	conn, err := c.app.connect(ctx)
43	if err != nil {
44		return err
45	}
46	defer conn.terminate(ctx)
47
48	from := span.Parse(args[0])
49	file := conn.AddFile(ctx, from.URI())
50	if file.err != nil {
51		return file.err
52	}
53
54	loc, err := file.mapper.Location(from)
55	if err != nil {
56		return err
57	}
58
59	p := protocol.CallHierarchyPrepareParams{
60		TextDocumentPositionParams: protocol.TextDocumentPositionParams{
61			TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
62			Position:     loc.Range.Start,
63		},
64	}
65
66	callItems, err := conn.PrepareCallHierarchy(ctx, &p)
67	if err != nil {
68		return err
69	}
70	if len(callItems) == 0 {
71		return fmt.Errorf("function declaration identifier not found at %v", args[0])
72	}
73
74	for _, item := range callItems {
75		incomingCalls, err := conn.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item})
76		if err != nil {
77			return err
78		}
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)
83			if err != nil {
84				return err
85			}
86			fmt.Printf("caller[%d]: %s\n", i, printString)
87		}
88
89		printString, err := callItemPrintString(ctx, conn, item, "", nil)
90		if err != nil {
91			return err
92		}
93		fmt.Printf("identifier: %s\n", printString)
94
95		outgoingCalls, err := conn.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item})
96		if err != nil {
97			return err
98		}
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)
103			if err != nil {
104				return err
105			}
106			fmt.Printf("callee[%d]: %s\n", i, printString)
107		}
108	}
109
110	return nil
111}
112
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).
115func 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
119	}
120	itemSpan, err := itemFile.mapper.Span(protocol.Location{URI: item.URI, Range: item.Range})
121	if err != nil {
122		return "", err
123	}
124
125	callsFile := conn.AddFile(ctx, callsURI.SpanURI())
126	if callsURI != "" && callsFile.err != nil {
127		return "", callsFile.err
128	}
129	var callRanges []string
130	for _, rng := range calls {
131		callSpan, err := callsFile.mapper.Span(protocol.Location{URI: item.URI, Range: rng})
132		if err != nil {
133			return "", err
134		}
135
136		spn := fmt.Sprint(callSpan)
137		callRanges = append(callRanges, fmt.Sprint(spn[strings.Index(spn, ":")+1:]))
138	}
139
140	printString := fmt.Sprintf("function %s in %v", item.Name, itemSpan)
141	if len(calls) > 0 {
142		printString = fmt.Sprintf("ranges %s in %s from/to %s", strings.Join(callRanges, ", "), callsURI.SpanURI().Filename(), printString)
143	}
144	return printString, nil
145}
146