From 2bf36ca61c553c26562e37be08e09057c775a829 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Mon, 18 Jan 2021 20:54:36 +0100 Subject: [PATCH] Handle basic editor commands (#1545) * editor.action.triggerSuggest (auto-complete) * editor.action.triggerParameterHints (signature help) --- plugin/core/windows.py | 4 ++++ plugin/documents.py | 8 ++++---- plugin/execute_command.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/plugin/core/windows.py b/plugin/core/windows.py index d4802eb64..895168161 100644 --- a/plugin/core/windows.py +++ b/plugin/core/windows.py @@ -110,6 +110,10 @@ def session_views_async(self) -> Iterable[SessionViewProtocol]: def get_language_id(self) -> str: raise NotImplementedError() + @abstractmethod + def do_signature_help_async(self, manual: bool) -> None: + raise NotImplementedError() + def extract_message(params: Any) -> str: return params.get("message", "???") if isinstance(params, dict) else "???" diff --git a/plugin/documents.py b/plugin/documents.py index a6c8b12fb..91a236d0e 100644 --- a/plugin/documents.py +++ b/plugin/documents.py @@ -272,7 +272,7 @@ def on_text_changed_async(self, change_count: int, changes: Iterable[sublime.Tex self._when_selection_remains_stable_async(self._do_color_boxes_async, current_region, after_ms=self.color_boxes_debounce_time) if "signatureHelp" not in userprefs().disabled_capabilities: - self._do_signature_help(manual=False) + self.do_signature_help_async(manual=False) if "codeLensProvider" not in userprefs().disabled_capabilities: self._when_selection_remains_stable_async(self._do_code_lenses_async, current_region, after_ms=self.code_lenses_debounce_time) @@ -330,7 +330,7 @@ def on_query_context(self, key: str, operator: int, operand: Any, match_all: boo elif key == "lsp.signature_help": if not self.view.is_popup_visible(): if operand == 0: - sublime.set_timeout_async(lambda: self._do_signature_help(manual=True)) + sublime.set_timeout_async(lambda: self.do_signature_help_async(manual=True)) return True elif self._sighelp and self._sighelp.has_multiple_signatures() and not self.view.is_auto_complete_visible(): # We use the "operand" for the number -1 or +1. See the keybindings. @@ -349,7 +349,7 @@ def on_hover(self, point: int, hover_zone: int) -> None: def on_post_text_command(self, command_name: str, args: Optional[Dict[str, Any]]) -> None: if command_name in ("next_field", "prev_field") and args is None: if "signatureHelp" not in userprefs().disabled_capabilities: - sublime.set_timeout_async(lambda: self._do_signature_help(manual=True)) + sublime.set_timeout_async(lambda: self.do_signature_help_async(manual=True)) if not self.view.is_popup_visible(): return if command_name in ["hide_auto_complete", "move", "commit_completion"] or 'delete' in command_name: @@ -370,7 +370,7 @@ def resolve(clist: sublime.CompletionList, items: List[sublime.CompletionItem], # --- textDocument/signatureHelp ----------------------------------------------------------------------------------- - def _do_signature_help(self, manual: bool) -> None: + def do_signature_help_async(self, manual: bool) -> None: # NOTE: We take the beginning of the region to check the previous char (see last_char variable). This is for # when a language server inserts a snippet completion. pos = self._stored_region.a diff --git a/plugin/execute_command.py b/plugin/execute_command.py index 90e900f45..da9c8d667 100644 --- a/plugin/execute_command.py +++ b/plugin/execute_command.py @@ -2,6 +2,7 @@ from .core.protocol import Error from .core.protocol import ExecuteCommandParams from .core.registry import LspTextCommand +from .core.registry import windows from .core.typing import List, Optional, Any from .core.views import uri_from_view, offset_to_point, region_to_range, text_document_identifier @@ -14,6 +15,18 @@ def run(self, command_args: Optional[List[Any]] = None, session_name: Optional[str] = None, event: Optional[dict] = None) -> None: + # Handle VSCode-specific command for triggering AC/sighelp + if command_name == "editor.action.triggerSuggest": + # Triggered from set_timeout as suggestions popup doesn't trigger otherwise. + return sublime.set_timeout(lambda: self.view.run_command("auto_complete")) + if command_name == "editor.action.triggerParameterHints": + + def run_async() -> None: + listener = windows.listener_for_view(self.view) + if listener: + listener.do_signature_help_async(manual=False) + + return sublime.set_timeout_async(run_async) session = self.session_by_name(session_name) if session_name else self.best_session(self.capability) if session and command_name: if command_args: