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 // highlight implements the highlight verb for gopls.
19 type highlight struct {
23 func (r *highlight) Name() string { return "highlight" }
24 func (r *highlight) Usage() string { return "<position>" }
25 func (r *highlight) ShortHelp() string { return "display selected identifier's highlights" }
26 func (r *highlight) DetailedHelp(f *flag.FlagSet) {
27 fmt.Fprint(f.Output(), `
30 $ # 1-indexed location (:line:column or :#offset) of the target identifier
31 $ gopls highlight helper/helper.go:8:6
32 $ gopls highlight helper/helper.go:#53
34 gopls highlight flags are:
39 func (r *highlight) Run(ctx context.Context, args ...string) error {
41 return tool.CommandLineErrorf("highlight expects 1 argument (position)")
44 conn, err := r.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.DocumentHighlightParams{
62 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
63 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
64 Position: loc.Range.Start,
67 highlights, err := conn.DocumentHighlight(ctx, &p)
72 var results []span.Span
73 for _, h := range highlights {
74 l := protocol.Location{Range: h.Range}
75 s, err := file.mapper.Span(l)
79 results = append(results, s)
81 // Sort results to make tests deterministic since DocumentHighlight uses a map.
82 sort.SliceStable(results, func(i, j int) bool {
83 return span.Compare(results[i], results[j]) == -1
86 for _, s := range results {