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 LspWindowCommand #1991

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from .core.protocol import Request
from .core.protocol import Response
from .core.protocol import WorkspaceFolder
from .core.registry import LspTextCommand
from .core.registry import LspWindowCommand
from .core.sessions import AbstractPlugin
from .core.sessions import register_plugin
from .core.sessions import Session
Expand All @@ -34,6 +36,8 @@
'FileWatcherEvent',
'FileWatcherEventType',
'FileWatcherProtocol',
'LspTextCommand',
'LspWindowCommand',
'MarkdownLangMap',
'matches_pattern',
'Notification',
Expand Down
36 changes: 32 additions & 4 deletions plugin/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,46 @@ def get_position(view: sublime.View, event: Optional[dict] = None, point: Option
return None


class LspWindowCommand(sublime_plugin.WindowCommand):
"""
Inherit from this class to define requests which are not bound to a particular view. This allows to run requests
for example from links in HtmlSheets or when an unrelated file has focus.
"""

# When this is defined in a derived class, the command is enabled only if there exists a session with the given
# capability attached to a view in the window.
capability = ''

# When this is defined in a derived class, the command is enabled only if there exists a session with the given
# name attached to a view in the window.
session_name = ''

def is_enabled(self) -> bool:
return self.session() is not None

def session(self) -> Optional[Session]:
for session in windows.lookup(self.window).get_sessions():
if self.capability and not session.has_capability(self.capability):
continue
if self.session_name and session.config.name != self.session_name:
continue
return session
else:
return None


class LspTextCommand(sublime_plugin.TextCommand):
"""
Inherit from this class to define your requests that should be triggered via the command palette and/or a
keybinding.
"""

# When this is defined in a derived class, the command is enabled only if there exists a session attached to the
# view that has the given capability.
# When this is defined in a derived class, the command is enabled only if there exists a session with the given
# capability attached to the active view.
capability = ''

# When this is defined in a derived class, the command is enabled only if there exists a session attached to the
# view that has the given name.
# When this is defined in a derived class, the command is enabled only if there exists a session with the given
# name attached to the active view.
session_name = ''

def is_enabled(self, event: Optional[dict] = None, point: Optional[int] = None) -> bool:
Expand Down
3 changes: 3 additions & 0 deletions plugin/core/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def __init__(
def get_config_manager(self) -> WindowConfigManager:
return self._configs

def get_sessions(self) -> Generator[Session, None, None]:
yield from self._sessions

def on_load_project_async(self) -> None:
self.update_workspace_folders_async()
self._configs.update()
Expand Down