added initial automatic cursor color assignment, minor code refactor, and a disconnect client command.

Former-commit-id: 6c4c5761b8b810be24ce8c3d1c0f9f2d8a45e1b4
This commit is contained in:
Camillo Schenone 2023-11-24 10:19:03 +01:00
parent 280d6314c9
commit dd5f8cdf92
2 changed files with 47 additions and 13 deletions

View file

@ -20,9 +20,9 @@
} }
}, },
{ {
"caption": "Codemp: Connect",
// # on_window_command, does not trigger when called from the command palette // # on_window_command, does not trigger when called from the command palette
// # See: https://github.com/sublimehq/sublime_text/issues/2234 // # See: https://github.com/sublimehq/sublime_text/issues/2234
"caption": "Codemp: Connect",
"command": "codemp_connect", "command": "codemp_connect",
"args": { "args": {
// "server_host": "http://[::1]:50051" // "server_host": "http://[::1]:50051"
@ -51,4 +51,10 @@
// 'remote_name' : 'name of buffer to disconnect' // 'remote_name' : 'name of buffer to disconnect'
} }
}, },
{
"caption": "Codemp: Disconnect Client",
"command": "codemp_disconnect",
"arg": {
}
},
] ]

View file

@ -16,6 +16,17 @@ _cursor_controller = None
_txt_change_listener = None _txt_change_listener = None
_exit_handler_id = None _exit_handler_id = None
_palette = [
"var(--redish)",
"var(--orangish)",
"var(--yellowish)",
"var(--greenish)",
"var(--cyanish)",
"var(--bluish)",
"var(--purplish)",
"var(--pinkish)",
]
_regions_colors = [ _regions_colors = [
"region.redish", "region.redish",
"region.orangeish", "region.orangeish",
@ -41,13 +52,10 @@ async def disconnect_client():
# buffers clean up after themselves after detaching # buffers clean up after themselves after detaching
for buff in _buffers: for buff in _buffers:
await buff.detach(_client) await buff.detach(_client)
for task in _tasks: for task in _tasks:
task.cancel() task.cancel()
if _cursor_controller: if _cursor_controller:
await _client.leave_workspace() await _client.leave_workspace()
if _txt_change_listener: if _txt_change_listener:
safe_listener_detach(_txt_change_listener) safe_listener_detach(_txt_change_listener)
@ -69,7 +77,6 @@ def plugin_unloaded():
status_log("unloading") status_log("unloading")
## Utils ## ## Utils ##
############################################################################## ##############################################################################
def status_log(msg): def status_log(msg):
@ -135,8 +142,12 @@ def safe_listener_detach(txt_listener):
async def connect_command(server_host, session): async def connect_command(server_host, session):
global _client global _client
status_log("Connecting to {}".format(server_host)) status_log("Connecting to {}".format(server_host))
try:
await _client.connect(server_host) await _client.connect(server_host)
await join_workspace(session) await join_workspace(session)
except Exception as e:
sublime.error_message("Could not connect:\n {}".format(e))
return
# Workspace and cursor (attaching, sending and receiving) # Workspace and cursor (attaching, sending and receiving)
############################################################################## ##############################################################################
@ -146,10 +157,13 @@ async def join_workspace(session):
status_log("Joining workspace: {}".format(session)) status_log("Joining workspace: {}".format(session))
_cursor_controller = await _client.join(session) _cursor_controller = await _client.join(session)
sublime_asyncio.dispatch(move_cursor(_cursor_controller), store_task("move-cursor")) sublime_asyncio.dispatch(
move_cursor(_cursor_controller),
store_task("move-cursor"))
async def move_cursor(cursor_controller): async def move_cursor(cursor_controller):
global _regions_colors global _regions_colors
global _palette
status_log("spinning up cursor worker...") status_log("spinning up cursor worker...")
# TODO: make the matching user/color more solid. now all users have one color cursor. # TODO: make the matching user/color more solid. now all users have one color cursor.
@ -161,15 +175,19 @@ async def move_cursor(cursor_controller):
if buffer: if buffer:
reg = rowcol_to_region(buffer.view, cursor_event.start, cursor_event.end) reg = rowcol_to_region(buffer.view, cursor_event.start, cursor_event.end)
reg_flags = sublime.RegionFlags.DRAW_EMPTY | sublime.RegionFlags.DRAW_NO_FILL # reg_flags = sublime.RegionFlags.DRAW_EMPTY | sublime.RegionFlags.DRAW_NO_FILL
reg_flags = sublime.RegionFlags.DRAW_EMPTY
user_hash = hash(cursor_event.user)
buffer.view.add_regions( buffer.view.add_regions(
"codemp_cursors", "codemp_cursors",
[reg], [reg],
flags = reg_flags, flags = reg_flags,
scope=_regions_colors[hash(cursor_event.user) % len(_regions_colors)], scope=_regions_colors[user_hash % len(_regions_colors)],
annotations = [cursor_event.user], annotations = [cursor_event.user],
annotation_color="#000") annotation_color=_palette[user_hash % len(_palette)]
)
except asyncio.CancelledError: except asyncio.CancelledError:
status_log("cursor worker stopped...") status_log("cursor worker stopped...")
@ -197,6 +215,7 @@ class CodempSublimeBuffer():
async def attach(self, client): async def attach(self, client):
global _txt_change_listener global _txt_change_listener
global _buffers
status_log("attaching local buffer '{}' to '{}'".format(self.view.file_name(), self.remote_name)) status_log("attaching local buffer '{}' to '{}'".format(self.view.file_name(), self.remote_name))
# attach to the matching codemp buffer # attach to the matching codemp buffer
@ -213,6 +232,7 @@ class CodempSublimeBuffer():
# start the buffer worker that waits for text_changes in the worker thread # start the buffer worker that waits for text_changes in the worker thread
sublime_asyncio.dispatch(self.apply_buffer_change(), store_task(self.worker_task_name)) sublime_asyncio.dispatch(self.apply_buffer_change(), store_task(self.worker_task_name))
_buffers.append(self)
# mark all views associated with the buffer as being connected to codemp # mark all views associated with the buffer as being connected to codemp
for v in self.view.buffer().views(): for v in self.view.buffer().views():
v.set_status("z_codemp_buffer", "[Codemp]") v.set_status("z_codemp_buffer", "[Codemp]")
@ -252,7 +272,6 @@ class CodempSublimeBuffer():
while text_change := await self.controller.recv(): while text_change := await self.controller.recv():
# In case a change arrives to a background buffer, just apply it. We are not listening on it. # In case a change arrives to a background buffer, just apply it. We are not listening on it.
# Otherwise, interrupt the listening to avoid echoing back the change just received. # Otherwise, interrupt the listening to avoid echoing back the change just received.
status_log("recieved txt change: ")
active = is_active(self.view) active = is_active(self.view)
if active: if active:
safe_listener_detach(_txt_change_listener) safe_listener_detach(_txt_change_listener)
@ -374,7 +393,6 @@ async def join_buffer_command(view, remote_name):
try: try:
buffer = CodempSublimeBuffer(view, remote_name) buffer = CodempSublimeBuffer(view, remote_name)
await buffer.attach(_client) await buffer.attach(_client)
_buffers.append(buffer)
## we should receive all contents from the server upon joining. ## we should receive all contents from the server upon joining.
except Exception as e: except Exception as e:
@ -427,6 +445,10 @@ class CodempClientViewEventListener(sublime_plugin.ViewEventListener):
print("view {} deactivated".format(self.view.id())) print("view {} deactivated".format(self.view.id()))
safe_listener_detach(_txt_change_listener) safe_listener_detach(_txt_change_listener)
def on_close(self):
buffer = get_buffer_from_buffer_id(self.view.buffer_id())
buffer.detach()
class CodempClientTextChangeListener(sublime_plugin.TextChangeListener): class CodempClientTextChangeListener(sublime_plugin.TextChangeListener):
@classmethod @classmethod
@ -554,6 +576,12 @@ class RemoteNameInputHandler(sublime_plugin.ListInputHandler):
return ret_list return ret_list
# Disconnect Command
#############################################################################
class CodempDisconnectCommand(sublime_plugin.WindowCommand):
def run(self):
sublime_asyncio.sync(disconnect_client())
# Proxy Commands ( NOT USED ) # Proxy Commands ( NOT USED )
############################################################################# #############################################################################
# class ProxyCodempShareCommand(sublime_plugin.WindowCommand): # class ProxyCodempShareCommand(sublime_plugin.WindowCommand):