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.
14 "golang.org/x/tools/internal/lsp/protocol"
15 "golang.org/x/tools/internal/span"
16 "golang.org/x/tools/internal/tool"
17 errors "golang.org/x/xerrors"
20 // links implements the links verb for gopls.
22 JSON bool `flag:"json" help:"emit document links in JSON format"`
27 func (l *links) Name() string { return "links" }
28 func (l *links) Usage() string { return "<filename>" }
29 func (l *links) ShortHelp() string { return "list links in a file" }
30 func (l *links) DetailedHelp(f *flag.FlagSet) {
31 fmt.Fprintf(f.Output(), `
32 Example: list links contained within a file:
34 $ gopls links internal/lsp/cmd/check.go
36 gopls links flags are:
41 // Run finds all the links within a document
42 // - if -json is specified, outputs location range and uri
43 // - otherwise, prints the a list of unique links
44 func (l *links) Run(ctx context.Context, args ...string) error {
46 return tool.CommandLineErrorf("links expects 1 argument")
48 conn, err := l.app.connect(ctx)
52 defer conn.terminate(ctx)
54 from := span.Parse(args[0])
56 file := conn.AddFile(ctx, uri)
60 results, err := conn.DocumentLink(ctx, &protocol.DocumentLinkParams{
61 TextDocument: protocol.TextDocumentIdentifier{
62 URI: protocol.URIFromSpanURI(uri),
66 return errors.Errorf("%v: %v", from, err)
69 enc := json.NewEncoder(os.Stdout)
70 enc.SetIndent("", "\t")
71 return enc.Encode(results)
73 for _, v := range results {