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
5package cmd
6
7import (
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
19type implementation struct {
20	app *Application
21}
22
23func (i *implementation) Name() string      { return "implementation" }
24func (i *implementation) Usage() string     { return "<position>" }
25func (i *implementation) ShortHelp() string { return "display selected identifier's implementation" }
26func (i *implementation) DetailedHelp(f *flag.FlagSet) {
27	fmt.Fprint(f.Output(), `
28Example:
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	f.PrintDefaults()
35}
36
37func (i *implementation) Run(ctx context.Context, args ...string) error {
38	if len(args) != 1 {
39		return tool.CommandLineErrorf("implementation expects 1 argument (position)")
40	}
41
42	conn, err := i.app.connect(ctx)
43	if err != nil {
44		return err
45	}
46	defer conn.terminate(ctx)
47
48	from := span.Parse(args[0])
49	file := conn.AddFile(ctx, from.URI())
50	if file.err != nil {
51		return file.err
52	}
53
54	loc, err := file.mapper.Location(from)
55	if err != nil {
56		return err
57	}
58
59	p := protocol.ImplementationParams{
60		TextDocumentPositionParams: protocol.TextDocumentPositionParams{
61			TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
62			Position:     loc.Range.Start,
63		},
64	}
65
66	implementations, err := conn.Implementation(ctx, &p)
67	if err != nil {
68		return err
69	}
70
71	var spans []string
72	for _, impl := range implementations {
73		f := conn.AddFile(ctx, fileURI(impl.URI))
74		span, err := f.mapper.Span(impl)
75		if err != nil {
76			return err
77		}
78		spans = append(spans, fmt.Sprint(span))
79	}
80	sort.Strings(spans)
81
82	for _, s := range spans {
83		fmt.Println(s)
84	}
85
86	return nil
87}
88