Skip to content

Commit df43568

Browse files
authored
Add menu item to disable/enable hover popups (#2316)
1 parent 4ff494b commit df43568

File tree

7 files changed

+74
-11
lines changed

7 files changed

+74
-11
lines changed

Main.sublime-menu

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
{
160160
"caption": "LSP: Show Inlay Hints",
161161
"command": "lsp_toggle_inlay_hints"
162+
},
163+
{
164+
"caption": "LSP: Show Hover Popups",
165+
"command": "lsp_toggle_hover_popups",
166+
"checkbox": true
162167
}
163168
]
164169
},

boot.py

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from .plugin.hierarchy import LspHierarchyToggleCommand
5757
from .plugin.hierarchy import LspTypeHierarchyCommand
5858
from .plugin.hover import LspHoverCommand
59+
from .plugin.hover import LspToggleHoverPopupsCommand
5960
from .plugin.inlay_hint import LspInlayHintClickCommand
6061
from .plugin.inlay_hint import LspToggleInlayHintsCommand
6162
from .plugin.panels import LspClearLogPanelCommand

plugin/core/constants.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# TODO: Move all constants which are shared by multiple modules into this file, so that they can be imported without
2+
# causing import loops
3+
4+
# Keys for View.add_regions
5+
HOVER_HIGHLIGHT_KEY = 'lsp_hover_highlight'
6+
7+
# Setting keys
8+
HOVER_ENABLED_KEY = 'lsp_show_hover_popups'
9+
HOVER_PROVIDER_COUNT_KEY = 'lsp_hover_provider_count'
10+
SHOW_DEFINITIONS_KEY = 'show_definitions'

plugin/core/sessions.py

+3
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,9 @@ def start_code_lenses_async(self) -> None:
581581
def set_code_lenses_pending_refresh(self, needs_refresh: bool = True) -> None:
582582
...
583583

584+
def reset_show_definitions(self) -> None:
585+
...
586+
584587

585588
class SessionBufferProtocol(Protocol):
586589

plugin/documents.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .code_actions import CodeActionOrCommand
33
from .code_actions import CodeActionsByConfigName
44
from .completion import QueryCompletionsTask
5+
from .core.constants import HOVER_ENABLED_KEY
56
from .core.logging import debug
67
from .core.panels import PanelName
78
from .core.protocol import Diagnostic
@@ -466,7 +467,8 @@ def on_query_context(self, key: str, operator: int, operand: Any, match_all: boo
466467
def on_hover(self, point: int, hover_zone: int) -> None:
467468
if self.view.is_popup_visible():
468469
return
469-
if hover_zone == sublime.HOVER_TEXT:
470+
window = self.view.window()
471+
if hover_zone == sublime.HOVER_TEXT and window and window.settings().get(HOVER_ENABLED_KEY, True):
470472
self.view.run_command("lsp_hover", {"point": point})
471473
elif hover_zone == sublime.HOVER_GUTTER:
472474
# Lightbulb must be visible and at the same line

plugin/hover.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from .code_actions import actions_manager
22
from .code_actions import CodeActionOrCommand
33
from .code_actions import CodeActionsByConfigName
4+
from .core.constants import HOVER_ENABLED_KEY
5+
from .core.constants import HOVER_HIGHLIGHT_KEY
6+
from .core.constants import HOVER_PROVIDER_COUNT_KEY
7+
from .core.constants import SHOW_DEFINITIONS_KEY
48
from .core.open import lsp_range_from_uri_fragment
59
from .core.open import open_file_uri
610
from .core.open import open_in_browser
@@ -18,6 +22,7 @@
1822
from .core.sessions import SessionBufferProtocol
1923
from .core.settings import userprefs
2024
from .core.typing import List, Optional, Dict, Tuple, Sequence, Union
25+
from .core.typing import cast
2126
from .core.url import parse_uri
2227
from .core.views import diagnostic_severity
2328
from .core.views import first_selection_region
@@ -35,12 +40,12 @@
3540
from .core.views import text_document_position_params
3641
from .core.views import unpack_href_location
3742
from .core.views import update_lsp_popup
38-
from .session_view import HOVER_HIGHLIGHT_KEY
3943
from functools import partial
4044
from urllib.parse import urlparse
4145
import html
4246
import mdpopups
4347
import sublime
48+
import sublime_plugin
4449

4550

4651
SUBLIME_WORD_MASK = 515
@@ -385,3 +390,34 @@ def try_open_custom_uri_async(self, href: str) -> None:
385390
for session in self.sessions():
386391
if session.try_open_uri_async(href, r) is not None:
387392
return
393+
394+
395+
class LspToggleHoverPopupsCommand(sublime_plugin.WindowCommand):
396+
397+
def is_enabled(self) -> bool:
398+
view = self.window.active_view()
399+
if view:
400+
return self._has_hover_provider(view)
401+
return False
402+
403+
def is_checked(self) -> bool:
404+
return bool(self.window.settings().get(HOVER_ENABLED_KEY, True))
405+
406+
def run(self) -> None:
407+
enable = not self.is_checked()
408+
self.window.settings().set(HOVER_ENABLED_KEY, enable)
409+
sublime.set_timeout_async(partial(self._update_views_async, enable))
410+
411+
def _has_hover_provider(self, view: sublime.View) -> bool:
412+
return cast(int, view.settings().get(HOVER_PROVIDER_COUNT_KEY, 0)) > 0
413+
414+
def _update_views_async(self, enable: bool) -> None:
415+
window_manager = windows.lookup(self.window)
416+
if not window_manager:
417+
return
418+
for session in window_manager.get_sessions():
419+
for session_view in session.session_views_async():
420+
if enable:
421+
session_view.view.settings().set(SHOW_DEFINITIONS_KEY, False)
422+
else:
423+
session_view.reset_show_definitions()

plugin/session_view.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from .code_lens import CodeLensView
22
from .core.active_request import ActiveRequest
3+
from .core.constants import HOVER_ENABLED_KEY
4+
from .core.constants import HOVER_HIGHLIGHT_KEY
5+
from .core.constants import HOVER_PROVIDER_COUNT_KEY
6+
from .core.constants import SHOW_DEFINITIONS_KEY
37
from .core.promise import Promise
48
from .core.protocol import CodeLens
59
from .core.protocol import CodeLensExtended
@@ -22,7 +26,6 @@
2226
import sublime
2327

2428
DIAGNOSTIC_TAG_VALUES = [v for (k, v) in DiagnosticTag.__dict__.items() if not k.startswith('_')] # type: List[int]
25-
HOVER_HIGHLIGHT_KEY = "lsp_hover_highlight"
2629

2730

2831
class TagData:
@@ -39,9 +42,7 @@ class SessionView:
3942
Holds state per session per view.
4043
"""
4144

42-
SHOW_DEFINITIONS_KEY = "show_definitions"
4345
HOVER_PROVIDER_KEY = "hoverProvider"
44-
HOVER_PROVIDER_COUNT_KEY = "lsp_hover_provider_count"
4546
AC_TRIGGERS_KEY = "auto_complete_triggers"
4647
COMPLETION_PROVIDER_KEY = "completionProvider"
4748
TRIGGER_CHARACTERS_KEY = "completionProvider.triggerCharacters"
@@ -224,20 +225,25 @@ def _apply_auto_complete_triggers(
224225

225226
def _increment_hover_count(self) -> None:
226227
settings = self.view.settings()
227-
count = settings.get(self.HOVER_PROVIDER_COUNT_KEY, 0)
228+
count = settings.get(HOVER_PROVIDER_COUNT_KEY, 0)
228229
if isinstance(count, int):
229230
count += 1
230-
settings.set(self.HOVER_PROVIDER_COUNT_KEY, count)
231-
settings.set(self.SHOW_DEFINITIONS_KEY, False)
231+
settings.set(HOVER_PROVIDER_COUNT_KEY, count)
232+
window = self.view.window()
233+
if window and window.settings().get(HOVER_ENABLED_KEY, True):
234+
settings.set(SHOW_DEFINITIONS_KEY, False)
232235

233236
def _decrement_hover_count(self) -> None:
234237
settings = self.view.settings()
235-
count = settings.get(self.HOVER_PROVIDER_COUNT_KEY)
238+
count = settings.get(HOVER_PROVIDER_COUNT_KEY)
236239
if isinstance(count, int):
237240
count -= 1
238241
if count == 0:
239-
settings.erase(self.HOVER_PROVIDER_COUNT_KEY)
240-
settings.set(self.SHOW_DEFINITIONS_KEY, True)
242+
settings.erase(HOVER_PROVIDER_COUNT_KEY)
243+
self.reset_show_definitions()
244+
245+
def reset_show_definitions(self) -> None:
246+
self.view.settings().erase(SHOW_DEFINITIONS_KEY)
241247

242248
def get_uri(self) -> Optional[DocumentUri]:
243249
listener = self.listener()

0 commit comments

Comments
 (0)