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
34 gopls implementation flags are:
39 func (i *implementation) Run(ctx context.Context, args ...string) error {
41 return tool.CommandLineErrorf("implementation expects 1 argument (position)")
44 conn, err := i.app.connect(ctx)
48 defer conn.terminate(ctx)
50 from := span.Parse(args[0])
51 file := conn.AddFile(ctx, from.URI())
56 loc, err := file.mapper.Location(from)
61 p := protocol.ImplementationParams{
62 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
63 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
64 Position: loc.Range.Start,
68 implementations, err := conn.Implementation(ctx, &p)
74 for _, impl := range implementations {
75 f := conn.AddFile(ctx, fileURI(impl.URI))
76 span, err := f.mapper.Span(impl)
80 spans = append(spans, fmt.Sprint(span))
84 for _, s := range spans {