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.
15 "golang.org/x/tools/internal/lsp/protocol"
16 "golang.org/x/tools/internal/lsp/source"
17 "golang.org/x/tools/internal/span"
18 "golang.org/x/tools/internal/tool"
19 errors "golang.org/x/xerrors"
22 // A Definition is the result of a 'definition' query.
23 type Definition struct {
24 Span span.Span `json:"span"` // span of the definition
25 Description string `json:"description"` // description of the denoted object
28 // These constant is printed in the help, and then used in a test to verify the
29 // help is still valid.
30 // They refer to "Set" in "flag.FlagSet" from the DetailedHelp method below.
37 // definition implements the definition verb for gopls.
38 type definition struct {
41 JSON bool `flag:"json" help:"emit output in JSON format"`
42 MarkdownSupported bool `flag:"markdown" help:"support markdown in responses"`
45 func (d *definition) Name() string { return "definition" }
46 func (d *definition) Usage() string { return "<position>" }
47 func (d *definition) ShortHelp() string { return "show declaration of selected identifier" }
48 func (d *definition) DetailedHelp(f *flag.FlagSet) {
49 fmt.Fprintf(f.Output(), `
50 Example: show the definition of the identifier at syntax at offset %[1]v in this file (flag.FlagSet):
52 $ gopls definition internal/lsp/cmd/definition.go:%[1]v:%[2]v
53 $ gopls definition internal/lsp/cmd/definition.go:#%[3]v
55 gopls query definition flags are:
56 `, exampleLine, exampleColumn, exampleOffset)
60 // Run performs the definition query as specified by args and prints the
62 func (d *definition) Run(ctx context.Context, args ...string) error {
64 return tool.CommandLineErrorf("definition expects 1 argument")
66 // Plaintext makes more sense for the command line.
68 d.app.options = func(o *source.Options) {
72 o.PreferredContentFormat = protocol.PlainText
73 if d.MarkdownSupported {
74 o.PreferredContentFormat = protocol.Markdown
77 conn, err := d.app.connect(ctx)
81 defer conn.terminate(ctx)
82 from := span.Parse(args[0])
83 file := conn.AddFile(ctx, from.URI())
87 loc, err := file.mapper.Location(from)
91 tdpp := protocol.TextDocumentPositionParams{
92 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
93 Position: loc.Range.Start,
95 p := protocol.DefinitionParams{
96 TextDocumentPositionParams: tdpp,
98 locs, err := conn.Definition(ctx, &p)
100 return errors.Errorf("%v: %v", from, err)
104 return errors.Errorf("%v: not an identifier", from)
106 q := protocol.HoverParams{
107 TextDocumentPositionParams: tdpp,
109 hover, err := conn.Hover(ctx, &q)
111 return errors.Errorf("%v: %v", from, err)
114 return errors.Errorf("%v: not an identifier", from)
116 file = conn.AddFile(ctx, fileURI(locs[0].URI))
118 return errors.Errorf("%v: %v", from, file.err)
120 definition, err := file.mapper.Span(locs[0])
122 return errors.Errorf("%v: %v", from, err)
124 description := strings.TrimSpace(hover.Contents.Value)
125 result := &Definition{
127 Description: description,
130 enc := json.NewEncoder(os.Stdout)
131 enc.SetIndent("", "\t")
132 return enc.Encode(result)
134 fmt.Printf("%v: defined here as %s", result.Span, result.Description)