@@ -17,57 +17,35 @@ func (s *server) publishDiagnostics(ctx context.Context, conn jsonrpc2.Conn, fil
17
17
if err != nil {
18
18
return err
19
19
}
20
- pkg , ok := s .cache .pkgs .Get (filepath .Dir (string (file .URI .Filename ())))
21
- if ok {
22
- errs := pkg .TypeCheckResult .Errors ()
23
- if errs != nil {
24
- errors = append (errors , errs ... )
20
+
21
+ if pkg , ok := s .cache .pkgs .Get (filepath .Dir (string (file .URI .Filename ()))); ok {
22
+ filename := filepath .Base (file .URI .Filename ())
23
+ for _ , er := range pkg .TypeCheckResult .Errors () {
24
+ // Skip errors from other files in the same package
25
+ if ! strings .HasSuffix (er .FileName , filename ) {
26
+ continue
27
+ }
28
+ errors = append (errors , er )
25
29
}
26
30
}
27
31
28
- mPublishDiagnosticParams := make (map [string ]* protocol.PublishDiagnosticsParams )
29
- publishDiagnosticParams := make ([]* protocol.PublishDiagnosticsParams , 0 )
32
+ diagnostics := make ([]protocol.Diagnostic , 0 ) // Init required for JSONRPC to send an empty array
30
33
for _ , er := range errors {
31
- if ! strings .HasSuffix (file .URI .Filename (), er .FileName ) {
32
- continue
33
- }
34
- diagnostic := protocol.Diagnostic {
34
+ diagnostics = append (diagnostics , protocol.Diagnostic {
35
35
Range : * posToRange (er .Line , er .Span ),
36
36
Severity : protocol .DiagnosticSeverityError ,
37
37
Source : "gnopls" ,
38
38
Message : er .Msg ,
39
39
Code : er .Tool ,
40
- }
41
- if pdp , ok := mPublishDiagnosticParams [er .FileName ]; ok {
42
- pdp .Diagnostics = append (pdp .Diagnostics , diagnostic )
43
- continue
44
- }
45
- publishDiagnosticParam := protocol.PublishDiagnosticsParams {
46
- URI : file .URI ,
47
- Diagnostics : []protocol.Diagnostic {diagnostic },
48
- }
49
- publishDiagnosticParams = append (publishDiagnosticParams , & publishDiagnosticParam )
50
- mPublishDiagnosticParams [er .FileName ] = & publishDiagnosticParam
51
- }
52
-
53
- // Clean old diagnosed errors if no error found for current file
54
- found := false
55
- for _ , er := range errors {
56
- if strings .HasSuffix (er .FileName , filepath .Base (file .URI .Filename ())) {
57
- found = true
58
- break
59
- }
60
- }
61
- if ! found {
62
- publishDiagnosticParams = append (publishDiagnosticParams , & protocol.PublishDiagnosticsParams {
63
- URI : file .URI ,
64
- Diagnostics : []protocol.Diagnostic {},
65
40
})
66
41
}
67
42
68
43
return conn .Notify (
69
44
ctx ,
70
45
protocol .MethodTextDocumentPublishDiagnostics ,
71
- publishDiagnosticParams ,
46
+ protocol.PublishDiagnosticsParams {
47
+ URI : file .URI ,
48
+ Diagnostics : diagnostics ,
49
+ },
72
50
)
73
51
}
0 commit comments