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

Add menu item to toggle code lenses #2351

Merged
merged 3 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
"id": "lsp",
"caption": "-"
},
{
"caption": "LSP: Show Code Lens",
jwortmann marked this conversation as resolved.
Show resolved Hide resolved
"command": "lsp_toggle_code_lens",
"checkbox": true
},
{
"caption": "LSP: Show Inlay Hints",
"command": "lsp_toggle_inlay_hints"
Expand Down
1 change: 1 addition & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion plugin/code_lens.py
Original file line number Diff line number Diff line change
@@ -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'
jwortmann marked this conversation as resolved.
Show resolved Hide resolved

@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',
Expand Down
1 change: 1 addition & 0 deletions plugin/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 3 additions & 0 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
...

Expand Down
8 changes: 8 additions & 0 deletions plugin/session_view.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -375,19 +376,26 @@ 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
self._code_lenses.handle_response(self.session.config.name, response)
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
Expand Down