From 29e34081ee08609ebf09949acbbde3b5f3bfc8b5 Mon Sep 17 00:00:00 2001 From: fetchTe Date: Wed, 6 Nov 2024 14:03:00 -0600 Subject: [PATCH] feat: add fallback_kinds to lsp_code_actions An option to specify a fallback_kinds if no only_kinds/default match --- Default.sublime-keymap | 7 +++++++ plugin/code_actions.py | 24 +++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Default.sublime-keymap b/Default.sublime-keymap index 325077470..fa358dfa7 100644 --- a/Default.sublime-keymap +++ b/Default.sublime-keymap @@ -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"], diff --git a/plugin/code_actions.py b/plugin/code_actions.py index 2056a866f..d9b4b74ec 100644 --- a/plugin/code_actions.py +++ b/plugin/code_actions.py @@ -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: @@ -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: @@ -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: