forked from golang/tools
/
diagnostics.go
174 lines (159 loc) · 5.46 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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"
"github.com/jackie-feng/tools/internal/lsp/protocol"
"github.com/jackie-feng/tools/internal/lsp/source"
"github.com/jackie-feng/tools/internal/lsp/telemetry"
"github.com/jackie-feng/tools/internal/telemetry/log"
"github.com/jackie-feng/tools/internal/telemetry/trace"
)
func (s *Server) diagnose(snapshot source.Snapshot, fh source.FileHandle) error {
switch fh.Identity().Kind {
case source.Go:
go s.diagnoseFile(snapshot, fh)
case source.Mod:
go s.diagnoseSnapshot(snapshot)
}
return nil
}
func (s *Server) diagnoseSnapshot(snapshot source.Snapshot) {
ctx := snapshot.View().BackgroundContext()
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
defer done()
for _, id := range snapshot.WorkspacePackageIDs(ctx) {
ph, err := snapshot.PackageHandle(ctx, id)
if err != nil {
log.Error(ctx, "diagnoseSnapshot: no PackageHandle for workspace package", err, telemetry.Package.Of(id))
continue
}
if len(ph.CompiledGoFiles()) == 0 {
continue
}
// Find a file on which to call diagnostics.
uri := ph.CompiledGoFiles()[0].File().Identity().URI
fh, err := snapshot.GetFile(ctx, uri)
if err != nil {
continue
}
// Run diagnostics on the workspace package.
go func(snapshot source.Snapshot, fh source.FileHandle) {
reports, _, err := source.Diagnostics(ctx, snapshot, fh, false, snapshot.View().Options().DisabledAnalyses)
if err != nil {
log.Error(ctx, "no diagnostics", err, telemetry.URI.Of(fh.Identity().URI))
return
}
// Don't publish empty diagnostics.
s.publishReports(ctx, reports, false)
}(snapshot, fh)
}
}
func (s *Server) diagnoseFile(snapshot source.Snapshot, fh source.FileHandle) {
ctx := snapshot.View().BackgroundContext()
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
defer done()
ctx = telemetry.File.With(ctx, fh.Identity().URI)
reports, warningMsg, err := source.Diagnostics(ctx, snapshot, fh, true, snapshot.View().Options().DisabledAnalyses)
// Check the warning message first.
if warningMsg != "" {
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
Type: protocol.Info,
Message: warningMsg,
})
}
if err != nil {
if err != context.Canceled {
log.Error(ctx, "diagnoseFile: could not generate diagnostics", err)
}
return
}
// Publish empty diagnostics for files.
s.publishReports(ctx, reports, true)
}
func (s *Server) publishReports(ctx context.Context, reports map[source.FileIdentity][]source.Diagnostic, publishEmpty bool) {
// Check for context cancellation before publishing diagnostics.
if ctx.Err() != nil {
return
}
s.deliveredMu.Lock()
defer s.deliveredMu.Unlock()
for fileID, diagnostics := range reports {
// Don't deliver diagnostics if the context has already been canceled.
if ctx.Err() != nil {
break
}
// Don't publish empty diagnostics unless specified.
if len(diagnostics) == 0 && !publishEmpty {
continue
}
// Pre-sort diagnostics to avoid extra work when we compare them.
source.SortDiagnostics(diagnostics)
toSend := sentDiagnostics{
version: fileID.Version,
identifier: fileID.Identifier,
sorted: diagnostics,
}
if delivered, ok := s.delivered[fileID.URI]; ok {
// We only reuse cached diagnostics in two cases:
// 1. This file is at a greater version than that of the previously sent diagnostics.
// 2. There are no known versions for the file.
greaterVersion := fileID.Version > delivered.version && delivered.version > 0
noVersions := (fileID.Version == 0 && delivered.version == 0) && delivered.identifier == fileID.Identifier
if (greaterVersion || noVersions) && equalDiagnostics(delivered.sorted, diagnostics) {
// Update the delivered map even if we reuse cached diagnostics.
s.delivered[fileID.URI] = toSend
continue
}
}
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
Diagnostics: toProtocolDiagnostics(ctx, diagnostics),
URI: protocol.NewURI(fileID.URI),
Version: fileID.Version,
}); err != nil {
log.Error(ctx, "failed to deliver diagnostic", err, telemetry.File)
continue
}
// Update the delivered map.
s.delivered[fileID.URI] = toSend
}
}
// equalDiagnostics returns true if the 2 lists of diagnostics are equal.
// It assumes that both a and b are already sorted.
func equalDiagnostics(a, b []source.Diagnostic) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if source.CompareDiagnostic(a[i], b[i]) != 0 {
return false
}
}
return true
}
func toProtocolDiagnostics(ctx context.Context, diagnostics []source.Diagnostic) []protocol.Diagnostic {
reports := []protocol.Diagnostic{}
for _, diag := range diagnostics {
related := make([]protocol.DiagnosticRelatedInformation, 0, len(diag.Related))
for _, rel := range diag.Related {
related = append(related, protocol.DiagnosticRelatedInformation{
Location: protocol.Location{
URI: protocol.NewURI(rel.URI),
Range: rel.Range,
},
Message: rel.Message,
})
}
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,
RelatedInformation: related,
})
}
return reports
}