mirror of
https://github.com/golang/go
synced 2024-11-19 00:24:41 -07:00
3cbd95df51
This change adds supports for a package belonging to multiple files. It requires additional packages.Loads for all of the packages to which a file belongs (for example, if a non-test file also belongs to a package's test variant). For now, we re-run go/packages.Load for each file we open, regardless of whether or not we already know about it. This solves the issue of packages randomly belonging to a test or not. Follow-up work needs to be done to support multiple packages in references, rename, and diagnostics. Fixes golang/go#32791 Fixes golang/go#30100 Change-Id: I0a5870a05825fc16cc46d405ef50c775094b0fbb Reviewed-on: https://go-review.googlesource.com/c/tools/+/183628 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
98 lines
2.9 KiB
Go
98 lines
2.9 KiB
Go
// 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.
|
|
|
|
package lsp
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI) {
|
|
f, err := view.GetFile(ctx, uri)
|
|
if err != nil {
|
|
s.session.Logger().Errorf(ctx, "no file for %s: %v", uri, err)
|
|
return
|
|
}
|
|
// For non-Go files, don't return any diagnostics.
|
|
gof, ok := f.(source.GoFile)
|
|
if !ok {
|
|
return
|
|
}
|
|
reports, err := source.Diagnostics(ctx, view, gof, s.disabledAnalyses)
|
|
if err != nil {
|
|
s.session.Logger().Errorf(ctx, "failed to compute diagnostics for %s: %v", gof.URI(), err)
|
|
return
|
|
}
|
|
|
|
s.undeliveredMu.Lock()
|
|
defer s.undeliveredMu.Unlock()
|
|
|
|
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)
|
|
}
|
|
s.session.Logger().Errorf(ctx, "failed to deliver diagnostic for %s (will retry): %v", uri, err)
|
|
s.undelivered[uri] = diagnostics
|
|
continue
|
|
}
|
|
// 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 {
|
|
if err := s.publishDiagnostics(ctx, view, uri, diagnostics); err != nil {
|
|
s.session.Logger().Errorf(ctx, "failed to deliver diagnostic for %s (will not retry): %v", uri, err)
|
|
}
|
|
// If we fail to deliver the same diagnostics twice, just give up.
|
|
delete(s.undelivered, uri)
|
|
}
|
|
}
|
|
|
|
func (s *Server) publishDiagnostics(ctx context.Context, view source.View, uri span.URI, diagnostics []source.Diagnostic) error {
|
|
protocolDiagnostics, err := toProtocolDiagnostics(ctx, view, diagnostics)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
Diagnostics: protocolDiagnostics,
|
|
URI: protocol.NewURI(uri),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []source.Diagnostic) ([]protocol.Diagnostic, error) {
|
|
reports := []protocol.Diagnostic{}
|
|
for _, diag := range diagnostics {
|
|
_, m, err := getSourceFile(ctx, v, diag.Span.URI())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var severity protocol.DiagnosticSeverity
|
|
switch diag.Severity {
|
|
case source.SeverityError:
|
|
severity = protocol.SeverityError
|
|
case source.SeverityWarning:
|
|
severity = protocol.SeverityWarning
|
|
}
|
|
rng, err := m.Range(diag.Span)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reports = append(reports, protocol.Diagnostic{
|
|
Message: strings.TrimSpace(diag.Message), // go list returns errors prefixed by newline
|
|
Range: rng,
|
|
Severity: severity,
|
|
Source: diag.Source,
|
|
})
|
|
}
|
|
return reports, nil
|
|
}
|