Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cancel pending requests as early as possible #2543

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = ..., *, ignore_pending: 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, ignore_pending=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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an urge to rename pending_response to pending_request.

pending_response sounds like a boolean state (waiting for response) rather than what it actually is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was that the request has already happened (was sent), so we're indeed only still waiting for the response now. But maybe we use the word "pending" in a sligthly different meaning. I guess it can both mean something like "upcoming" (that's how I used it here), or "unanswered". Of course the server response is directly coupled to the request, so feel free to rename it if you like to.

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, ignore_pending: 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 ignore_pending:
return
self.session.cancel_request(self._document_diagnostic_pending_request.request_id)
params: DocumentDiagnosticParams = {'textDocument': text_document_identifier(view)}
Expand Down