Skip to content

Commit

Permalink
fix: change publish diagnostics to return params for a single file (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeronimoalbi authored Aug 9, 2024
1 parent a2f18c7 commit 864ad35
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions internal/lsp/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,35 @@ func (s *server) publishDiagnostics(ctx context.Context, conn jsonrpc2.Conn, fil
if err != nil {
return err
}
pkg, ok := s.cache.pkgs.Get(filepath.Dir(string(file.URI.Filename())))
if ok {
errs := pkg.TypeCheckResult.Errors()
if errs != nil {
errors = append(errors, errs...)

if pkg, ok := s.cache.pkgs.Get(filepath.Dir(string(file.URI.Filename()))); ok {
filename := filepath.Base(file.URI.Filename())
for _, er := range pkg.TypeCheckResult.Errors() {
// Skip errors from other files in the same package
if !strings.HasSuffix(er.FileName, filename) {
continue
}
errors = append(errors, er)
}
}

mPublishDiagnosticParams := make(map[string]*protocol.PublishDiagnosticsParams)
publishDiagnosticParams := make([]*protocol.PublishDiagnosticsParams, 0)
diagnostics := make([]protocol.Diagnostic, 0) // Init required for JSONRPC to send an empty array
for _, er := range errors {
if !strings.HasSuffix(file.URI.Filename(), er.FileName) {
continue
}
diagnostic := protocol.Diagnostic{
diagnostics = append(diagnostics, protocol.Diagnostic{
Range: *posToRange(er.Line, er.Span),
Severity: protocol.DiagnosticSeverityError,
Source: "gnopls",
Message: er.Msg,
Code: er.Tool,
}
if pdp, ok := mPublishDiagnosticParams[er.FileName]; ok {
pdp.Diagnostics = append(pdp.Diagnostics, diagnostic)
continue
}
publishDiagnosticParam := protocol.PublishDiagnosticsParams{
URI: file.URI,
Diagnostics: []protocol.Diagnostic{diagnostic},
}
publishDiagnosticParams = append(publishDiagnosticParams, &publishDiagnosticParam)
mPublishDiagnosticParams[er.FileName] = &publishDiagnosticParam
}

// Clean old diagnosed errors if no error found for current file
found := false
for _, er := range errors {
if strings.HasSuffix(er.FileName, filepath.Base(file.URI.Filename())) {
found = true
break
}
}
if !found {
publishDiagnosticParams = append(publishDiagnosticParams, &protocol.PublishDiagnosticsParams{
URI: file.URI,
Diagnostics: []protocol.Diagnostic{},
})
}

return conn.Notify(
ctx,
protocol.MethodTextDocumentPublishDiagnostics,
publishDiagnosticParams,
protocol.PublishDiagnosticsParams{
URI: file.URI,
Diagnostics: diagnostics,
},
)
}

0 comments on commit 864ad35

Please sign in to comment.