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: code actions on save not fixing issues if saving quickly #2540

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions plugin/code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def request_factory(sb: SessionBufferProtocol) -> Request | None:
if diag_sb == sb:
diagnostics = diags
break
# Pull for diagnostics to ensure that server computes them before receiving code action request.
sb.do_document_diagnostic_async(view)
Copy link
Member

Choose a reason for hiding this comment

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

I probably would have put this part under if request: in _collect_code_actions_async (line 165), because it's not strictly necessary for creating the Request object.

Copy link
Member Author

Choose a reason for hiding this comment

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

makes sense

Ideally perhaps this should be trigger before any code action request. While normal usage should not require that, if someone uses a macro or some sort of automation then it could become a problem then too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually this code is used for any code action request so all good.

params = text_document_code_action_params(view, region, diagnostics, matching_kinds, manual=False)
return Request.codeAction(params, view)

Expand Down
49 changes: 30 additions & 19 deletions plugin/session_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ def update(self, version: int, changes: Iterable[sublime.TextChange]) -> None:
self.changes.extend(changes)


class PendingDocumentDiagnosticRequest:

__slots__ = ('version', 'request_id')

def __init__(self, version: int, request_id: int) -> None:
self.version = version
self.request_id = request_id


class SemanticTokensData:

__slots__ = (
Expand Down Expand Up @@ -122,7 +131,7 @@ def __init__(self, session_view: SessionViewProtocol, buffer_id: int, uri: Docum
self.diagnostics_flags = 0
self._diagnostics_are_visible = False
self.document_diagnostic_needs_refresh = False
self._document_diagnostic_pending_response: int | None = None
self._document_diagnostic_pending_request: PendingDocumentDiagnosticRequest | None = None
self._last_synced_version = 0
self._last_text_change_time = 0.0
self._diagnostics_debouncer_async = DebouncerNonThreadSafe(async_thread=True)
Expand Down Expand Up @@ -471,30 +480,32 @@ def update_document_link(self, new_link: DocumentLink) -> None:

def do_document_diagnostic_async(self, view: sublime.View, version: int | None = None) -> None:
mgr = self.session.manager()
if not mgr:
if not mgr or not self.has_capability("diagnosticProvider"):
return
if mgr.should_ignore_diagnostics(self._last_known_uri, self.session.config):
return
if version is None:
version = view.change_count()
if self.has_capability("diagnosticProvider"):
if self._document_diagnostic_pending_response:
self.session.cancel_request(self._document_diagnostic_pending_response)
params: DocumentDiagnosticParams = {'textDocument': text_document_identifier(view)}
identifier = self.get_capability("diagnosticProvider.identifier")
if identifier:
params['identifier'] = identifier
result_id = self.session.diagnostics_result_ids.get(self._last_known_uri)
if result_id is not None:
params['previousResultId'] = result_id
self._document_diagnostic_pending_response = self.session.send_request_async(
Request.documentDiagnostic(params, view),
partial(self._on_document_diagnostic_async, version),
partial(self._on_document_diagnostic_error_async, version)
)
if self._document_diagnostic_pending_request:
if self._document_diagnostic_pending_request.version == version:
return
self.session.cancel_request(self._document_diagnostic_pending_request.request_id)
params: DocumentDiagnosticParams = {'textDocument': text_document_identifier(view)}
identifier = self.get_capability("diagnosticProvider.identifier")
if identifier:
params['identifier'] = identifier
result_id = self.session.diagnostics_result_ids.get(self._last_known_uri)
if result_id is not None:
params['previousResultId'] = result_id
request_id = self.session.send_request_async(
Request.documentDiagnostic(params, view),
partial(self._on_document_diagnostic_async, version),
partial(self._on_document_diagnostic_error_async, version)
)
self._document_diagnostic_pending_request = PendingDocumentDiagnosticRequest(version, request_id)

Comment on lines 481 to 506
Copy link
Member Author

@rchl rchl Oct 30, 2024

Choose a reason for hiding this comment

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

Moved check for self.has_capability("diagnosticProvider") to the top which changed indentation quite a bit and made it harder to review. Enable "hide whitespace changes" to make it easier to read.

Apart from that, added a check and bail for the case when requesting diagnostics for document version for which there already is a pending request. Without the added check it would cancel existing and trigger a new one which is a waste if the document hasn't changed since.

def _on_document_diagnostic_async(self, version: int, response: DocumentDiagnosticReport) -> None:
self._document_diagnostic_pending_response = None
self._document_diagnostic_pending_request = None
self._if_view_unchanged(self._apply_document_diagnostic_async, version)(response)

def _apply_document_diagnostic_async(
Expand All @@ -511,7 +522,7 @@ def _apply_document_diagnostic_async(
None, cast(DocumentDiagnosticReport, diagnostic_report))

def _on_document_diagnostic_error_async(self, version: int, error: ResponseError) -> None:
self._document_diagnostic_pending_response = None
self._document_diagnostic_pending_request = None
if error['code'] == LSPErrorCodes.ServerCancelled:
data = error.get('data')
if is_diagnostic_server_cancellation_data(data) and data['retriggerRequest']:
Expand Down