Skip to content

Commit

Permalink
Make toggle survive ST restart
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann committed Oct 30, 2023
1 parent 1ec1626 commit 01a67cf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
9 changes: 7 additions & 2 deletions plugin/core/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# TODO: Move all constants which are shared by multiple modules into this file, so that they can be imported without
# causing import loops

HOVER_HIGHLIGHT_KEY = "lsp_hover_highlight"
HOVER_PROVIDER_COUNT_KEY = "lsp_hover_provider_count"
# Keys for View.add_regions
HOVER_HIGHLIGHT_KEY = 'lsp_hover_highlight'

# Setting keys
HOVER_ENABLED_KEY = 'lsp_show_hover_popups'
HOVER_PROVIDER_COUNT_KEY = 'lsp_hover_provider_count'
SHOW_DEFINITIONS_KEY = 'show_definitions'
1 change: 0 additions & 1 deletion plugin/core/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def __init__(self, window: sublime.Window, workspace: ProjectFolders, config_man
self.suppress_sessions_restart_on_project_update = False
self.total_error_count = 0
self.total_warning_count = 0
self.hover_enabled = True
sublime.set_timeout(functools.partial(self._update_panel_main_thread, _NO_DIAGNOSTICS_PLACEHOLDER, []))
self.panel_manager.ensure_log_panel()
self._config_manager.add_change_listener(self)
Expand Down
4 changes: 3 additions & 1 deletion plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .code_actions import CodeActionOrCommand
from .code_actions import CodeActionsByConfigName
from .completion import QueryCompletionsTask
from .core.constants import HOVER_ENABLED_KEY
from .core.logging import debug
from .core.panels import PanelName
from .core.protocol import Diagnostic
Expand Down Expand Up @@ -466,7 +467,8 @@ def on_query_context(self, key: str, operator: int, operand: Any, match_all: boo
def on_hover(self, point: int, hover_zone: int) -> None:
if self.view.is_popup_visible():
return
if hover_zone == sublime.HOVER_TEXT and self._manager and self._manager.hover_enabled:
window = self.view.window()
if hover_zone == sublime.HOVER_TEXT and window and window.settings().get(HOVER_ENABLED_KEY, True):
self.view.run_command("lsp_hover", {"point": point})
elif hover_zone == sublime.HOVER_GUTTER:
# Lightbulb must be visible and at the same line
Expand Down
45 changes: 27 additions & 18 deletions plugin/hover.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from .code_actions import actions_manager
from .code_actions import CodeActionOrCommand
from .code_actions import CodeActionsByConfigName
from .core.constants import HOVER_ENABLED_KEY
from .core.constants import HOVER_HIGHLIGHT_KEY
from .core.constants import HOVER_PROVIDER_COUNT_KEY
from .core.constants import SHOW_DEFINITIONS_KEY
from .core.open import lsp_range_from_uri_fragment
from .core.open import open_file_uri
from .core.open import open_in_browser
Expand Down Expand Up @@ -390,25 +392,32 @@ def try_open_custom_uri_async(self, href: str) -> None:
return


class LspToggleHoverPopupsCommand(sublime_plugin.TextCommand):
class LspToggleHoverPopupsCommand(sublime_plugin.WindowCommand):

def is_enabled(self) -> bool:
return self._has_hover_provider()
view = self.window.active_view()
if view:
return self._has_hover_provider(view)
return False

def is_checked(self) -> bool:
window_manager = windows.lookup(self.view.window())
return window_manager.hover_enabled if window_manager else False

def run(self, edit: sublime.Edit) -> None:
window_manager = windows.lookup(self.view.window())
if window_manager:
window_manager.hover_enabled = not window_manager.hover_enabled
for session in window_manager.get_sessions():
for session_view in session.session_views_async():
if window_manager.hover_enabled:
session_view.view.settings().set('show_definitions', False)
else:
session_view.reset_show_definitions()

def _has_hover_provider(self) -> bool:
return cast(int, self.view.settings().get(HOVER_PROVIDER_COUNT_KEY, 0)) > 0
return bool(self.window.settings().get(HOVER_ENABLED_KEY, True))

def run(self) -> None:
enable = not self.is_checked()
self.window.settings().set(HOVER_ENABLED_KEY, enable)
sublime.set_timeout_async(partial(self._update_views_async, enable))

def _has_hover_provider(self, view: sublime.View) -> bool:
return cast(int, view.settings().get(HOVER_PROVIDER_COUNT_KEY, 0)) > 0

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.view.settings().set(SHOW_DEFINITIONS_KEY, False)
else:
session_view.reset_show_definitions()
12 changes: 6 additions & 6 deletions plugin/session_view.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from .code_lens import CodeLensView
from .core.active_request import ActiveRequest
from .core.constants import HOVER_ENABLED_KEY
from .core.constants import HOVER_HIGHLIGHT_KEY
from .core.constants import HOVER_PROVIDER_COUNT_KEY
from .core.constants import SHOW_DEFINITIONS_KEY
from .core.promise import Promise
from .core.protocol import CodeLens
from .core.protocol import CodeLensExtended
from .core.protocol import DiagnosticTag
from .core.protocol import DocumentUri
from .core.protocol import Notification
from .core.protocol import Request
from .core.registry import windows
from .core.sessions import AbstractViewListener
from .core.sessions import Session
from .core.settings import globalprefs
Expand Down Expand Up @@ -42,7 +43,6 @@ class SessionView:
Holds state per session per view.
"""

SHOW_DEFINITIONS_KEY = "show_definitions"
HOVER_PROVIDER_KEY = "hoverProvider"
AC_TRIGGERS_KEY = "auto_complete_triggers"
COMPLETION_PROVIDER_KEY = "completionProvider"
Expand Down Expand Up @@ -230,9 +230,9 @@ def _increment_hover_count(self) -> None:
if isinstance(count, int):
count += 1
settings.set(HOVER_PROVIDER_COUNT_KEY, count)
manager = windows.lookup(self.view.window())
if manager and manager.hover_enabled:
settings.set(self.SHOW_DEFINITIONS_KEY, False)
window = self.view.window()
if window and window.settings().get(HOVER_ENABLED_KEY, True):
settings.set(SHOW_DEFINITIONS_KEY, False)

def _decrement_hover_count(self) -> None:
settings = self.view.settings()
Expand All @@ -244,7 +244,7 @@ def _decrement_hover_count(self) -> None:
self.reset_show_definitions()

def reset_show_definitions(self) -> None:
self.view.settings().set(self.SHOW_DEFINITIONS_KEY, globalprefs().get(self.SHOW_DEFINITIONS_KEY))
self.view.settings().set(SHOW_DEFINITIONS_KEY, globalprefs().get(SHOW_DEFINITIONS_KEY))

def get_uri(self) -> Optional[DocumentUri]:
listener = self.listener()
Expand Down

0 comments on commit 01a67cf

Please sign in to comment.