Skip to content

Commit 4877332

Browse files
committed
internal/lsp: don't send diagnostics for old file versions
This isn't a strictly necessary change, but it simplifies things for open files when the initial workspace load diagnostics come in too late. Updates golang/go#36452 Change-Id: I85a9c201876b7c63a97d8dca020fc396b3c57706 Reviewed-on: https://go-review.googlesource.com/c/tools/+/213820 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 675cf51 commit 4877332

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

internal/lsp/diagnostics.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,22 @@ func (s *Server) publishReports(ctx context.Context, reports map[source.FileIden
130130
sorted: diagnostics,
131131
}
132132
delivered, ok := s.delivered[fileID.URI]
133+
// If diagnostics are empty and not previously delivered,
134+
// only send them if we are publishing empty diagnostics.
135+
if !ok && len(diagnostics) == 0 && !publishEmpty {
136+
// Update the delivered map to cache the diagnostics.
137+
s.delivered[fileID.URI] = toSend
138+
continue
139+
}
133140
// Reuse equivalent cached diagnostics for subsequent file versions (if known),
134141
// or identical files (if versions are not known).
135142
if ok {
143+
// If the file is open, and we've already delivered diagnostics for
144+
// a later version, do nothing. This only works for open files,
145+
// since their contents in the editor are the source of truth.
146+
if s.session.IsOpen(fileID.URI) && fileID.Version < delivered.version {
147+
continue
148+
}
136149
geqVersion := fileID.Version >= delivered.version && delivered.version > 0
137150
noVersions := (fileID.Version == 0 && delivered.version == 0) && delivered.identifier == fileID.Identifier
138151
if (geqVersion || noVersions) && equalDiagnostics(delivered.sorted, diagnostics) {
@@ -141,13 +154,7 @@ func (s *Server) publishReports(ctx context.Context, reports map[source.FileIden
141154
continue
142155
}
143156
}
144-
// If diagnostics are empty and not previously delivered,
145-
// only send them if we are publishing empty diagnostics.
146-
if !ok && len(diagnostics) == 0 && !publishEmpty {
147-
// Update the delivered map to cache the diagnostics.
148-
s.delivered[fileID.URI] = toSend
149-
continue
150-
}
157+
151158
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
152159
Diagnostics: toProtocolDiagnostics(ctx, diagnostics),
153160
URI: protocol.NewURI(fileID.URI),

0 commit comments

Comments
 (0)