/
issue.go
495 lines (431 loc) · 8.36 KB
/
issue.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package mg
import (
"bytes"
"margo.sh/htm"
"margo.sh/mgutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
)
var (
commonPatterns = struct {
sync.RWMutex
m map[Lang][]*regexp.Regexp
}{
m: map[Lang][]*regexp.Regexp{
"": {
regexp.MustCompile(`^\s*(?P<path>.+?\.\w+):(?P<line>\d+:)(?P<column>\d+:?)?(?P<message>.+)$`),
regexp.MustCompile(`^\s*(?P<path>.+?\.\w+)\((?P<line>\d+)(?:,(?P<column>\d+))?\):(?P<message>.+)$`),
},
},
}
)
type commonPattern struct {
Lang Lang
Patterns []*regexp.Regexp
}
func AddCommonPatterns(lang Lang, l ...*regexp.Regexp) {
p := &commonPatterns
p.Lock()
defer p.Unlock()
for _, k := range []Lang{"", lang} {
p.m[k] = append(p.m[k], l...)
}
}
func CommonPatterns(langs ...Lang) []*regexp.Regexp {
p := &commonPatterns
p.RLock()
defer p.RUnlock()
l := p.m[""]
l = l[:len(l):len(l)]
for _, lang := range langs {
l = append(l, p.m[lang]...)
}
return l
}
type IssueTag string
const (
Error = IssueTag("error")
Warning = IssueTag("warning")
Notice = IssueTag("notice")
)
type issueHash struct {
loc string
row int
msg string
}
type Issue struct {
Path string
Name string
Row int
Col int
End int
Tag IssueTag
Label string
Message string
}
func (isu *Issue) finalize() Issue {
v := *isu
if v.Tag == "" {
v.Tag = Error
}
return v
}
func (isu *Issue) hash() issueHash {
h := issueHash{
loc: isu.Path,
row: isu.Row,
msg: isu.Message,
}
if h.loc == "" {
h.loc = isu.Name
}
return h
}
func (isu *Issue) Equal(p Issue) bool {
return isu.hash() == p.hash()
}
func (isu *Issue) SameFile(p Issue) bool {
if isu.Path != "" {
return isu.Path == p.Path
}
return isu.Name == p.Name
}
func (isu *Issue) InView(v *View) bool {
if isu.Path != "" && isu.Path == v.Path {
return true
}
if isu.Name != "" && isu.Name == v.Name {
return true
}
return false
}
func (isu *Issue) Valid() bool {
return (isu.Name != "" || isu.Path != "") && isu.Message != ""
}
type IssueSet []Issue
func (s IssueSet) Equal(issues IssueSet) bool {
if len(s) != len(issues) {
return false
}
for _, p := range s {
if !issues.Has(p) {
return false
}
}
return true
}
func (s IssueSet) Add(l ...Issue) IssueSet {
m := make(map[issueHash]*Issue, len(s)+len(l))
for _, lst := range []IssueSet{s, IssueSet(l)} {
for i, _ := range lst {
isu := &lst[i]
m[isu.hash()] = isu
}
}
s = make(IssueSet, 0, len(m))
for _, isu := range m {
s = append(s, isu.finalize())
}
return s
}
func (s IssueSet) Remove(l ...Issue) IssueSet {
res := make(IssueSet, 0, len(s)+len(l))
q := IssueSet(l)
for _, p := range s {
if !q.Has(p) {
res = append(res, p)
}
}
return res
}
func (s IssueSet) Has(p Issue) bool {
for _, q := range s {
if p.Equal(q) {
return true
}
}
return false
}
func (is IssueSet) AllInView(v *View) IssueSet {
issues := make(IssueSet, 0, len(is))
for _, i := range is {
if i.InView(v) {
issues = append(issues, i)
}
}
return issues
}
type StoreIssues struct {
ActionType
IssueKey
Issues IssueSet
}
type IssueKey struct {
Key interface{}
Name string
Path string
Dir string
}
type issueKeySupport struct {
ReducerType
issues map[IssueKey]IssueSet
}
func (iks *issueKeySupport) RMount(mx *Ctx) {
iks.issues = map[IssueKey]IssueSet{}
}
func (iks *issueKeySupport) Reduce(mx *Ctx) *State {
switch act := mx.Action.(type) {
case StoreIssues:
if len(act.Issues) == 0 {
delete(iks.issues, act.IssueKey)
} else {
iks.issues[act.IssueKey] = act.Issues
}
}
issues := IssueSet{}
norm := filepath.Clean
name := norm(mx.View.Name)
path := norm(mx.View.Path)
dir := norm(mx.View.Dir())
match := func(k IssueKey) bool {
// no restrictions were set
k.Key = nil
if k == (IssueKey{}) {
return true
}
if path != "" && path == k.Path {
return true
}
if name != "" && name == k.Name {
return true
}
// if the view doesn't exist on disk, the dir is unreliable
if path != "" && dir != "" && dir == k.Dir {
return true
}
return false
}
for k, v := range iks.issues {
if match(k) {
issues = append(issues, v...)
}
}
return mx.State.AddIssues(issues...)
}
type issueStatusSupport struct {
ReducerType
buf bytes.Buffer
}
func (re *issueStatusSupport) Reduce(mx *Ctx) *State {
if len(mx.Issues) == 0 {
return mx.State
}
type Cfg struct {
title string
loc int
rem int
}
cfgs := map[IssueTag]*Cfg{
Error: {title: "Error"},
Warning: {title: "Warning"},
Notice: {title: "Notice"},
}
msg := ""
els := []htm.Element{}
for _, isu := range mx.Issues {
cfg, ok := cfgs[isu.Tag]
if !ok {
continue
}
if !isu.InView(mx.View) {
cfg.rem++
continue
}
cfg.loc++
if isu.Message == "" || isu.Row != mx.View.Row {
continue
}
s := ""
if isu.Label == "" {
s = isu.Message
} else {
s = isu.Label + ": " + isu.Message
}
els = append(els, htm.Text(s))
if len(msg) <= 1 {
msg = s
}
}
status := make([]string, 0, len(cfgs)+1)
for _, k := range []IssueTag{Error, Warning, Notice} {
cfg := cfgs[k]
if cfg.loc == 0 && cfg.rem == 0 {
continue
}
re.buf.Reset()
loc, rem := mgutil.PrimaryDigits, mgutil.SecondaryDigits
if cfg.loc == 0 {
loc, rem = rem, loc
}
re.buf.WriteString(cfg.title)
re.buf.WriteByte(' ')
loc.DrawInto(cfg.loc, &re.buf)
re.buf.WriteRune('ꞏ')
rem.DrawInto(cfg.rem, &re.buf)
status = append(status, re.buf.String())
}
st := mx.State.AddHUD(
htm.Span(nil,
htm.A(&htm.AAttrs{Action: DisplayIssues{}}, htm.Text("Issues")),
htm.Textf(" ( %s )", strings.Join(status, ", ")),
),
els...,
)
if msg != "" {
status = append(status, msg)
}
return st.AddStatus(status...)
}
type IssueOut struct {
Patterns []*regexp.Regexp
Base Issue
Dir string
Done chan<- struct{}
buf []byte
mu sync.Mutex
issues IssueSet
isu *Issue
pfx []byte
closed bool
}
func (w *IssueOut) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.closed {
return 0, os.ErrClosed
}
w.buf = append(w.buf, p...)
w.scan(false)
return len(p), nil
}
func (w *IssueOut) Close() error {
defer w.Flush()
w.mu.Lock()
defer w.mu.Unlock()
if w.closed {
return os.ErrClosed
}
w.closed = true
if w.Done != nil {
close(w.Done)
}
return nil
}
func (w *IssueOut) Flush() error {
w.mu.Lock()
defer w.mu.Unlock()
w.flush()
return nil
}
func (w *IssueOut) Issues() IssueSet {
w.mu.Lock()
defer w.mu.Unlock()
w.scan(true)
issues := make(IssueSet, len(w.issues))
copy(issues, w.issues)
return issues
}
func (w *IssueOut) scan(scanTail bool) {
lines := bytes.Split(w.buf, []byte{'\n'})
var tail []byte
if !scanTail {
n := len(lines) - 1
tail, lines = lines[n], lines[:n]
}
for _, ln := range lines {
w.scanLine(bytes.TrimRight(ln, "\r"))
}
w.buf = append(w.buf[:0], tail...)
}
func (w *IssueOut) scanLine(ln []byte) {
pfx := ln[:len(ln)-len(bytes.TrimLeft(ln, " \t"))]
ind := bytes.TrimPrefix(pfx, w.pfx)
if n := len(ind); n > 0 && w.isu != nil {
w.isu.Message += "\n" + string(ln[len(pfx)-n:])
return
}
w.flush()
w.pfx = pfx
ln = ln[len(pfx):]
w.isu = w.match(ln)
}
func (w *IssueOut) flush() {
if w.isu == nil {
return
}
isu := *w.isu
w.isu = nil
if isu.Valid() && !w.issues.Has(isu) {
w.issues = append(w.issues, isu)
}
}
func (w *IssueOut) match(s []byte) *Issue {
for _, p := range w.Patterns {
if isu := w.matchOne(p, s); isu != nil {
return isu
}
}
return nil
}
func (w *IssueOut) matchOne(p *regexp.Regexp, s []byte) *Issue {
submatch := p.FindSubmatch(s)
if submatch == nil {
return nil
}
str := func(s []byte) string {
return string(bytes.Trim(s, ": \t\r\n"))
}
num := func(s []byte) int {
if n, _ := strconv.Atoi(str(s)); n > 0 {
return n - 1
}
return 0
}
isu := w.Base
for i, k := range p.SubexpNames() {
v := submatch[i]
switch k {
case "path":
isu.Path = str(v)
if isu.Path != "" && w.Dir != "" && !filepath.IsAbs(isu.Path) {
isu.Path = filepath.Join(w.Dir, isu.Path)
}
case "line":
isu.Row = num(v)
case "column":
isu.Col = num(v)
case "end":
isu.End = num(v)
case "label":
lbl := str(v)
if lbl != "" {
isu.Label = lbl
}
case "error", "warning", "notice":
isu.Tag = IssueTag(k)
isu.Message = str(v)
case "message":
isu.Message = str(v)
case "tag":
tag := IssueTag(str(v))
if tag == Warning || tag == Error || tag == Notice {
isu.Tag = tag
}
}
}
return &isu
}