Skip to content

Commit

Permalink
Add menu item to toggle code lenses (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl authored Nov 5, 2023
1 parent f07c307 commit 104e4eb
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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 LspToggleCodeLensesCommand
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 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',
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 LspToggleCodeLensesCommand
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 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
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 LspToggleCodeLensesCommand.are_enabled(self.view.window()):
return
if not self._code_lenses.is_initialized():
self.start_code_lenses_async()
return
Expand Down

0 comments on commit 104e4eb

Please sign in to comment.