Skip to content

Commit

Permalink
fix: cancel pending requests as early as possible (#2543)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl authored Nov 5, 2024
1 parent d8fe83c commit 1d2e995
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 7 additions & 5 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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:
...


Expand Down Expand Up @@ -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()

Expand Down
15 changes: 13 additions & 2 deletions plugin/session_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)}
Expand Down

0 comments on commit 1d2e995

Please sign in to comment.