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 / implementation.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 // implementation implements the implementation verb for gopls
19 type implementation struct {
20         app *Application
21 }
22
23 func (i *implementation) Name() string      { return "implementation" }
24 func (i *implementation) Usage() string     { return "<position>" }
25 func (i *implementation) ShortHelp() string { return "display selected identifier's implementation" }
26 func (i *implementation) 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 implementation helper/helper.go:8:6
32   $ gopls implementation helper/helper.go:#53
33
34   gopls implementation flags are:
35 `)
36         f.PrintDefaults()
37 }
38
39 func (i *implementation) Run(ctx context.Context, args ...string) error {
40         if len(args) != 1 {
41                 return tool.CommandLineErrorf("implementation expects 1 argument (position)")
42         }
43
44         conn, err := i.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.ImplementationParams{
62                 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
63                         TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
64                         Position:     loc.Range.Start,
65                 },
66         }
67
68         implementations, err := conn.Implementation(ctx, &p)
69         if err != nil {
70                 return err
71         }
72
73         var spans []string
74         for _, impl := range implementations {
75                 f := conn.AddFile(ctx, fileURI(impl.URI))
76                 span, err := f.mapper.Span(impl)
77                 if err != nil {
78                         return err
79                 }
80                 spans = append(spans, fmt.Sprint(span))
81         }
82         sort.Strings(spans)
83
84         for _, s := range spans {
85                 fmt.Println(s)
86         }
87
88         return nil
89 }