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
37 func (r *highlight) Run(ctx context.Context, args ...string) error {
39 return tool.CommandLineErrorf("highlight expects 1 argument (position)")
42 conn, err := r.app.connect(ctx)
46 defer conn.terminate(ctx)
48 from := span.Parse(args[0])
49 file := conn.AddFile(ctx, from.URI())
54 loc, err := file.mapper.Location(from)
59 p := protocol.DocumentHighlightParams{
60 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
61 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
62 Position: loc.Range.Start,
65 highlights, err := conn.DocumentHighlight(ctx, &p)
70 var results []span.Span
71 for _, h := range highlights {
72 l := protocol.Location{Range: h.Range}
73 s, err := file.mapper.Span(l)
77 results = append(results, s)
79 // Sort results to make tests deterministic since DocumentHighlight uses a map.
80 sort.SliceStable(results, func(i, j int) bool {
81 return span.Compare(results[i], results[j]) == -1
84 for _, s := range results {