diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 5b9ef6f07..31b018de5 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -2059,16 +2059,16 @@ def _on_workspace_diagnostics_async( ) -> None: if reset_pending_response: self.workspace_diagnostics_pending_responses[identifier] = None - for diagnostic_report in response['items']: - uri = normalize_uri(diagnostic_report['uri']) - version = diagnostic_report['version'] + for report in response['items']: + uri = normalize_uri(report['uri']) + version = report['version'] # Skip if outdated if isinstance(version, int) and (session_buffer := self.get_session_buffer_for_uri_async(uri)) and \ version < session_buffer.last_synced_version: continue - self.diagnostics_result_ids[(uri, identifier)] = diagnostic_report.get('resultId') - if is_workspace_full_document_diagnostic_report(diagnostic_report): - self.handle_diagnostics_async(uri, identifier, version, diagnostic_report['items']) + self.diagnostics_result_ids[(uri, identifier)] = report.get('resultId') + diagnostics = report['items'] if is_workspace_full_document_diagnostic_report(report) else None + self.handle_diagnostics_async(uri, identifier, version, diagnostics) def _on_workspace_diagnostics_error_async(self, identifier: DiagnosticsIdentifier, error: ResponseError) -> None: if error['code'] == LSPErrorCodes.ServerCancelled: @@ -2209,7 +2209,11 @@ def on_text_document_publish_diagnostics(self, params: PublishDiagnosticsParams) self.handle_diagnostics_async(params['uri'], None, None, params['diagnostics']) def handle_diagnostics_async( - self, uri: DocumentUri, identifier: DiagnosticsIdentifier, version: int | None, diagnostics: list[Diagnostic] + self, + uri: DocumentUri, + identifier: DiagnosticsIdentifier, + version: int | None, + diagnostics: list[Diagnostic] | None ) -> None: mgr = self.manager() if not mgr: @@ -2218,8 +2222,12 @@ def handle_diagnostics_async( if isinstance(reason, str): debug("ignoring unsuitable diagnostics for", uri, "reason:", reason) return - self.diagnostics.set_diagnostics(uri, identifier, diagnostics) - mgr.on_diagnostics_updated() + if diagnostics is not None: + self.diagnostics.set_diagnostics(uri, identifier, diagnostics) + mgr.on_diagnostics_updated() + # Even if we received an UnchangedDocumentDiagnosticReport (represented by the diagnostics argument being None) + # we still have to redraw the diagnostic regions in the view to ensure they keep their original positions after + # a buffer change. if session_buffer := self.get_session_buffer_for_uri_async(uri): self._publish_diagnostics_to_session_buffer_async( session_buffer, self.diagnostics.get_diagnostics_for_uri(uri), version) diff --git a/plugin/session_buffer.py b/plugin/session_buffer.py index ed7452a90..ebf759c43 100644 --- a/plugin/session_buffer.py +++ b/plugin/session_buffer.py @@ -671,14 +671,14 @@ def _on_document_diagnostic_async( self._diagnostics_versions[identifier] = version self._document_diagnostic_pending_requests[identifier] = None self.session.diagnostics_result_ids[(self._last_known_uri, identifier)] = response.get('resultId') - if is_related_full_document_diagnostic_report(response): - self.session.handle_diagnostics_async(self._last_known_uri, identifier, version, response['items']) + diagnostics = response['items'] if is_related_full_document_diagnostic_report(response) else None + self.session.handle_diagnostics_async(self._last_known_uri, identifier, version, diagnostics) if related_documents := response.get('relatedDocuments'): - for uri, diagnostic_report in related_documents.items(): + for uri, report in related_documents.items(): uri = normalize_uri(uri) - self.session.diagnostics_result_ids[(uri, identifier)] = diagnostic_report.get('resultId') - if is_full_document_diagnostic_report(diagnostic_report): - self.session.handle_diagnostics_async(uri, identifier, None, diagnostic_report['items']) + self.session.diagnostics_result_ids[(uri, identifier)] = report.get('resultId') + diagnostics = report['items'] if is_full_document_diagnostic_report(report) else None + self.session.handle_diagnostics_async(uri, identifier, None, diagnostics) def _on_document_diagnostic_error_async( self, view: sublime.View, identifier: DiagnosticsIdentifier, version: int, error: ResponseError