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

feat: add fallback_kinds to lsp_code_actions #2548

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
// "args": {"only_kinds": ["source"]},
// "context": [{"key": "lsp.session_with_capability", "operand": "codeActionProvider.codeActionKinds"}]
// },
// Run Code Actions with Source Actions fallback
// {
// "keys": ["UNBOUND"],
// "command": "lsp_code_actions",
// "args": {"fallback_kinds": ["source"]},
// "context": [{"key": "lsp.session_with_capability", "operand": "codeActionProvider.codeActionKinds"}]
// },
// Run Code Lens
// {
// "keys": ["UNBOUND"],
Expand Down
24 changes: 19 additions & 5 deletions plugin/code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,19 @@ def run(
edit: sublime.Edit,
event: dict | None = None,
only_kinds: list[CodeActionKind] | None = None,
code_actions_by_config: list[CodeActionsByConfigName] | None = None
code_actions_by_config: list[CodeActionsByConfigName] | None = None,
fallback_kinds: list[CodeActionKind] | None = None
) -> None:
if code_actions_by_config:
self._handle_code_actions(code_actions_by_config, run_first=True)
return
self._run_async(only_kinds)
self._run_async(only_kinds, fallback_kinds)

def _run_async(self, only_kinds: list[CodeActionKind] | None = None) -> None:
def _run_async(
self,
only_kinds: list[CodeActionKind] | None = None,
fallback_kinds: list[CodeActionKind] | None = None
) -> None:
view = self.view
region = first_selection_region(view)
if region is None:
Expand All @@ -309,9 +314,16 @@ def _run_async(self, only_kinds: list[CodeActionKind] | None = None) -> None:
session_buffer_diagnostics, covering = listener.diagnostics_intersecting_async(region)
actions_manager \
.request_for_region_async(view, covering, session_buffer_diagnostics, only_kinds, manual=True) \
.then(lambda actions: sublime.set_timeout(lambda: self._handle_code_actions(actions)))
.then(lambda actions: sublime.set_timeout(
lambda: self._handle_code_actions(actions, run_first=False, fallback_kinds=fallback_kinds))
)

def _handle_code_actions(self, response: list[CodeActionsByConfigName], run_first: bool = False) -> None:
def _handle_code_actions(
self,
response: list[CodeActionsByConfigName],
run_first: bool = False,
fallback_kinds: list[CodeActionKind] | None = None,
) -> None:
# Flatten response to a list of (config_name, code_action) tuples.
actions: list[tuple[ConfigName, CodeActionOrCommand]] = []
for config_name, session_actions in response:
Expand All @@ -321,6 +333,8 @@ def _handle_code_actions(self, response: list[CodeActionsByConfigName], run_firs
self._handle_select(0, actions)
else:
self._show_code_actions(actions)
elif fallback_kinds:
self._run_async(fallback_kinds)
else:
window = self.view.window()
if window:
Expand Down