Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / internal / lsp / cmd / call_hierarchy.go
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
5 package cmd
6
7 import (
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.
19 type callHierarchy struct {
20         app *Application
21 }
22
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(), `
28 Example:
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   gopls call_hierarchy flags are:
35 `)
36         f.PrintDefaults()
37 }
38
39 func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
40         if len(args) != 1 {
41                 return tool.CommandLineErrorf("call_hierarchy expects 1 argument (position)")
42         }
43
44         conn, err := c.app.connect(ctx)
45         if err != nil {
46                 return err
47         }
48         defer conn.terminate(ctx)
49
50         from := span.Parse(args[0])
51         file := conn.AddFile(ctx, from.URI())
52         if file.err != nil {
53                 return file.err
54         }
55
56         loc, err := file.mapper.Location(from)
57         if err != nil {
58                 return err
59         }
60
61         p := protocol.CallHierarchyPrepareParams{
62                 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
63                         TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
64                         Position:     loc.Range.Start,
65                 },
66         }
67
68         callItems, err := conn.PrepareCallHierarchy(ctx, &p)
69         if err != nil {
70                 return err
71         }
72         if len(callItems) == 0 {
73                 return fmt.Errorf("function declaration identifier not found at %v", args[0])
74         }
75
76         for _, item := range callItems {
77                 incomingCalls, err := conn.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item})
78                 if err != nil {
79                         return err
80                 }
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)
85                         if err != nil {
86                                 return err
87                         }
88                         fmt.Printf("caller[%d]: %s\n", i, printString)
89                 }
90
91                 printString, err := callItemPrintString(ctx, conn, item, "", nil)
92                 if err != nil {
93                         return err
94                 }
95                 fmt.Printf("identifier: %s\n", printString)
96
97                 outgoingCalls, err := conn.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item})
98                 if err != nil {
99                         return err
100                 }
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)
105                         if err != nil {
106                                 return err
107                         }
108                         fmt.Printf("callee[%d]: %s\n", i, printString)
109                 }
110         }
111
112         return nil
113 }
114
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
121         }
122         itemSpan, err := itemFile.mapper.Span(protocol.Location{URI: item.URI, Range: item.Range})
123         if err != nil {
124                 return "", err
125         }
126
127         callsFile := conn.AddFile(ctx, callsURI.SpanURI())
128         if callsURI != "" && callsFile.err != nil {
129                 return "", callsFile.err
130         }
131         var callRanges []string
132         for _, rng := range calls {
133                 callSpan, err := callsFile.mapper.Span(protocol.Location{URI: item.URI, Range: rng})
134                 if err != nil {
135                         return "", err
136                 }
137
138                 spn := fmt.Sprint(callSpan)
139                 callRanges = append(callRanges, fmt.Sprint(spn[strings.Index(spn, ":")+1:]))
140         }
141
142         printString := fmt.Sprintf("function %s in %v", item.Name, itemSpan)
143         if len(calls) > 0 {
144                 printString = fmt.Sprintf("ranges %s in %s from/to %s", strings.Join(callRanges, ", "), callsURI.SpanURI().Filename(), printString)
145         }
146         return printString, nil
147 }