@@ -6,7 +6,6 @@ package source
6
6
7
7
import (
8
8
"context"
9
- "fmt"
10
9
"strings"
11
10
12
11
"golang.org/x/tools/go/analysis"
@@ -69,7 +68,9 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, withA
69
68
// Prepare the reports we will send for the files in this package.
70
69
reports := make (map [FileIdentity ][]Diagnostic )
71
70
for _ , fh := range pkg .CompiledGoFiles () {
72
- clearReports (snapshot , reports , fh .File ().Identity ())
71
+ if err := clearReports (snapshot , reports , fh .File ().Identity ().URI ); err != nil {
72
+ return nil , warningMsg , err
73
+ }
73
74
}
74
75
// Prepare any additional reports for the errors in this package.
75
76
for _ , e := range pkg .GetErrors () {
@@ -85,11 +86,9 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, withA
85
86
}
86
87
}
87
88
}
88
- fh , err := snapshot .GetFile (e .URI )
89
- if err != nil {
89
+ if err := clearReports (snapshot , reports , e .URI ); err != nil {
90
90
return nil , warningMsg , err
91
91
}
92
- clearReports (snapshot , reports , fh .Identity ())
93
92
}
94
93
// Run diagnostics for the package that this URI belongs to.
95
94
hadDiagnostics , err := diagnostics (ctx , snapshot , reports , pkg )
@@ -142,21 +141,17 @@ func diagnostics(ctx context.Context, snapshot Snapshot, reports map[FileIdentit
142
141
_ = ctx // circumvent SA4006
143
142
defer done ()
144
143
145
- diagSets := make (map [FileIdentity ]* diagnosticSet )
144
+ diagSets := make (map [span. URI ]* diagnosticSet )
146
145
for _ , e := range pkg .GetErrors () {
147
146
diag := & Diagnostic {
148
147
Message : e .Message ,
149
148
Range : e .Range ,
150
149
Severity : protocol .SeverityError ,
151
150
}
152
- fh , err := snapshot .GetFile (e .URI )
153
- if err != nil {
154
- return false , err
155
- }
156
- set , ok := diagSets [fh .Identity ()]
151
+ set , ok := diagSets [e .URI ]
157
152
if ! ok {
158
153
set = & diagnosticSet {}
159
- diagSets [fh . Identity () ] = set
154
+ diagSets [e . URI ] = set
160
155
}
161
156
switch e .Kind {
162
157
case ParseError :
@@ -171,7 +166,7 @@ func diagnostics(ctx context.Context, snapshot Snapshot, reports map[FileIdentit
171
166
}
172
167
}
173
168
var nonEmptyDiagnostics bool // track if we actually send non-empty diagnostics
174
- for fileID , set := range diagSets {
169
+ for uri , set := range diagSets {
175
170
// Don't report type errors if there are parse errors or list errors.
176
171
diags := set .typeErrors
177
172
if len (set .parseErrors ) > 0 {
@@ -182,7 +177,9 @@ func diagnostics(ctx context.Context, snapshot Snapshot, reports map[FileIdentit
182
177
if len (diags ) > 0 {
183
178
nonEmptyDiagnostics = true
184
179
}
185
- addReports (ctx , snapshot , reports , fileID , diags ... )
180
+ if err := addReports (ctx , snapshot , reports , uri , diags ... ); err != nil {
181
+ return false , err
182
+ }
186
183
}
187
184
return nonEmptyDiagnostics , nil
188
185
}
@@ -210,59 +207,53 @@ func analyses(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][
210
207
if onlyDeletions (e .SuggestedFixes ) {
211
208
tags = append (tags , protocol .Unnecessary )
212
209
}
213
- fh , err := snapshot .GetFile (e .URI )
214
- if err != nil {
215
- return err
216
- }
217
- addReports (ctx , snapshot , reports , fh .Identity (), & Diagnostic {
210
+ if err := addReports (ctx , snapshot , reports , e .URI , & Diagnostic {
218
211
Range : e .Range ,
219
212
Message : e .Message ,
220
213
Source : e .Category ,
221
214
Severity : protocol .SeverityWarning ,
222
215
Tags : tags ,
223
216
SuggestedFixes : e .SuggestedFixes ,
224
217
Related : e .Related ,
225
- })
218
+ }); err != nil {
219
+ return err
220
+ }
226
221
}
227
222
return nil
228
223
}
229
224
230
- func clearReports (snapshot Snapshot , reports map [FileIdentity ][]Diagnostic , fileID FileIdentity ) {
231
- if snapshot .View ().Ignore (fileID . URI ) {
232
- return
225
+ func clearReports (snapshot Snapshot , reports map [FileIdentity ][]Diagnostic , uri span. URI ) error {
226
+ if snapshot .View ().Ignore (uri ) {
227
+ return nil
233
228
}
234
- reports [fileID ] = []Diagnostic {}
229
+ fh , err := snapshot .GetFile (uri )
230
+ if err != nil {
231
+ return err
232
+ }
233
+ reports [fh .Identity ()] = []Diagnostic {}
234
+ return nil
235
235
}
236
236
237
- func addReports (ctx context.Context , snapshot Snapshot , reports map [FileIdentity ][]Diagnostic , fileID FileIdentity , diagnostics ... * Diagnostic ) error {
238
- if snapshot .View ().Ignore (fileID . URI ) {
237
+ func addReports (ctx context.Context , snapshot Snapshot , reports map [FileIdentity ][]Diagnostic , uri span. URI , diagnostics ... * Diagnostic ) error {
238
+ if snapshot .View ().Ignore (uri ) {
239
239
return nil
240
240
}
241
- if _ , ok := reports [fileID ]; ! ok {
242
- return errors .Errorf ("diagnostics for unexpected file %s" , fileID .URI )
241
+ fh , err := snapshot .GetFile (uri )
242
+ if err != nil {
243
+ return err
244
+ }
245
+ if _ , ok := reports [fh .Identity ()]; ! ok {
246
+ return errors .Errorf ("diagnostics for unexpected file %s" , uri )
243
247
}
244
248
for _ , diag := range diagnostics {
245
249
if diag == nil {
246
250
continue
247
251
}
248
- reports [fileID ] = append (reports [fileID ], * diag )
252
+ reports [fh . Identity () ] = append (reports [fh . Identity () ], * diag )
249
253
}
250
254
return nil
251
255
}
252
256
253
- func singleDiagnostic (fileID FileIdentity , format string , a ... interface {}) map [FileIdentity ][]Diagnostic {
254
- return map [FileIdentity ][]Diagnostic {
255
- fileID : []Diagnostic {
256
- {
257
- Source : "gopls" ,
258
- Range : protocol.Range {},
259
- Message : fmt .Sprintf (format , a ... ),
260
- Severity : protocol .SeverityError ,
261
- },
262
- },
263
- }
264
- }
265
-
266
257
// onlyDeletions returns true if all of the suggested fixes are deletions.
267
258
func onlyDeletions (fixes []SuggestedFix ) bool {
268
259
for _ , fix := range fixes {
0 commit comments