Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit 864ad35

Browse files
authored
fix: change publish diagnostics to return params for a single file (#2)
1 parent a2f18c7 commit 864ad35

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

internal/lsp/diagnostics.go

+15-37
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,35 @@ func (s *server) publishDiagnostics(ctx context.Context, conn jsonrpc2.Conn, fil
1717
if err != nil {
1818
return err
1919
}
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)
2529
}
2630
}
2731

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
3033
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{
3535
Range: *posToRange(er.Line, er.Span),
3636
Severity: protocol.DiagnosticSeverityError,
3737
Source: "gnopls",
3838
Message: er.Msg,
3939
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{},
6540
})
6641
}
6742

6843
return conn.Notify(
6944
ctx,
7045
protocol.MethodTextDocumentPublishDiagnostics,
71-
publishDiagnosticParams,
46+
protocol.PublishDiagnosticsParams{
47+
URI: file.URI,
48+
Diagnostics: diagnostics,
49+
},
7250
)
7351
}

0 commit comments

Comments
 (0)