From f9500df470b9ada5e37338422170759be75a1e6f Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 2 Nov 2023 00:08:52 +0100 Subject: [PATCH 1/3] Add menu item to toggle code lenses --- Main.sublime-menu | 5 +++++ boot.py | 1 + plugin/code_lens.py | 34 +++++++++++++++++++++++++++++++++- plugin/core/constants.py | 1 + plugin/core/sessions.py | 3 +++ plugin/session_view.py | 8 ++++++++ 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Main.sublime-menu b/Main.sublime-menu index 0b5d71bd1..a4e696ee6 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -156,6 +156,11 @@ "id": "lsp", "caption": "-" }, + { + "caption": "LSP: Show Code Lens", + "command": "lsp_toggle_code_lens", + "checkbox": true + }, { "caption": "LSP: Show Inlay Hints", "command": "lsp_toggle_inlay_hints" diff --git a/boot.py b/boot.py index f16116d15..8f3aabe2c 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 LspToggleCodeLensCommand 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..d454613d1 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 LspToggleCodeLensCommand(LspWindowCommand): + capability = 'inlayHintProvider' + + @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 bool(self.window.settings().get(CODE_LENS_ENABLED_KEY, True)) + + 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..3cc54120d 100644 --- a/plugin/session_view.py +++ b/plugin/session_view.py @@ -1,4 +1,5 @@ from .code_lens import CodeLensView +from .code_lens import LspToggleCodeLensCommand 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 LspToggleCodeLensCommand.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 LspToggleCodeLensCommand.are_enabled(self.view.window()): + return if not self._code_lenses.is_initialized(): self.start_code_lenses_async() return From 0bb859b99ccbab9d64f299b63317178454a206d3 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 2 Nov 2023 09:21:39 +0100 Subject: [PATCH 2/3] review comments --- Main.sublime-menu | 7 ++++--- boot.py | 2 +- plugin/code_lens.py | 6 +++--- plugin/session_view.py | 6 +++--- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Main.sublime-menu b/Main.sublime-menu index a4e696ee6..c59db3bd7 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -157,13 +157,14 @@ "caption": "-" }, { - "caption": "LSP: Show Code Lens", - "command": "lsp_toggle_code_lens", + "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 8f3aabe2c..a54c62206 100644 --- a/boot.py +++ b/boot.py @@ -7,7 +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 LspToggleCodeLensCommand +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 d454613d1..7546065d1 100644 --- a/plugin/code_lens.py +++ b/plugin/code_lens.py @@ -14,8 +14,8 @@ import sublime -class LspToggleCodeLensCommand(LspWindowCommand): - capability = 'inlayHintProvider' +class LspToggleCodeLensesCommand(LspWindowCommand): + capability = 'codeLensProvider' @classmethod def are_enabled(cls, window: Optional[sublime.Window]) -> bool: @@ -24,7 +24,7 @@ def are_enabled(cls, window: Optional[sublime.Window]) -> bool: return bool(window.settings().get(CODE_LENS_ENABLED_KEY, True)) def is_checked(self) -> bool: - return bool(self.window.settings().get(CODE_LENS_ENABLED_KEY, True)) + return self.are_enabled(self.window) def run(self) -> None: enable = not self.is_checked() diff --git a/plugin/session_view.py b/plugin/session_view.py index 3cc54120d..0e775acb9 100644 --- a/plugin/session_view.py +++ b/plugin/session_view.py @@ -1,5 +1,5 @@ from .code_lens import CodeLensView -from .code_lens import LspToggleCodeLensCommand +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 @@ -376,7 +376,7 @@ def on_post_save_async(self, new_uri: DocumentUri) -> None: # --- textDocument/codeLens ---------------------------------------------------------------------------------------- def start_code_lenses_async(self) -> None: - if not LspToggleCodeLensCommand.are_enabled(self.view.window()): + 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(): @@ -394,7 +394,7 @@ 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 LspToggleCodeLensCommand.are_enabled(self.view.window()): + if not LspToggleCodeLensesCommand.are_enabled(self.view.window()): return if not self._code_lenses.is_initialized(): self.start_code_lenses_async() From aa69237f89525fcf0961e91b65a1a017555037b4 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 2 Nov 2023 23:42:44 +0100 Subject: [PATCH 3/3] add to command palette --- Default.sublime-commands | 4 ++++ 1 file changed, 4 insertions(+) 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",