-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics.go
86 lines (76 loc) · 1.89 KB
/
diagnostics.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
package tekton
import (
"fmt"
"regexp"
"strconv"
protocol "github.com/tliron/glsp/protocol_3_16"
)
// diagnostics sends into the argument channel any problems identified
// in the document. It currently only reports references for which none
// identifier has been found.
func (d *Document) diagnostics(c chan<- *protocol.Diagnostic) {
for _, ref := range d.references {
if ref.ident != nil {
continue
}
sev := protocol.DiagnosticSeverityError
src := fmt.Sprintf("unknown-%s", ref.kind)
c <- &protocol.Diagnostic{
Range: protocol.Range{
Start: d.OffsetPosition(ref.offsets[0]),
End: d.OffsetPosition(ref.offsets[1]),
},
Message: fmt.Sprintf("unknown %s %s", ref.kind, ref.name),
Severity: &sev,
Source: &src,
}
}
for _, id := range d.identifiers {
if len(id.references) != 0 {
continue
}
if id.kind == IdentKindPipelineTask {
continue
}
sev := protocol.DiagnosticSeverityWarning
src := fmt.Sprintf("unused-%s", id.kind)
c <- &protocol.Diagnostic{
Range: id.location.Range,
Message: fmt.Sprintf("unused %s %s", id.kind, id.meta.Name()),
Severity: &sev,
Source: &src,
}
}
}
var syntaxErrorRegexp = regexp.MustCompile(`(?s)^\[(\d+):(\d+)\] (.+)`)
// syntaxErrorDiagnostic is a hack to extract error position from goccy/go-yaml
// unexported syntaxError
func syntaxErrorDiagnostic(err error) *protocol.Diagnostic {
ms := syntaxErrorRegexp.FindStringSubmatch(err.Error())
if len(ms) != 4 {
return nil
}
line, err := strconv.Atoi(ms[1])
if err != nil {
return nil
}
col, err := strconv.Atoi(ms[2])
if err != nil {
return nil
}
pos := protocol.Position{
Line: uint32(line - 1),
Character: uint32(col - 1),
}
sev := protocol.DiagnosticSeverityError
src := "syntax"
return &protocol.Diagnostic{
Range: protocol.Range{
Start: pos,
End: pos,
},
Message: ms[3],
Severity: &sev,
Source: &src,
}
}