Skip to content

Commit a579b9d

Browse files
authored
Add LspWindowCommand (#1991)
Resolves #1988
1 parent e79d20f commit a579b9d

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

plugin/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from .core.protocol import Request
1010
from .core.protocol import Response
1111
from .core.protocol import WorkspaceFolder
12+
from .core.registry import LspTextCommand
13+
from .core.registry import LspWindowCommand
1214
from .core.sessions import AbstractPlugin
1315
from .core.sessions import register_plugin
1416
from .core.sessions import Session
@@ -34,6 +36,8 @@
3436
'FileWatcherEvent',
3537
'FileWatcherEventType',
3638
'FileWatcherProtocol',
39+
'LspTextCommand',
40+
'LspWindowCommand',
3741
'MarkdownLangMap',
3842
'matches_pattern',
3943
'Notification',

plugin/core/registry.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,46 @@ def get_position(view: sublime.View, event: Optional[dict] = None, point: Option
3838
return None
3939

4040

41+
class LspWindowCommand(sublime_plugin.WindowCommand):
42+
"""
43+
Inherit from this class to define requests which are not bound to a particular view. This allows to run requests
44+
for example from links in HtmlSheets or when an unrelated file has focus.
45+
"""
46+
47+
# When this is defined in a derived class, the command is enabled only if there exists a session with the given
48+
# capability attached to a view in the window.
49+
capability = ''
50+
51+
# When this is defined in a derived class, the command is enabled only if there exists a session with the given
52+
# name attached to a view in the window.
53+
session_name = ''
54+
55+
def is_enabled(self) -> bool:
56+
return self.session() is not None
57+
58+
def session(self) -> Optional[Session]:
59+
for session in windows.lookup(self.window).get_sessions():
60+
if self.capability and not session.has_capability(self.capability):
61+
continue
62+
if self.session_name and session.config.name != self.session_name:
63+
continue
64+
return session
65+
else:
66+
return None
67+
68+
4169
class LspTextCommand(sublime_plugin.TextCommand):
4270
"""
4371
Inherit from this class to define your requests that should be triggered via the command palette and/or a
4472
keybinding.
4573
"""
4674

47-
# When this is defined in a derived class, the command is enabled only if there exists a session attached to the
48-
# view that has the given capability.
75+
# When this is defined in a derived class, the command is enabled only if there exists a session with the given
76+
# capability attached to the active view.
4977
capability = ''
5078

51-
# When this is defined in a derived class, the command is enabled only if there exists a session attached to the
52-
# view that has the given name.
79+
# When this is defined in a derived class, the command is enabled only if there exists a session with the given
80+
# name attached to the active view.
5381
session_name = ''
5482

5583
def is_enabled(self, event: Optional[dict] = None, point: Optional[int] = None) -> bool:

plugin/core/windows.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ def __init__(
8585
def get_config_manager(self) -> WindowConfigManager:
8686
return self._configs
8787

88+
def get_sessions(self) -> Generator[Session, None, None]:
89+
yield from self._sessions
90+
8891
def on_load_project_async(self) -> None:
8992
self.update_workspace_folders_async()
9093
self._configs.update()

0 commit comments

Comments
 (0)