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.
13 "golang.org/x/tools/internal/lsp/protocol"
14 "golang.org/x/tools/internal/span"
15 "golang.org/x/tools/internal/tool"
18 // implementation implements the implementation verb for gopls
19 type implementation struct {
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(), `
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
37 func (i *implementation) Run(ctx context.Context, args ...string) error {
39 return tool.CommandLineErrorf("implementation expects 1 argument (position)")
42 conn, err := i.app.connect(ctx)
46 defer conn.terminate(ctx)
48 from := span.Parse(args[0])
49 file := conn.AddFile(ctx, from.URI())
54 loc, err := file.mapper.Location(from)
59 p := protocol.ImplementationParams{
60 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
61 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
62 Position: loc.Range.Start,
66 implementations, err := conn.Implementation(ctx, &p)
72 for _, impl := range implementations {
73 f := conn.AddFile(ctx, fileURI(impl.URI))
74 span, err := f.mapper.Span(impl)
78 spans = append(spans, fmt.Sprint(span))
82 for _, s := range spans {