From 1d2e995983491d073997315299848f128c76968e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Tue, 5 Nov 2024 09:40:25 +0100 Subject: [PATCH] fix: cancel pending requests as early as possible (#2543) --- plugin/core/sessions.py | 12 +++++++----- plugin/session_buffer.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index beabce998..b4392348d 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -671,7 +671,7 @@ def update_document_link(self, new_link: DocumentLink) -> None: def do_semantic_tokens_async(self, view: sublime.View) -> None: ... - def set_semantic_tokens_pending_refresh(self, needs_refresh: bool = True) -> None: + def set_semantic_tokens_pending_refresh(self, needs_refresh: bool = ...) -> None: ... def get_semantic_tokens(self) -> list[Any]: @@ -680,16 +680,18 @@ def get_semantic_tokens(self) -> list[Any]: def do_inlay_hints_async(self, view: sublime.View) -> None: ... - def set_inlay_hints_pending_refresh(self, needs_refresh: bool = True) -> None: + def set_inlay_hints_pending_refresh(self, needs_refresh: bool = ...) -> None: ... def remove_inlay_hint_phantom(self, phantom_uuid: str) -> None: ... - def do_document_diagnostic_async(self, view: sublime.View, version: int | None = None) -> None: + def do_document_diagnostic_async( + self, view: sublime.View, version: int | None = ..., *, forced_update: bool = ... + ) -> None: ... - def set_document_diagnostic_pending_refresh(self, needs_refresh: bool = True) -> None: + def set_document_diagnostic_pending_refresh(self, needs_refresh: bool = ...) -> None: ... @@ -2070,7 +2072,7 @@ def m_workspace_diagnostic_refresh(self, params: None, request_id: Any) -> None: self.send_response(Response(request_id, None)) visible_session_views, not_visible_session_views = self.session_views_by_visibility() for sv in visible_session_views: - sv.session_buffer.do_document_diagnostic_async(sv.view) + sv.session_buffer.do_document_diagnostic_async(sv.view, forced_update=True) for sv in not_visible_session_views: sv.session_buffer.set_document_diagnostic_pending_refresh() diff --git a/plugin/session_buffer.py b/plugin/session_buffer.py index 28b7e6b96..aaa21fb60 100644 --- a/plugin/session_buffer.py +++ b/plugin/session_buffer.py @@ -311,9 +311,18 @@ def on_text_changed_async(self, view: sublime.View, change_count: int, self._pending_changes.update(change_count, changes) purge = True if purge: + self._cancel_pending_requests_async() debounced(lambda: self.purge_changes_async(view), FEATURES_TIMEOUT, lambda: view.is_valid() and change_count == view.change_count(), async_thread=True) + def _cancel_pending_requests_async(self) -> None: + if self._document_diagnostic_pending_request: + self.session.cancel_request(self._document_diagnostic_pending_request.request_id) + self._document_diagnostic_pending_request = None + if self.semantic_tokens.pending_response: + self.session.cancel_request(self.semantic_tokens.pending_response) + self.semantic_tokens.pending_response = None + def on_revert_async(self, view: sublime.View) -> None: self._pending_changes = None # Don't bother with pending changes version = view.change_count() @@ -478,7 +487,9 @@ def update_document_link(self, new_link: DocumentLink) -> None: # --- textDocument/diagnostic -------------------------------------------------------------------------------------- - def do_document_diagnostic_async(self, view: sublime.View, version: int | None = None) -> None: + def do_document_diagnostic_async( + self, view: sublime.View, version: int | None = None, forced_update: bool = False + ) -> None: mgr = self.session.manager() if not mgr or not self.has_capability("diagnosticProvider"): return @@ -487,7 +498,7 @@ def do_document_diagnostic_async(self, view: sublime.View, version: int | None = if version is None: version = view.change_count() if self._document_diagnostic_pending_request: - if self._document_diagnostic_pending_request.version == version: + if self._document_diagnostic_pending_request.version == version and not forced_update: return self.session.cancel_request(self._document_diagnostic_pending_request.request_id) params: DocumentDiagnosticParams = {'textDocument': text_document_identifier(view)}