.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / internal / lsp / cmd / highlight.go
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         "sort"
12
13         "golang.org/x/tools/internal/lsp/protocol"
14         "golang.org/x/tools/internal/span"
15         "golang.org/x/tools/internal/tool"
16 )
17
18 // highlight implements the highlight verb for gopls.
19 type highlight struct {
20         app *Application
21 }
22
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(), `
28 Example:
29
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
33 `)
34         f.PrintDefaults()
35 }
36
37 func (r *highlight) Run(ctx context.Context, args ...string) error {
38         if len(args) != 1 {
39                 return tool.CommandLineErrorf("highlight expects 1 argument (position)")
40         }
41
42         conn, err := r.app.connect(ctx)
43         if err != nil {
44                 return err
45         }
46         defer conn.terminate(ctx)
47
48         from := span.Parse(args[0])
49         file := conn.AddFile(ctx, from.URI())
50         if file.err != nil {
51                 return file.err
52         }
53
54         loc, err := file.mapper.Location(from)
55         if err != nil {
56                 return err
57         }
58
59         p := protocol.DocumentHighlightParams{
60                 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
61                         TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
62                         Position:     loc.Range.Start,
63                 },
64         }
65         highlights, err := conn.DocumentHighlight(ctx, &p)
66         if err != nil {
67                 return err
68         }
69
70         var results []span.Span
71         for _, h := range highlights {
72                 l := protocol.Location{Range: h.Range}
73                 s, err := file.mapper.Span(l)
74                 if err != nil {
75                         return err
76                 }
77                 results = append(results, s)
78         }
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
82         })
83
84         for _, s := range results {
85                 fmt.Println(s)
86         }
87         return nil
88 }