-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathctags.go
321 lines (275 loc) · 6.65 KB
/
ctags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Package ctags provides a Go wrapper for universal-ctags.
package ctags
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"strings"
"unicode/utf8"
)
type Entry struct {
Name string
Path string
Line int
Kind string
Language string
Parent string
ParentKind string
Pattern string
Signature string
FileLimited bool
}
type Parser interface {
Parse(path string, content []byte) ([]*Entry, error)
Close()
}
type Options struct {
// Bin is the command to run. Defaults to "universal-ctags" if empty.
Bin string
// PatternLengthLimit is the cutoff length of the patterns output by
// ctags. (--pattern-length-limit). If 0 defaults to 255.
//
// Note: --pattern-length-limit=0 disables this in universal-ctags. We don't
// allow disabling it.
PatternLengthLimit int
// Info if non-nil will log info messages
Info *log.Logger
// Debug if non-nil will log debug messages
Debug *log.Logger
}
func New(opts Options) (Parser, error) {
if opts.Bin == "" {
opts.Bin = "universal-ctags"
}
if opts.PatternLengthLimit == 0 {
opts.PatternLengthLimit = 255
}
// ctagsArgs is the contents of ctags.d. ctags.d needs to be in a specific
// location to be read correctly. We have accidently regressed on this
// twice. Instead we pass in the arguments here.
args := []string{"--_interactive=default", "--fields=*", fmt.Sprintf("--pattern-length-limit=%d", opts.PatternLengthLimit)}
args = append(args, ctagsArgs...)
// Some languages cause issues in universal-ctags. Stick to an allowlist of
// known working languages.
args = append(args, "--languages="+strings.Join(SupportedLanguages[:], ","))
cmd := exec.Command(opts.Bin, args...)
in, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
out, err := cmd.StdoutPipe()
if err != nil {
in.Close()
return nil, err
}
cmd.Stderr = os.Stderr
proc := ctagsProcess{
cmd: cmd,
in: in,
out: &scanner{r: bufio.NewReaderSize(out, 4096)},
outPipe: out,
Info: opts.Info,
Debug: opts.Debug,
}
if err := cmd.Start(); err != nil {
return nil, err
}
var init reply
if err := proc.read(&init); err != nil {
proc.Close()
return nil, err
}
if init.Typ == "error" {
proc.Close()
return nil, fmt.Errorf("starting %s failed with: %s", opts.Bin, init.Message)
}
return &proc, nil
}
type ctagsProcess struct {
cmd *exec.Cmd
in io.WriteCloser
out *scanner
outPipe io.ReadCloser
Info *log.Logger
Debug *log.Logger
}
func (p *ctagsProcess) Close() {
_ = p.cmd.Process.Kill()
_ = p.outPipe.Close()
_ = p.in.Close()
_ = p.cmd.Wait()
}
func (p *ctagsProcess) read(rep *reply) error {
if !p.out.Scan() {
// Some errors do not kill the parser. We would deadlock if we waited
// for the process to exit.
err := p.out.Err()
p.Close()
return err
}
if p.Debug != nil {
p.Debug.Printf("read %q", p.out.Bytes())
}
// See https://github.com/universal-ctags/ctags/issues/1493
if bytes.Equal([]byte("(null)"), p.out.Bytes()) {
return nil
}
err := json.Unmarshal(p.out.Bytes(), rep)
if err != nil {
return fmt.Errorf("unmarshal(%q): %v", p.out.Bytes(), err)
}
return nil
}
// universal-ctags line buffer size is only 1024.
const ctagsLineBufferSize = 1024
func (p *ctagsProcess) post(req *request, content []byte) (bool, error) {
body, err := json.Marshal(req)
if err != nil {
return false, err
}
body = append(body, '\n')
// -1 for c-style string
if len(body) > ctagsLineBufferSize-1 {
return false, nil
}
if p.Debug != nil {
p.Debug.Printf("post %q", body)
}
if _, err = p.in.Write(body); err != nil {
return false, err
}
_, err = p.in.Write(content)
if p.Debug != nil {
p.Debug.Println(string(content))
}
return err == nil, err
}
type request struct {
Command string `json:"command"`
Filename string `json:"filename"`
Size int `json:"size"`
}
type reply struct {
// Init
Typ string `json:"_type"`
Name string `json:"name"`
Version string `json:"version"`
// completed
Command string `json:"command"`
// error
Message string `json:"message"`
Fatal bool `json:"fatal"`
Path string `json:"path"`
Language string `json:"language"`
Line int `json:"line"`
Kind string `json:"kind"`
End int `json:"end"`
Scope string `json:"scope"`
ScopeKind string `json:"scopeKind"`
Access string `json:"access"`
File bool `json:"file"`
Signature string `json:"signature"`
Pattern string `json:"pattern"`
}
func (p *ctagsProcess) Parse(name string, content []byte) ([]*Entry, error) {
filename := path.Base(name)
req := request{
Command: "generate-tags",
Size: len(content),
Filename: filename,
}
if !utf8.Valid(content) {
if p.Info != nil {
p.Info.Printf("ctags skipping file due not being utf-8 encoded: %s", name)
}
return nil, nil
}
if ok, err := p.post(&req, content); err != nil {
return nil, err
} else if !ok {
if p.Info != nil {
p.Info.Printf("ctags skipping file due to long filename: %s", name)
}
return nil, nil
}
// 250 is a better guess for initial size
es := make([]*Entry, 0, 250)
for {
var rep reply
if err := p.read(&rep); err != nil {
return nil, err
}
switch rep.Typ {
case "completed":
return es, nil
case "error":
if rep.Fatal {
return nil, fmt.Errorf("fatal ctags error for %s: %s", name, rep.Message)
} else if p.Info != nil {
p.Info.Printf("ignoring non-fatal ctags error for %s: %s", name, rep.Message)
}
case "tag":
if rep.Path == filename {
rep.Path = name
}
es = append(es, &Entry{
Name: rep.Name,
Path: rep.Path,
Line: rep.Line,
Kind: rep.Kind,
Language: rep.Language,
Parent: rep.Scope,
ParentKind: rep.ScopeKind,
Pattern: rep.Pattern,
Signature: rep.Signature,
})
default:
return nil, fmt.Errorf("ctags unexpected response %s for %s", rep.Typ, name)
}
}
}
// scanner is like bufio.Scanner but skips long lines instead of returning
// bufio.ErrTooLong.
//
// Additionally it will skip empty lines.
type scanner struct {
r *bufio.Reader
line []byte
err error
}
func (s *scanner) Scan() bool {
if s.err != nil {
return false
}
var (
err error
line []byte
)
for err == nil && len(line) == 0 {
line, err = s.r.ReadSlice('\n')
for err == bufio.ErrBufferFull {
// make line empty so we ignore it
line = nil
_, err = s.r.ReadSlice('\n')
}
line = bytes.TrimSuffix(line, []byte{'\n'})
line = bytes.TrimSuffix(line, []byte{'\r'})
}
s.line, s.err = line, err
return len(line) > 0
}
func (s *scanner) Bytes() []byte {
return s.line
}
func (s *scanner) Err() error {
if s.err == io.EOF {
return nil
}
return s.err
}