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"
|
2019-09-17 12:36:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2018-10-19 14:03:29 -06:00
|
|
|
)
|
|
|
|
|
2019-09-17 12:36:39 -06:00
|
|
|
func (s *Server) diagnostics(view source.View, uri span.URI) error {
|
|
|
|
ctx := view.BackgroundContext()
|
|
|
|
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
|
|
|
|
defer done()
|
|
|
|
|
2019-07-14 21:08:10 -06:00
|
|
|
ctx = telemetry.File.With(ctx, uri)
|
2019-09-17 09:19:11 -06:00
|
|
|
|
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-09-17 12:36:39 -06:00
|
|
|
return err
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
reports, warningMsg, err := source.Diagnostics(ctx, view, f, view.Options().DisabledAnalyses)
|
2019-05-23 13:03:11 -06:00
|
|
|
if err != nil {
|
2019-09-17 12:36:39 -06:00
|
|
|
return err
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
2019-09-06 16:25:36 -06:00
|
|
|
if warningMsg != "" {
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Info,
|
|
|
|
Message: warningMsg,
|
|
|
|
})
|
|
|
|
}
|
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-05-01 20:46:07 -06:00
|
|
|
s.undelivered[uri] = diagnostics
|
2019-09-17 09:19:11 -06:00
|
|
|
|
|
|
|
log.Error(ctx, "failed to deliver diagnostic (will retry)", err, telemetry.File)
|
2019-05-01 20:46:07 -06:00
|
|
|
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-09-17 09:19:11 -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)
|
|
|
|
}
|
2019-09-17 12:36:39 -06:00
|
|
|
return nil
|
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-03-14 15:19:01 -06:00
|
|
|
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
2019-09-24 14:28:59 -06:00
|
|
|
Diagnostics: toProtocolDiagnostics(ctx, diagnostics),
|
2019-03-14 15:19:01 -06:00
|
|
|
URI: protocol.NewURI(uri),
|
|
|
|
})
|
|
|
|
return nil
|
2018-12-18 14:18:03 -07:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:28:59 -06:00
|
|
|
func toProtocolDiagnostics(ctx context.Context, diagnostics []source.Diagnostic) []protocol.Diagnostic {
|
2018-11-12 12:15:47 -07:00
|
|
|
reports := []protocol.Diagnostic{}
|
|
|
|
for _, diag := range diagnostics {
|
2019-09-24 22:46:57 -06:00
|
|
|
reports = append(reports, protocol.Diagnostic{
|
|
|
|
Message: strings.TrimSpace(diag.Message), // go list returns errors prefixed by newline
|
|
|
|
Range: diag.Range,
|
|
|
|
Severity: diag.Severity,
|
|
|
|
Source: diag.Source,
|
|
|
|
Tags: diag.Tags,
|
|
|
|
})
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
return reports
|
2018-10-29 16:12:41 -06:00
|
|
|
}
|