-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
228 lines (172 loc) · 7.31 KB
/
main.py
File metadata and controls
228 lines (172 loc) · 7.31 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# pyright: reportIncompatibleMethodOverride=false
import sublime
import sublime_plugin
import logging
import codemp
from .plugin.utils import is_codemp_buffer, safe_listener_detach
from .plugin.utils import safe_listener_attach
from .plugin.utils import some
from .plugin.utils import is_codemp_buffer
from .plugin.core.session import session
from .plugin.core.workspace import workspaces
from .plugin.core.buffers import buffers
from .plugin.text_listener import TEXT_LISTENER
from .plugin.input_handlers import SimpleListInput
from .plugin import globals as g
from .plugin.commands.client import *
from .plugin.commands.workspace import *
from .plugin.quickpanel.qpbrowser import QPServerBrowser
from .plugin.quickpanel.qpbrowser import QPWorkspaceBrowser
from .plugin.logpanel import CodempToggleLogPanelCommand
from .plugin.logpanel import CodempLogMessageCommand
from .plugin.logpanel import CodempLogger
LOG_LEVEL = logging.DEBUG
package_logger = CodempLogger(__package__, LOG_LEVEL)
# handler = logging.StreamHandler()
# handler.setFormatter(
# logging.Formatter(
# fmt="{levelname} <{asctime}> ({threadName}) [{name}::{funcName}:{lineno}] {message}",
# style="{",
# )
# )
# package_logger = logging.getLogger(__package__)
# package_logger.setLevel(LOG_LEVEL)
# package_logger.propagate = False
logger = logging.getLogger(__name__)
# Initialisation and Deinitialisation
##############################################################################
def plugin_loaded():
# package_logger.addHandler(handler)
package_logger.enable_logging()
version = codemp.version()
logger.debug("plugin loaded - library version: {}".format(version))
def plugin_unloaded():
logger.debug("unloading")
safe_listener_detach(TEXT_LISTENER)
package_logger.disable_logging()
# package_logger.removeHandler(handler)
def kill_all():
for ws in workspaces.lookup():
session.client.leave_workspace(ws.id)
workspaces.remove(ws)
session.stop()
def buffid_from_view(view):
if not is_codemp_buffer(view):
raise ValueError("The view is not a Codemp Buffer.")
buffid = str(view.settings().get(g.CODEMP_BUFFER_ID))
return buffid
def vbuff_form_view(view):
buffid = buffid_from_view(view)
vbuff = buffers.lookupId(buffid)
return vbuff
def objects_from_view(view):
vbuff = vbuff_form_view(view)
vws = buffers.lookupParent(vbuff)
win = workspaces.lookupParent(vws)
return win, vws, vbuff
class CodempBrowseWorkspaceCommand(sublime_plugin.WindowCommand):
def is_enabled(self) -> bool:
return len(workspaces.lookup(self.window)) > 0
def run(self, workspace_id):
wks = workspaces.lookupId(workspace_id)
buffers = wks.handle.fetch_buffers()
QPWorkspaceBrowser(self.window, workspace_id, buffers.wait()).run()
def input(self, args):
if "workspace_id" not in args:
wslist = session.client.active_workspaces()
return SimpleListInput(
("workspace_id", wslist),
)
class CodempBrowseServerCommand(sublime_plugin.WindowCommand):
def is_enabled(self) -> bool:
return session.is_active()
def run(self):
wks = session.get_workspaces()
QPServerBrowser(self.window, session.config.host, wks).run()
class CodempReplaceTextCommand(sublime_plugin.TextCommand):
def run(self, edit, start, end, content, change_id = None):
# we modify the region to account for any change that happened in the mean time
region = sublime.Region(start, end)
if change_id:
region = self.view.transform_region_from(sublime.Region(start, end), change_id)
self.view.replace(edit, region, content)
class CodempSyncBuffer(sublime_plugin.TextCommand):
def is_enabled(self) -> bool:
return is_codemp_buffer(self.view)
def run(self, edit):
buff = vbuff_form_view(self.view)
buff.sync(TEXT_LISTENER)
class EventListener(sublime_plugin.EventListener):
def is_enabled(self):
return session.is_active()
def on_exit(self):
kill_all()
def on_pre_close_window(self, window):
for vws in workspaces.lookup(window):
sublime.run_command("codemp_leave_workspace", {
"workspace_id": vws.id
})
def on_text_command(self, view, command_name, args):
if command_name == "codemp_replace_text":
logger.info("got a codemp_replace_text command!")
def on_post_text_command(self, view, command_name, args):
if command_name == "codemp_replace_text":
logger.info("got a codemp_replace_text command!")
class CodempClientViewEventListener(sublime_plugin.ViewEventListener):
@classmethod
def is_applicable(cls, settings):
return settings.get(g.CODEMP_VIEW_TAG) is not None
@classmethod
def applies_to_primary_view_only(cls):
return False
def on_selection_modified_async(self):
region = self.view.sel()[0]
start = self.view.rowcol(region.begin())
end = self.view.rowcol(region.end())
try:
_, vws, vbuff = objects_from_view(self.view)
except KeyError:
logger.error(f"Could not find buffers associated with the view {self.view}.\
Removing the tag to disable event listener. Reattach.")
# delete the tag so we disable this event listener on the view
del self.view.settings()[g.CODEMP_VIEW_TAG]
return
vws.send_cursor(vbuff.id, start, end)
# logger.debug(f"selection modified! {vws.id}, {vbuff.id} - {start}, {end}")
def on_activated(self):
logger.debug(f"'{self.view}' view activated!")
safe_listener_detach(TEXT_LISTENER)
safe_listener_attach(TEXT_LISTENER, self.view.buffer()) # pyright: ignore
def on_deactivated(self):
logger.debug(f"'{self.view}' view deactivated!")
safe_listener_detach(TEXT_LISTENER) # pyright: ignore
def on_pre_close(self):
if self.view == sublime.active_window().active_view():
logger.debug("closing active view")
safe_listener_detach(TEXT_LISTENER) # pyright: ignore
try:
bid = buffid_from_view(self.view)
some(self.view.window()).run_command(
"codemp_leave_buffer", {"buffer_id": bid})
except KeyError:
return
def on_text_command(self, command_name, args):
if command_name == "codemp_replace_text":
logger.info("got a codemp_replace_text command! but in the view listener")
def on_post_text_command(self, command_name, args):
if command_name == "codemp_replace_text":
logger.info("got a codemp_replace_text command! but in the view listener")
# Proxy Commands ( NOT USED, left just in case we need it again. )
#############################################################################
# class ProxyCodempShareCommand(sublime_plugin.WindowCommand):
# # on_window_command, does not trigger when called from the command palette
# # See: https://github.com/sublimehq/sublime_text/issues/2234
# def run(self, **kwargs):
# self.window.run_command("codemp_share", kwargs)
#
# def input(self, args):
# if 'sublime_buffer' not in args:
# return SublimeBufferPathInputHandler()
#
# def input_description(self):
# return 'Share Buffer:'