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

Only do a single pass on running code actions on save #2283

Merged
merged 2 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 1 addition & 9 deletions plugin/code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,12 @@ def _request_code_actions_async(self) -> None:
def _handle_response_async(self, responses: List[CodeActionsByConfigName]) -> None:
if self._cancelled:
return
document_version = self._task_runner.view.change_count()
tasks = [] # type: List[Promise]
for config_name, code_actions in responses:
session = self._task_runner.session_by_name(config_name, 'codeActionProvider')
if session:
tasks.extend([session.run_code_action_async(action, progress=False) for action in code_actions])
Promise.all(tasks).then(lambda _: self._on_code_actions_completed(document_version))

def _on_code_actions_completed(self, previous_document_version: int) -> None:
if previous_document_version != self._task_runner.view.change_count():
# Give on_text_changed_async a chance to trigger.
sublime.set_timeout_async(self._request_code_actions_async)
else:
self._on_complete()
Promise.all(tasks).then(lambda _: self._on_complete())


LspSaveCommand.register_task(CodeActionOnSaveTask)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_requests_with_diagnostics(self) -> Generator:
self.assertEquals(entire_content(self.view), 'const x = 1;')
self.assertEquals(self.view.is_dirty(), False)

def test_applies_in_two_iterations(self) -> Generator:
def test_applies_only_one_pass(self) -> Generator:
self.insert_characters('const x = 1')
initial_change_count = self.view.change_count()
yield from self.await_client_notification(
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_applies_in_two_iterations(self) -> Generator:
self.view.run_command('lsp_save')
# Wait for the view to be saved
yield lambda: not self.view.is_dirty()
self.assertEquals(entire_content(self.view), 'const x = 1;\nAnd again!')
self.assertEquals(entire_content(self.view), 'const x = 1;')

def test_applies_immediately_after_text_change(self) -> Generator:
self.insert_characters('const x = 1')
Expand Down