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
|
|
|
|
2019-03-29 14:05:59 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) cacheAndDiagnose(ctx context.Context, uri span.URI, content string) error {
|
2019-03-29 14:05:59 -06:00
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
if err := view.SetContent(ctx, uri, []byte(content)); err != nil {
|
2019-02-19 19:11:15 -07:00
|
|
|
return err
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
go func() {
|
2019-03-29 14:05:59 -06:00
|
|
|
ctx := view.BackgroundContext()
|
2019-03-05 15:30:44 -07:00
|
|
|
if ctx.Err() != nil {
|
2019-04-16 10:58:00 -06:00
|
|
|
s.log.Errorf(ctx, "canceling diagnostics for %s: %v", uri, ctx.Err())
|
2019-03-05 15:30:44 -07:00
|
|
|
return
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
reports, err := source.Diagnostics(ctx, view, uri)
|
2018-12-05 15:00:36 -07:00
|
|
|
if err != nil {
|
2019-04-16 10:58:00 -06:00
|
|
|
s.log.Errorf(ctx, "failed to compute diagnostics for %s: %v", uri, err)
|
|
|
|
return
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-03-14 15:19:01 -06:00
|
|
|
|
|
|
|
s.undeliveredMu.Lock()
|
|
|
|
defer s.undeliveredMu.Unlock()
|
|
|
|
|
2019-02-19 19:11:15 -07:00
|
|
|
for uri, diagnostics := range reports {
|
2019-03-29 14:05:59 -06:00
|
|
|
if err := s.publishDiagnostics(ctx, view, uri, diagnostics); err != nil {
|
2019-04-01 13:11:03 -06:00
|
|
|
if s.undelivered == nil {
|
|
|
|
s.undelivered = make(map[span.URI][]source.Diagnostic)
|
|
|
|
}
|
2019-03-14 15:19:01 -06:00
|
|
|
s.undelivered[uri] = diagnostics
|
|
|
|
continue
|
2019-03-14 14:55:23 -06:00
|
|
|
}
|
2019-03-14 15:19:01 -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-03-29 14:05:59 -06:00
|
|
|
s.publishDiagnostics(ctx, view, uri, diagnostics)
|
2019-03-14 15:19:01 -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-02-19 19:11:15 -07:00
|
|
|
return nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:05:59 -06:00
|
|
|
func (s *Server) publishDiagnostics(ctx context.Context, view *cache.View, uri span.URI, diagnostics []source.Diagnostic) error {
|
|
|
|
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
|
|
|
}
|