Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / internal / lsp / cmd / references.go
1 // Copyright 2019 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         "sort"
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 // references implements the references verb for gopls
19 type references struct {
20         IncludeDeclaration bool `flag:"d" help:"include the declaration of the specified identifier in the results"`
21
22         app *Application
23 }
24
25 func (r *references) Name() string      { return "references" }
26 func (r *references) Usage() string     { return "<position>" }
27 func (r *references) ShortHelp() string { return "display selected identifier's references" }
28 func (r *references) DetailedHelp(f *flag.FlagSet) {
29         fmt.Fprint(f.Output(), `
30 Example:
31
32   $ # 1-indexed location (:line:column or :#offset) of the target identifier
33   $ gopls references helper/helper.go:8:6
34   $ gopls references helper/helper.go:#53
35
36   gopls references flags are:
37 `)
38         f.PrintDefaults()
39 }
40
41 func (r *references) Run(ctx context.Context, args ...string) error {
42         if len(args) != 1 {
43                 return tool.CommandLineErrorf("references expects 1 argument (position)")
44         }
45
46         conn, err := r.app.connect(ctx)
47         if err != nil {
48                 return err
49         }
50         defer conn.terminate(ctx)
51
52         from := span.Parse(args[0])
53         file := conn.AddFile(ctx, from.URI())
54         if file.err != nil {
55                 return file.err
56         }
57         loc, err := file.mapper.Location(from)
58         if err != nil {
59                 return err
60         }
61         p := protocol.ReferenceParams{
62                 Context: protocol.ReferenceContext{
63                         IncludeDeclaration: r.IncludeDeclaration,
64                 },
65                 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
66                         TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
67                         Position:     loc.Range.Start,
68                 },
69         }
70         locations, err := conn.References(ctx, &p)
71         if err != nil {
72                 return err
73         }
74         var spans []string
75         for _, l := range locations {
76                 f := conn.AddFile(ctx, fileURI(l.URI))
77                 // convert location to span for user-friendly 1-indexed line
78                 // and column numbers
79                 span, err := f.mapper.Span(l)
80                 if err != nil {
81                         return err
82                 }
83                 spans = append(spans, fmt.Sprint(span))
84         }
85
86         sort.Strings(spans)
87         for _, s := range spans {
88                 fmt.Println(s)
89         }
90         return nil
91 }