Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change publish diagnostics to return params for a single file #2

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
)
}
Loading