-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathregistry.py
131 lines (108 loc) · 4.84 KB
/
registry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from .configurations import ConfigManager
from .sessions import Session
from .settings import client_configs
from .typing import Optional, Any, Generator, Iterable
from .windows import WindowRegistry
import sublime
import sublime_plugin
def best_session(view: sublime.View, sessions: Iterable[Session], point: Optional[int] = None) -> Optional[Session]:
if point is None:
try:
point = view.sel()[0].b
except IndexError:
return None
try:
return max(sessions, key=lambda s: view.score_selector(point, s.config.priority_selector)) # type: ignore
except ValueError:
return None
configs = ConfigManager(client_configs.all)
client_configs.set_listener(configs.update)
windows = WindowRegistry(configs)
def get_position(view: sublime.View, event: Optional[dict] = None, point: Optional[int] = None) -> Optional[int]:
if isinstance(point, int):
return point
if event:
x, y = event.get("x"), event.get("y")
if x is not None and y is not None:
return view.window_to_text((x, y))
try:
return view.sel()[0].begin()
except IndexError:
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.
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.
session_name = ''
def is_enabled(self, event: Optional[dict] = None, point: Optional[int] = None) -> bool:
if self.capability:
# At least one active session with the given capability must exist.
position = get_position(self.view, event, point)
if position is None:
return False
if not self.best_session(self.capability, position):
return False
if self.session_name:
# There must exist an active session with the given (config) name.
if not self.session_by_name(self.session_name):
return False
if not self.capability and not self.session_name:
# Any session will do.
return any(self.sessions())
return True
def want_event(self) -> bool:
return True
def best_session(self, capability: str, point: Optional[int] = None) -> Optional[Session]:
listener = windows.listener_for_view(self.view)
return listener.session_async(capability, point) if listener else None
def session_by_name(self, name: Optional[str] = None, capability_path: Optional[str] = None) -> Optional[Session]:
target = name if name else self.session_name
listener = windows.listener_for_view(self.view)
if listener:
for sv in listener.session_views_async():
if sv.session.config.name == target:
if capability_path is None or sv.has_capability_async(capability_path):
return sv.session
else:
return None
return None
def sessions(self, capability_path: Optional[str] = None) -> Generator[Session, None, None]:
listener = windows.listener_for_view(self.view)
if listener:
for sv in listener.session_views_async():
if capability_path is None or sv.has_capability_async(capability_path):
yield sv.session
class LspRestartServerCommand(LspTextCommand):
def run(self, edit: Any, config_name: str = None) -> None:
window = self.view.window()
if not window:
return
self._config_names = [session.config.name for session in self.sessions()] if not config_name else [config_name]
if not self._config_names:
return
self._wm = windows.lookup(window)
if len(self._config_names) == 1:
self.restart_server(0)
else:
window.show_quick_panel(self._config_names, self.restart_server)
def restart_server(self, index: int) -> None:
if index < 0:
return
def run_async() -> None:
config_name = self._config_names[index]
if not config_name:
return
self._wm.end_config_sessions_async(config_name)
listener = windows.listener_for_view(self.view)
if listener:
self._wm.register_listener_async(listener)
sublime.set_timeout_async(run_async)
class LspRecheckSessionsCommand(sublime_plugin.WindowCommand):
def run(self) -> None:
sublime.set_timeout_async(lambda: windows.lookup(self.window).restart_sessions_async())