@@ -17,13 +17,62 @@ import (
17
17
18
18
var (
19
19
// errRegexp matches the various Jsonnet location formats in errors.
20
- // file:line msg
21
- // file:line:col-endCol msg
22
- // file:(line:endLine)-(col:endCol) msg
23
- // Has 10 matching groups.
24
- errRegexp = regexp .MustCompile (`/.*:(?:(\d+)|(?:(\d+):(\d+)-(\d+))|(?:\((\d+):(\d+)\)-\((\d+):(\d+))\))\s(.*)` )
20
+ // 1. file:line msg
21
+ // 2. file:line:col msg
22
+ // 3. file:line:col-endCol msg
23
+ // 4. file:(line:col)-(endLine:endCol) msg
24
+ // https://regex101.com/r/tL5VWi/2
25
+ errRegexp = regexp .MustCompile (`/[^:]*:` +
26
+ `(?:(?P<startLine1>\d+)` +
27
+ `|(?P<startLine2>\d+):(?P<startCol2>\d+)` +
28
+ `|(?:(?P<startLine3>\d+):(?P<startCol3>\d+)-(?P<endCol3>\d+))` +
29
+ `|(?:\((?P<startLine4>\d+):(?P<startCol4>\d+)\)-\((?P<endLine4>\d+):(?P<endCol4>\d+))\))` +
30
+ `\s(?P<message>.*)` )
25
31
)
26
32
33
+ func parseErrRegexpMatch (match []string ) (string , protocol.Range ) {
34
+ get := func (name string ) string {
35
+ idx := errRegexp .SubexpIndex (name )
36
+ if len (match ) <= idx {
37
+ return ""
38
+ }
39
+ return match [idx ]
40
+ }
41
+
42
+ message , line , col , endLine , endCol := "" , 1 , 1 , 1 , 1
43
+ if len (match ) > 1 {
44
+ if lineStr := get ("startLine1" ); lineStr != "" {
45
+ line , _ = strconv .Atoi (lineStr )
46
+ endLine = line
47
+ }
48
+
49
+ if lineStr := get ("startLine2" ); lineStr != "" {
50
+ line , _ = strconv .Atoi (lineStr )
51
+ endLine = line
52
+ col , _ = strconv .Atoi (get ("startCol2" ))
53
+ endCol = col
54
+ }
55
+
56
+ if lineStr := get ("startLine3" ); lineStr != "" {
57
+ line , _ = strconv .Atoi (lineStr )
58
+ endLine = line
59
+ col , _ = strconv .Atoi (get ("startCol3" ))
60
+ endCol , _ = strconv .Atoi (get ("endCol3" ))
61
+ }
62
+
63
+ if lineStr := get ("startLine4" ); lineStr != "" {
64
+ line , _ = strconv .Atoi (lineStr )
65
+ endLine , _ = strconv .Atoi (get ("endLine4" ))
66
+ col , _ = strconv .Atoi (get ("startCol4" ))
67
+ endCol , _ = strconv .Atoi (get ("endCol4" ))
68
+ }
69
+
70
+ message = get ("message" )
71
+ }
72
+
73
+ return message , position .NewProtocolRange (line - 1 , col - 1 , endLine - 1 , endCol - 1 )
74
+ }
75
+
27
76
func (s * server ) queueDiagnostics (uri protocol.DocumentURI ) {
28
77
s .cache .diagMutex .Lock ()
29
78
defer s .cache .diagMutex .Unlock ()
@@ -109,9 +158,7 @@ func (s *server) getEvalDiags(doc *document) (diags []protocol.Diagnostic) {
109
158
doc .val , doc .err = vm .EvaluateAnonymousSnippet (doc .item .URI .SpanURI ().Filename (), doc .item .Text )
110
159
}
111
160
112
- // Initialize with 1 because we indiscriminately subtract one to map error ranges to LSP ranges.
113
161
if doc .err != nil {
114
- line , col , endLine , endCol := 1 , 1 , 1 , 1
115
162
diag := protocol.Diagnostic {Source : "jsonnet evaluation" }
116
163
lines := strings .Split (doc .err .Error (), "\n " )
117
164
if len (lines ) == 0 {
@@ -127,34 +174,17 @@ func (s *server) getEvalDiags(doc *document) (diags []protocol.Diagnostic) {
127
174
} else {
128
175
match = errRegexp .FindStringSubmatch (lines [0 ])
129
176
}
130
- if len (match ) == 10 {
131
- if match [1 ] != "" {
132
- line , _ = strconv .Atoi (match [1 ])
133
- endLine = line + 1
134
- }
135
- if match [2 ] != "" {
136
- line , _ = strconv .Atoi (match [2 ])
137
- col , _ = strconv .Atoi (match [3 ])
138
- endLine = line
139
- endCol , _ = strconv .Atoi (match [4 ])
140
- }
141
- if match [5 ] != "" {
142
- line , _ = strconv .Atoi (match [5 ])
143
- col , _ = strconv .Atoi (match [6 ])
144
- endLine , _ = strconv .Atoi (match [7 ])
145
- endCol , _ = strconv .Atoi (match [8 ])
146
- }
147
- }
148
177
178
+ message , rang := parseErrRegexpMatch (match )
149
179
if runtimeErr {
150
180
diag .Message = doc .err .Error ()
151
181
diag .Severity = protocol .SeverityWarning
152
182
} else {
153
- diag .Message = match [ 9 ]
183
+ diag .Message = message
154
184
diag .Severity = protocol .SeverityError
155
185
}
156
186
157
- diag .Range = position . NewProtocolRange ( line - 1 , col - 1 , endLine - 1 , endCol - 1 )
187
+ diag .Range = rang
158
188
diags = append (diags , diag )
159
189
}
160
190
@@ -167,31 +197,8 @@ func (s *server) getLintDiags(doc *document) (diags []protocol.Diagnostic) {
167
197
log .Errorf ("getLintDiags: %s: %v\n " , errorRetrievingDocument , err )
168
198
} else {
169
199
for _ , match := range errRegexp .FindAllStringSubmatch (result , - 1 ) {
170
- line , col , endLine , endCol := 1 , 1 , 1 , 1
171
200
diag := protocol.Diagnostic {Source : "lint" , Severity : protocol .SeverityWarning }
172
-
173
- if len (match ) == 10 {
174
- if match [1 ] != "" {
175
- line , _ = strconv .Atoi (match [1 ])
176
- endLine = line + 1
177
- }
178
- if match [2 ] != "" {
179
- line , _ = strconv .Atoi (match [2 ])
180
- col , _ = strconv .Atoi (match [3 ])
181
- endLine = line
182
- endCol , _ = strconv .Atoi (match [4 ])
183
- }
184
- if match [5 ] != "" {
185
- line , _ = strconv .Atoi (match [5 ])
186
- col , _ = strconv .Atoi (match [6 ])
187
- endLine , _ = strconv .Atoi (match [7 ])
188
- endCol , _ = strconv .Atoi (match [8 ])
189
- }
190
- }
191
-
192
- diag .Message = match [9 ]
193
-
194
- diag .Range = position .NewProtocolRange (line - 1 , col - 1 , endLine - 1 , endCol - 1 )
201
+ diag .Message , diag .Range = parseErrRegexpMatch (match )
195
202
diags = append (diags , diag )
196
203
}
197
204
}
0 commit comments