|
| 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 | + |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "flag" |
| 10 | + "fmt" |
| 11 | + "golang.org/x/tools/internal/lsp/protocol" |
| 12 | + "golang.org/x/tools/internal/span" |
| 13 | + "golang.org/x/tools/internal/tool" |
| 14 | + "sort" |
| 15 | +) |
| 16 | + |
| 17 | +// references implements the references verb for gopls |
| 18 | +type references struct { |
| 19 | + IncludeDeclaration bool `flag:"d" help:"include the declaration of the specified identifier in the results"` |
| 20 | + |
| 21 | + app *Application |
| 22 | +} |
| 23 | + |
| 24 | +func (r *references) Name() string { return "references" } |
| 25 | +func (r *references) Usage() string { return "<position>" } |
| 26 | +func (r *references) ShortHelp() string { return "display selected identifier's references" } |
| 27 | +func (r *references) DetailedHelp(f *flag.FlagSet) { |
| 28 | + fmt.Fprint(f.Output(), ` |
| 29 | +Example: |
| 30 | +
|
| 31 | + $ # 1-indexed location (:line:column or :#offset) of the target identifier |
| 32 | + $ gopls references helper/helper.go:8:6 |
| 33 | + $ gopls references helper/helper.go:#53 |
| 34 | +
|
| 35 | + gopls references flags are: |
| 36 | +`) |
| 37 | + f.PrintDefaults() |
| 38 | +} |
| 39 | + |
| 40 | +func (r *references) Run(ctx context.Context, args ...string) error { |
| 41 | + if len(args) != 1 { |
| 42 | + return tool.CommandLineErrorf("references expects 1 argument (position)") |
| 43 | + } |
| 44 | + |
| 45 | + conn, err := r.app.connect(ctx) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + defer conn.terminate(ctx) |
| 50 | + |
| 51 | + from := span.Parse(args[0]) |
| 52 | + file := conn.AddFile(ctx, from.URI()) |
| 53 | + if file.err != nil { |
| 54 | + return file.err |
| 55 | + } |
| 56 | + |
| 57 | + loc, err := file.mapper.Location(from) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + p := protocol.ReferenceParams{ |
| 63 | + Context: protocol.ReferenceContext{ |
| 64 | + IncludeDeclaration: r.IncludeDeclaration, |
| 65 | + }, |
| 66 | + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ |
| 67 | + TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI}, |
| 68 | + Position: loc.Range.Start, |
| 69 | + }, |
| 70 | + } |
| 71 | + locations, err := conn.References(ctx, &p) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + if len(locations) == 0 { |
| 77 | + return tool.CommandLineErrorf("%v: not an identifier", from) |
| 78 | + } |
| 79 | + |
| 80 | + var spans []string |
| 81 | + for _, l := range locations { |
| 82 | + f := conn.AddFile(ctx, span.NewURI(l.URI)) |
| 83 | + // convert location to span for user-friendly 1-indexed line |
| 84 | + // and column numbers |
| 85 | + span, err := f.mapper.Span(l) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + spans = append(spans, fmt.Sprint(span)) |
| 90 | + } |
| 91 | + |
| 92 | + sort.Strings(spans) |
| 93 | + for _, s := range spans { |
| 94 | + fmt.Println(s) |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments