2018-11-05 12:48:08 -07:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-10-19 14:03:29 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
2018-12-05 15:00:36 -07:00
|
|
|
"context"
|
2019-05-17 11:45:50 -06:00
|
|
|
"strings"
|
2018-11-13 09:13:53 -07:00
|
|
|
|
2018-10-19 14:03:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2018-11-02 14:15:31 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2018-10-19 14:03:29 -06:00
|
|
|
)
|
|
|
|
|
2019-06-21 15:00:02 -06:00
|
|
|
func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI) {
|
2019-07-14 21:08:10 -06:00
|
|
|
ctx = telemetry.File.With(ctx, uri)
|
2019-06-21 15:00:02 -06:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
2019-05-01 20:46:07 -06:00
|
|
|
if err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "no file", err, telemetry.File)
|
2019-05-23 13:03:11 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// For non-Go files, don't return any diagnostics.
|
|
|
|
gof, ok := f.(source.GoFile)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-09-05 22:17:36 -06:00
|
|
|
reports, err := source.Diagnostics(ctx, view, gof, s.session.Options().DisabledAnalyses)
|
2019-05-23 13:03:11 -06:00
|
|
|
if err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "failed to compute diagnostics", err, telemetry.File)
|
2019-05-01 20:46:07 -06:00
|
|
|
return
|
|
|
|
}
|
2019-03-14 15:19:01 -06:00
|
|
|
|
2019-05-01 20:46:07 -06:00
|
|
|
s.undeliveredMu.Lock()
|
|
|
|
defer s.undeliveredMu.Unlock()
|
2019-03-14 15:19:01 -06:00
|
|
|
|
2019-05-01 20:46:07 -06:00
|
|
|
for uri, diagnostics := range reports {
|
2019-08-16 11:49:17 -06:00
|
|
|
if err := s.publishDiagnostics(ctx, uri, diagnostics); err != nil {
|
2019-05-01 20:46:07 -06:00
|
|
|
if s.undelivered == nil {
|
|
|
|
s.undelivered = make(map[span.URI][]source.Diagnostic)
|
2019-03-14 14:55:23 -06:00
|
|
|
}
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "failed to deliver diagnostic (will retry)", err, telemetry.File)
|
2019-05-01 20:46:07 -06:00
|
|
|
s.undelivered[uri] = diagnostics
|
|
|
|
continue
|
2019-03-14 15:19:01 -06:00
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
// In case we had old, undelivered diagnostics.
|
|
|
|
delete(s.undelivered, uri)
|
|
|
|
}
|
|
|
|
// Anytime we compute diagnostics, make sure to also send along any
|
|
|
|
// undelivered ones (only for remaining URIs).
|
|
|
|
for uri, diagnostics := range s.undelivered {
|
2019-08-16 11:49:17 -06:00
|
|
|
if err := s.publishDiagnostics(ctx, uri, diagnostics); err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "failed to deliver diagnostic for (will not retry)", err, telemetry.File)
|
2019-05-17 11:45:50 -06:00
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
// If we fail to deliver the same diagnostics twice, just give up.
|
|
|
|
delete(s.undelivered, uri)
|
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:49:17 -06:00
|
|
|
func (s *Server) publishDiagnostics(ctx context.Context, uri span.URI, diagnostics []source.Diagnostic) error {
|
2019-08-14 18:12:18 -06:00
|
|
|
protocolDiagnostics, err := toProtocolDiagnostics(ctx, diagnostics)
|
2019-03-14 15:19:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
|
|
Diagnostics: protocolDiagnostics,
|
|
|
|
URI: protocol.NewURI(uri),
|
|
|
|
})
|
|
|
|
return nil
|
2018-12-18 14:18:03 -07:00
|
|
|
}
|
|
|
|
|
2019-08-14 18:12:18 -06:00
|
|
|
func toProtocolDiagnostics(ctx context.Context, diagnostics []source.Diagnostic) ([]protocol.Diagnostic, error) {
|
2018-11-12 12:15:47 -07:00
|
|
|
reports := []protocol.Diagnostic{}
|
|
|
|
for _, diag := range diagnostics {
|
2019-08-14 18:12:18 -06:00
|
|
|
diagnostic, err := toProtocolDiagnostic(ctx, diag)
|
2019-02-19 19:11:15 -07:00
|
|
|
if err != nil {
|
2019-03-14 14:55:23 -06:00
|
|
|
return nil, err
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
2019-06-20 14:57:45 -06:00
|
|
|
reports = append(reports, diagnostic)
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-03-14 14:55:23 -06:00
|
|
|
return reports, nil
|
2018-10-29 16:12:41 -06:00
|
|
|
}
|
2019-06-20 14:57:45 -06:00
|
|
|
|
2019-08-14 18:12:18 -06:00
|
|
|
func toProtocolDiagnostic(ctx context.Context, diag source.Diagnostic) (protocol.Diagnostic, error) {
|
2019-06-20 14:57:45 -06:00
|
|
|
var severity protocol.DiagnosticSeverity
|
|
|
|
switch diag.Severity {
|
|
|
|
case source.SeverityError:
|
|
|
|
severity = protocol.SeverityError
|
|
|
|
case source.SeverityWarning:
|
|
|
|
severity = protocol.SeverityWarning
|
|
|
|
}
|
|
|
|
return protocol.Diagnostic{
|
|
|
|
Message: strings.TrimSpace(diag.Message), // go list returns errors prefixed by newline
|
2019-08-14 18:12:18 -06:00
|
|
|
Range: diag.Range,
|
2019-06-20 14:57:45 -06:00
|
|
|
Severity: severity,
|
|
|
|
Source: diag.Source,
|
|
|
|
}, nil
|
|
|
|
}
|