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"
|
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-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-10-19 14:03:29 -06:00
|
|
|
)
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI) {
|
2019-05-01 20:46:07 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
s.log.Errorf(ctx, "canceling diagnostics for %s: %v", uri, ctx.Err())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reports, err := source.Diagnostics(ctx, view, uri)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Errorf(ctx, "failed to compute diagnostics for %s: %v", uri, err)
|
|
|
|
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 {
|
|
|
|
if err := s.publishDiagnostics(ctx, view, uri, diagnostics); err != nil {
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
s.publishDiagnostics(ctx, view, uri, diagnostics)
|
2019-03-14 15:19:01 -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-05-14 21:04:23 -06:00
|
|
|
func (s *Server) publishDiagnostics(ctx context.Context, view source.View, uri span.URI, diagnostics []source.Diagnostic) error {
|
2019-03-29 14:05:59 -06:00
|
|
|
protocolDiagnostics, err := toProtocolDiagnostics(ctx, view, 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-03-14 14:55:23 -06:00
|
|
|
func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []source.Diagnostic) ([]protocol.Diagnostic, error) {
|
2018-11-12 12:15:47 -07:00
|
|
|
reports := []protocol.Diagnostic{}
|
|
|
|
for _, diag := range diagnostics {
|
2019-03-15 11:19:43 -06:00
|
|
|
_, m, err := newColumnMap(ctx, v, diag.Span.URI())
|
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-02-27 16:08:04 -07:00
|
|
|
var severity protocol.DiagnosticSeverity
|
|
|
|
switch diag.Severity {
|
|
|
|
case source.SeverityError:
|
|
|
|
severity = protocol.SeverityError
|
|
|
|
case source.SeverityWarning:
|
|
|
|
severity = protocol.SeverityWarning
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
rng, err := m.Range(diag.Span)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
reports = append(reports, protocol.Diagnostic{
|
2018-12-18 13:46:14 -07:00
|
|
|
Message: diag.Message,
|
2019-03-15 11:19:43 -06:00
|
|
|
Range: rng,
|
2019-02-27 16:08:04 -07:00
|
|
|
Severity: severity,
|
2019-04-16 18:27:09 -06:00
|
|
|
Source: diag.Source,
|
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
|
|
|
}
|