Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / internal / lsp / cmd / semantictokens.go
1 // Copyright 2020 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         "bytes"
9         "context"
10         "flag"
11         "fmt"
12         "go/parser"
13         "go/token"
14         "io/ioutil"
15         "log"
16         "os"
17         "runtime"
18         "unicode/utf8"
19
20         "golang.org/x/tools/internal/lsp"
21         "golang.org/x/tools/internal/lsp/protocol"
22         "golang.org/x/tools/internal/lsp/source"
23         "golang.org/x/tools/internal/span"
24 )
25
26 // generate semantic tokens and interpolate them in the file
27
28 // The output is the input file decorated with comments showing the
29 // syntactic tokens. The comments are stylized:
30 //   /*<arrow><length>,<token type>,[<modifiers]*/
31 // For most occurrences, the comment comes just before the token it
32 // describes, and arrow is a right arrow. If the token is inside a string
33 // the comment comes just after the string, and the arrow is a left arrow.
34 // <length> is the length of the token in runes, <token type> is one
35 // of the supported semantic token types, and <modifiers. is a
36 // (possibly empty) list of token type modifiers.
37
38 // There are 3 coordinate systems for lines and character offsets in lines
39 // LSP (what's returned from semanticTokens()):
40 //    0-based: the first line is line 0, the first character of a line
41 //      is character 0, and characters are counted as UTF-16 code points
42 // gopls (and Go error messages):
43 //    1-based: the first line is line1, the first chararcter of a line
44 //      is character 0, and characters are counted as bytes
45 // internal (as used in marks, and lines:=bytes.Split(buf, '\n'))
46 //    0-based: lines and character positions are 1 less than in
47 //      the gopls coordinate system
48
49 type semtok struct {
50         app *Application
51 }
52
53 var colmap *protocol.ColumnMapper
54
55 func (c *semtok) Name() string      { return "semtok" }
56 func (c *semtok) Usage() string     { return "<filename>" }
57 func (c *semtok) ShortHelp() string { return "show semantic tokens for the specified file" }
58 func (c *semtok) DetailedHelp(f *flag.FlagSet) {
59         for i := 1; ; i++ {
60                 _, f, l, ok := runtime.Caller(i)
61                 if !ok {
62                         break
63                 }
64                 log.Printf("%d: %s:%d", i, f, l)
65         }
66         fmt.Fprint(f.Output(), `
67 Example: show the semantic tokens for this file:
68
69   $ gopls semtok internal/lsp/cmd/semtok.go
70
71         gopls semtok flags are:
72 `)
73         f.PrintDefaults()
74 }
75
76 // Run performs the semtok on the files specified by args and prints the
77 // results to stdout. PJW: fix this description
78 func (c *semtok) Run(ctx context.Context, args ...string) error {
79         log.SetFlags(log.Lshortfile)
80         if len(args) != 1 {
81                 return fmt.Errorf("expected one file name, got %d", len(args))
82         }
83         // perhaps simpler if app had just had a FlagSet member
84         origOptions := c.app.options
85         c.app.options = func(opts *source.Options) {
86                 origOptions(opts)
87                 opts.SemanticTokens = true
88         }
89         conn, err := c.app.connect(ctx)
90         if err != nil {
91                 return err
92         }
93         defer conn.terminate(ctx)
94         uri := span.URIFromPath(args[0])
95         file := conn.AddFile(ctx, uri)
96         if file.err != nil {
97                 return file.err
98         }
99
100         resp, err := conn.semanticTokens(ctx, uri)
101         if err != nil {
102                 return err
103         }
104         buf, err := ioutil.ReadFile(args[0])
105         if err != nil {
106                 log.Fatal(err)
107         }
108         fset := token.NewFileSet()
109         f, err := parser.ParseFile(fset, args[0], buf, 0)
110         if err != nil {
111                 log.Printf("parsing %s failed %v", args[0], err)
112                 return err
113         }
114         tok := fset.File(f.Pos())
115         if tok == nil {
116                 // can't happen; just parsed this file
117                 return fmt.Errorf("can't find %s in fset", args[0])
118         }
119         tc := span.NewContentConverter(args[0], buf)
120         colmap = &protocol.ColumnMapper{
121                 URI:       span.URI(args[0]),
122                 Content:   buf,
123                 Converter: tc,
124         }
125         memo = lsp.SemanticMemo
126         err = decorate(file.uri.Filename(), resp.Data)
127         if err != nil {
128                 return err
129         }
130         return nil
131 }
132
133 var memo *lsp.SemMemo
134
135 type mark struct {
136         line, offset int // 1-based, from RangeSpan
137         len          int // bytes, not runes
138         typ          string
139         mods         []string
140 }
141
142 // prefixes for semantic token comments
143 const (
144         SemanticLeft  = "/*⇐"
145         SemanticRight = "/*⇒"
146 )
147
148 func markLine(m mark, lines [][]byte) {
149         l := lines[m.line-1] // mx is 1-based
150         length := utf8.RuneCount(l[m.offset-1 : m.offset-1+m.len])
151         splitAt := m.offset - 1
152         insert := ""
153         if m.typ == "namespace" && m.offset-1+m.len < len(l) && l[m.offset-1+m.len] == '"' {
154                 // it is the last component of an import spec
155                 // cannot put a comment inside a string
156                 insert = fmt.Sprintf("%s%d,namespace,[]*/", SemanticLeft, length)
157                 splitAt = m.offset + m.len
158         } else {
159                 insert = fmt.Sprintf("%s%d,%s,%v*/", SemanticRight, length, m.typ, m.mods)
160         }
161         x := append([]byte(insert), l[splitAt:]...)
162         l = append(l[:splitAt], x...)
163         lines[m.line-1] = l
164 }
165
166 func decorate(file string, result []float64) error {
167         buf, err := ioutil.ReadFile(file)
168         if err != nil {
169                 return err
170         }
171         marks := newMarks(result)
172         if len(marks) == 0 {
173                 return nil
174         }
175         lines := bytes.Split(buf, []byte{'\n'})
176         for i := len(marks) - 1; i >= 0; i-- {
177                 mx := marks[i]
178                 markLine(mx, lines)
179         }
180         os.Stdout.Write(bytes.Join(lines, []byte{'\n'}))
181         return nil
182 }
183
184 func newMarks(d []float64) []mark {
185         ans := []mark{}
186         // the following two loops could be merged, at the cost
187         // of making the logic slightly more complicated to understand
188         // first, convert from deltas to absolute, in LSP coordinates
189         lspLine := make([]float64, len(d)/5)
190         lspChar := make([]float64, len(d)/5)
191         line, char := 0.0, 0.0
192         for i := 0; 5*i < len(d); i++ {
193                 lspLine[i] = line + d[5*i+0]
194                 if d[5*i+0] > 0 {
195                         char = 0
196                 }
197                 lspChar[i] = char + d[5*i+1]
198                 char = lspChar[i]
199                 line = lspLine[i]
200         }
201         // second, convert to gopls coordinates
202         for i := 0; 5*i < len(d); i++ {
203                 pr := protocol.Range{
204                         Start: protocol.Position{
205                                 Line:      lspLine[i],
206                                 Character: lspChar[i],
207                         },
208                         End: protocol.Position{
209                                 Line:      lspLine[i],
210                                 Character: lspChar[i] + d[5*i+2],
211                         },
212                 }
213                 spn, err := colmap.RangeSpan(pr)
214                 if err != nil {
215                         log.Fatal(err)
216                 }
217                 m := mark{
218                         line:   spn.Start().Line(),
219                         offset: spn.Start().Column(),
220                         len:    spn.End().Column() - spn.Start().Column(),
221                         typ:    memo.Type(int(d[5*i+3])),
222                         mods:   memo.Mods(int(d[5*i+4])),
223                 }
224                 ans = append(ans, m)
225         }
226         return ans
227 }