From 104e4eba5bc9dee7cc7f98739be32a5accc61569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Sun, 5 Nov 2023 21:26:21 +0100 Subject: [PATCH] Add menu item to toggle code lenses (#2351) --- Default.sublime-commands | 4 ++++ Main.sublime-menu | 8 +++++++- boot.py | 1 + plugin/code_lens.py | 34 +++++++++++++++++++++++++++++++++- plugin/core/constants.py | 1 + plugin/core/sessions.py | 3 +++ plugin/session_view.py | 8 ++++++++ 7 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Default.sublime-commands b/Default.sublime-commands index f0e9e026e..c798956b8 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -163,6 +163,10 @@ "caption": "LSP: Show Type Hierarchy", "command": "lsp_type_hierarchy" }, + { + "caption": "LSP: Toggle Code Lenses", + "command": "lsp_toggle_code_lenses", + }, { "caption": "LSP: Toggle Inlay Hints", "command": "lsp_toggle_inlay_hints", diff --git a/Main.sublime-menu b/Main.sublime-menu index 0b5d71bd1..c59db3bd7 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -156,9 +156,15 @@ "id": "lsp", "caption": "-" }, + { + "caption": "LSP: Show Code Lenses", + "command": "lsp_toggle_code_lenses", + "checkbox": true + }, { "caption": "LSP: Show Inlay Hints", - "command": "lsp_toggle_inlay_hints" + "command": "lsp_toggle_inlay_hints", + "checkbox": true }, { "caption": "LSP: Show Hover Popups", diff --git a/boot.py b/boot.py index f16116d15..a54c62206 100644 --- a/boot.py +++ b/boot.py @@ -7,6 +7,7 @@ from .plugin.code_actions import LspRefactorCommand from .plugin.code_actions import LspSourceActionCommand from .plugin.code_lens import LspCodeLensCommand +from .plugin.code_lens import LspToggleCodeLensesCommand from .plugin.color import LspColorPresentationCommand from .plugin.completion import LspCommitCompletionWithOppositeInsertMode from .plugin.completion import LspResolveDocsCommand diff --git a/plugin/code_lens.py b/plugin/code_lens.py index e78fb5a15..7546065d1 100644 --- a/plugin/code_lens.py +++ b/plugin/code_lens.py @@ -1,16 +1,48 @@ +from .core.constants import CODE_LENS_ENABLED_KEY from .core.protocol import CodeLens from .core.protocol import CodeLensExtended from .core.protocol import Error -from .core.typing import List, Tuple, Dict, Iterable, Generator, Union, cast +from .core.typing import List, Tuple, Dict, Iterable, Optional, Generator, Union, cast from .core.registry import LspTextCommand +from .core.registry import LspWindowCommand from .core.registry import windows from .core.views import make_command_link from .core.views import range_to_region from html import escape as html_escape +from functools import partial import itertools import sublime +class LspToggleCodeLensesCommand(LspWindowCommand): + capability = 'codeLensProvider' + + @classmethod + def are_enabled(cls, window: Optional[sublime.Window]) -> bool: + if not window: + return False + return bool(window.settings().get(CODE_LENS_ENABLED_KEY, True)) + + def is_checked(self) -> bool: + return self.are_enabled(self.window) + + def run(self) -> None: + enable = not self.is_checked() + self.window.settings().set(CODE_LENS_ENABLED_KEY, enable) + sublime.set_timeout_async(partial(self._update_views_async, enable)) + + def _update_views_async(self, enable: bool) -> None: + window_manager = windows.lookup(self.window) + if not window_manager: + return + for session in window_manager.get_sessions(): + for session_view in session.session_views_async(): + if enable: + session_view.start_code_lenses_async() + else: + session_view.clear_code_lenses_async() + + class CodeLensData: __slots__ = ( 'data', diff --git a/plugin/core/constants.py b/plugin/core/constants.py index 188ea4910..daa5b908d 100644 --- a/plugin/core/constants.py +++ b/plugin/core/constants.py @@ -5,6 +5,7 @@ HOVER_HIGHLIGHT_KEY = 'lsp_hover_highlight' # Setting keys +CODE_LENS_ENABLED_KEY = 'lsp_show_code_lens' HOVER_ENABLED_KEY = 'lsp_show_hover_popups' HOVER_PROVIDER_COUNT_KEY = 'lsp_hover_provider_count' SHOW_DEFINITIONS_KEY = 'show_definitions' diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 2e538f5b4..04db7656d 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -578,6 +578,9 @@ def get_resolved_code_lenses_for_region(self, region: sublime.Region) -> Generat def start_code_lenses_async(self) -> None: ... + def clear_code_lenses_async(self) -> None: + ... + def set_code_lenses_pending_refresh(self, needs_refresh: bool = True) -> None: ... diff --git a/plugin/session_view.py b/plugin/session_view.py index 4044a375e..0e775acb9 100644 --- a/plugin/session_view.py +++ b/plugin/session_view.py @@ -1,4 +1,5 @@ from .code_lens import CodeLensView +from .code_lens import LspToggleCodeLensesCommand from .core.active_request import ActiveRequest from .core.constants import HOVER_ENABLED_KEY from .core.constants import HOVER_HIGHLIGHT_KEY @@ -375,12 +376,17 @@ def on_post_save_async(self, new_uri: DocumentUri) -> None: # --- textDocument/codeLens ---------------------------------------------------------------------------------------- def start_code_lenses_async(self) -> None: + if not LspToggleCodeLensesCommand.are_enabled(self.view.window()): + return params = {'textDocument': text_document_identifier(self.view)} for request_id, data in self._active_requests.items(): if data.request.method == "codeAction/resolve": self.session.send_notification(Notification("$/cancelRequest", {"id": request_id})) self.session.send_request_async(Request("textDocument/codeLens", params, self.view), self._on_code_lenses_async) + def clear_code_lenses_async(self) -> None: + self._code_lenses.clear_view() + def _on_code_lenses_async(self, response: Optional[List[CodeLens]]) -> None: if not self._is_listener_alive() or not isinstance(response, list): return @@ -388,6 +394,8 @@ def _on_code_lenses_async(self, response: Optional[List[CodeLens]]) -> None: self.resolve_visible_code_lenses_async() def resolve_visible_code_lenses_async(self) -> None: + if not LspToggleCodeLensesCommand.are_enabled(self.view.window()): + return if not self._code_lenses.is_initialized(): self.start_code_lenses_async() return