From abb027217c899b6f1986026f3f395fb89030ab37 Mon Sep 17 00:00:00 2001 From: Camillo Schenone Date: Wed, 21 Feb 2024 23:59:49 +0100 Subject: [PATCH] Major code refactor, to support v0.6 codemp. Should work, minor details to go through, like internal buffer mappings. Former-commit-id: 3602917d52fd33e9eb77fb5a9fe9a87010e94a03 --- Codemp.sublime-commands | 9 +- plugin.py | 846 ++++++++++++++++------------------------ src/codemp_client.py | 567 +++++++++++++++++++++++---- 3 files changed, 845 insertions(+), 577 deletions(-) diff --git a/Codemp.sublime-commands b/Codemp.sublime-commands index a9690d7..e591a79 100644 --- a/Codemp.sublime-commands +++ b/Codemp.sublime-commands @@ -38,12 +38,19 @@ } }, { - "caption": "Codemp: Join", + "caption": "Codemp: Join Workspace", "command": "codemp_join", "arg": { // 'server_buffer' : 'test' }, }, + { + "caption": "Codemp: Join buffer", + "command": "codemp_attach", + "arg": { + // 'server_buffer' : 'test' + }, + }, { "caption": "Codemp: Disconnect Buffer", "command": "codemp_disconnect_buffer", diff --git a/plugin.py b/plugin.py index ce3ef67..513696e 100644 --- a/plugin.py +++ b/plugin.py @@ -2,621 +2,451 @@ import sublime import sublime_plugin # import Codemp.codemp_client as codemp -from Codemp.src.codemp_client import * - -# we import the PyTextChange type to be able to access its @classmethods: from_diff and index_to_rowcol -# PyTextChange instances are not meant to be created from python, but only received immutable from codemp. -from Codemp.bindings.codemp_client import PyTextChange - -import Codemp.ext.sublime_asyncio as sublime_asyncio -import asyncio -import os -import time +from Codemp.src.codemp_client import ( + VirtualClient, + status_log, + safe_listener_detach, + is_active, +)xs # UGLYYYY, find a way to not have global variables laying around. -_tasks = [] -_buffers = [] _client = None -_cursor_controller = None _txt_change_listener = None -_exit_handler_id = None _palette = [ - "var(--redish)", - "var(--orangish)", - "var(--yellowish)", - "var(--greenish)", - "var(--cyanish)", - "var(--bluish)", - "var(--purplish)", - "var(--pinkish)", + "var(--redish)", + "var(--orangish)", + "var(--yellowish)", + "var(--greenish)", + "var(--cyanish)", + "var(--bluish)", + "var(--purplish)", + "var(--pinkish)", ] _regions_colors = [ - "region.redish", - "region.orangeish", - "region.yellowish", - "region.greenish", - "region.cyanish", - "region.bluish", - "region.purplish", - "region.pinkish" + "region.redish", + "region.orangeish", + "region.yellowish", + "region.greenish", + "region.cyanish", + "region.bluish", + "region.purplish", + "region.pinkish", ] -## Initialisation and Deinitialisation + +# Initialisation and Deinitialisation ############################################################################## -async def disconnect_client(): - global _client - global _cursor_controller - global _buffers - global _txt_change_listener - global _tasks - status_log("disconnecting...") - - # buffers clean up after themselves after detaching - for buff in _buffers: - await buff.detach(_client) - for task in _tasks: - task.cancel() - if _cursor_controller: - await _client.leave_workspace() - if _txt_change_listener: - safe_listener_detach(_txt_change_listener) def plugin_loaded(): - global _client - global _txt_change_listener - global _exit_handler_id - _client = CodempClient() # create an empty instance of the codemp client. - _txt_change_listener = CodempClientTextChangeListener() # instantiate the listener to attach around. - - # instantiate and start a global asyncio event loop. - # pass in the exit_handler coroutine that will be called upon relasing the event loop. - _exit_handler_id = sublime_asyncio.acquire(disconnect_client) - status_log("plugin loaded") + global _client + global _txt_change_listener + + # instantiate and start a global asyncio event loop. + # pass in the exit_handler coroutine that will be called upon relasing the event loop. + _client = VirtualClient(disconnect_client) + _txt_change_listener = CodempClientTextChangeListener() + + status_log("plugin loaded") + + +async def disconnect_client(): + global _client + global _txt_change_listener + + safe_listener_detach(_txt_change_listener) + _client.tm.stop_all() + + for vws in _client.workspaces: + vws.cleanup() + + # fime: allow riconnections + _client = None + def plugin_unloaded(): - sublime_asyncio.release(False, _exit_handler_id) - # disconnect the client. - status_log("unloading") + global _client + # releasing the runtime, runs the disconnect callback defined when acquiring the event loop. + _client.tm.release(False) + status_log("plugin unloaded") -## Utils ## +# Utils ############################################################################## -def status_log(msg): - sublime.status_message("[codemp] {}".format(msg)) - print("[codemp] {}".format(msg)) -def store_task(name = None): - def store_named_task(task): - global _tasks - task.set_name(name) - _tasks.append(task) - - return store_named_task def get_contents(view): - r = sublime.Region(0, view.size()) - return view.substr(r) + r = sublime.Region(0, view.size()) + return view.substr(r) + def populate_view(view, content): - view.run_command("codemp_replace_text", { - "start": 0, - "end": view.size(), - "content": content, - "change_id": view.change_id(), - }) + view.run_command( + "codemp_replace_text", + { + "start": 0, + "end": view.size(), + "content": content, + "change_id": view.change_id(), + }, + ) + def get_view_from_local_path(path): - for window in sublime.windows(): - for view in window.views(): - if view.file_name() == path: - return view - -def rowcol_to_region(view, start, end): - a = view.text_point(start[0], start[1]) - b = view.text_point(end[0], end[1]) - return sublime.Region(a, b) - -def get_buffer_from_buffer_id(buffer_id): - global _buffers - for b in _buffers: - if b.view.buffer_id() == buffer_id: - return b - -def get_buffer_from_remote_name(remote_name): - global _buffers - for b in _buffers: - if b.remote_name == remote_name: - return b - -def is_active(view): - if view.window().active_view() == view: - return True - return False - -def safe_listener_detach(txt_listener): - if txt_listener.is_attached(): - txt_listener.detach() - -## Main logic (build coroutines to be dispatched through sublime_asyncio) -# Connection command -############################################################################## - -async def connect_command(server_host, session): - global _client - status_log("Connecting to {}".format(server_host)) - try: - await _client.connect(server_host) - await join_workspace(session) - except Exception as e: - sublime.error_message("Could not connect:\n Make sure the server is up.") - return - -# Workspace and cursor (attaching, sending and receiving) -############################################################################## -async def join_workspace(session): - global _client - global _cursor_controller - - status_log("Joining workspace: {}".format(session)) - _cursor_controller = await _client.join(session) - sublime_asyncio.dispatch( - move_cursor(_cursor_controller), - store_task("move-cursor")) - -async def move_cursor(cursor_controller): - global _regions_colors - global _palette - - status_log("spinning up cursor worker...") - # TODO: make the matching user/color more solid. now all users have one color cursor. - # Maybe make all cursors the same color and only use annotations as a discriminant. - # idea: use a user id hash map that maps to a color. - try: - while cursor_event := await cursor_controller.recv(): - buffer = get_buffer_from_remote_name(cursor_event.buffer) - - if not buffer: - status_log("Received a cursor event for an unknown buffer: {}".format(cursor_event.buffer)) - continue - - reg = rowcol_to_region(buffer.view, cursor_event.start, cursor_event.end) - reg_flags = sublime.RegionFlags.DRAW_EMPTY # show cursors. - - user_hash = hash(cursor_event.user) - - buffer.view.add_regions( - "codemp-cursors-{}".format(user_hash), - [reg], - flags = reg_flags, - scope=_regions_colors[user_hash % len(_regions_colors)], - annotations = [cursor_event.user], - annotation_color=_palette[user_hash % len(_palette)] - ) - - except asyncio.CancelledError: - status_log("cursor worker stopped...") - -def send_cursor(view): - global _cursor_controller - - buffer_name = get_buffer_from_buffer_id(view.buffer_id()).remote_name - region = view.sel()[0] # TODO: only the last placed cursor/selection. - start = view.rowcol(region.begin()) #only counts UTF8 chars - end = view.rowcol(region.end()) - - _cursor_controller.send(buffer_name, start, end) - -# Buffer Controller (managing text modifications) -############################################################################## - -# This class is used as an abstraction between the local buffers (sublime side) and the -# remote buffers (codemp side), to handle the syncronicity. -class CodempSublimeBuffer(): - def __init__(self, view, remote_name): - self.view = view - self.remote_name = remote_name - self.worker_task_name = "buffer-worker-{}".format(self.remote_name) - - async def attach(self, client): - global _txt_change_listener - global _buffers - - status_log("attaching local buffer '{}' to '{}'".format(self.view.file_name(), self.remote_name)) - # attach to the matching codemp buffer - self.controller = await client.attach(self.remote_name) - - # if the view is already active calling focus_view() will not trigger the on_activate() - if is_active(self.view): - status_log("\tattaching text listener...") - safe_listener_detach(_txt_change_listener) - _txt_change_listener.attach(self.view.buffer()) - else: - self.view.window().focus_view(self.view) - - # 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)) - - _buffers.append(self) - # mark all views associated with the buffer as being connected to codemp - for v in self.view.buffer().views(): - v.set_status("z_codemp_buffer", "[Codemp]") - v.settings()["codemp_buffer"] = True - - async def detach(self, client): - global _txt_change_listener - global _tasks - global _buffers - status_log("detaching buffer '{}' ({})".format(self.remote_name, self.view.file_name())) - - if is_active(self.view): - safe_listener_detach(_txt_change_listener) - - await client.disconnect_buffer(self.remote_name) - - # take down the worker task - for task in _tasks: - if task.get_name() == self.worker_task_name: - task.cancel() - _tasks.remove(task) - break - - # remove yourself from the _buffers - _buffers.remove(self) - - # clean up all the stuff we left around - for v in self.view.buffer().views(): - del v.settings()["codemp_buffer"] - v.erase_status("z_codemp_buffer") - v.erase_regions("codemp_cursors") - - async def apply_buffer_change(self): - global _txt_change_listener - status_log("spinning up '{}' buffer worker...".format(self.remote_name)) - try: - 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. - # Otherwise, interrupt the listening to avoid echoing back the change just received. - if text_change.is_empty(): - status_log("change is empty. skipping.") - continue - - active = is_active(self.view) - if active: - safe_listener_detach(_txt_change_listener) - - # we need to go through a sublime text command, since the method, view.replace - # needs an edit token, that is obtained only when calling a textcommand associated with a view. - self.view.run_command("codemp_replace_text", { - "start": text_change.start_incl, - "end": text_change.end_excl, - "content": text_change.content, - "change_id": self.view.change_id() - }) - - if active: - _txt_change_listener.attach(self.view.buffer()) - - except asyncio.CancelledError: - status_log("'{}' buffer worker stopped...".format(self.remote_name)) - - def send_buffer_change(self, changes): - # Sublime text on_text_changed events, can give a list of changes. - # in case of simple insertion or deletion the change is singular. - # but if we swap a string (select it and add another string in it's place) or have multiple selections - # or do an undo of some kind after the just mentioned events we receive multiple split text changes, - # e.g. select the world `hello` and replace it with `12345`: Sublime will separate it into two singular changes, - # first add `12345` in front of `hello`: `12345hello` then, delete the `hello`. - - # we do not do any index checking, and trust sublime with providing the correct sequential indexing, assuming the - # changes are applied in the order they are received. - for change in changes: - region = sublime.Region(change.a.pt, change.b.pt) - status_log("sending txt change: Reg({} {}) -> '{}'".format(region.begin(), region.end(), change.str)) - self.controller.send(region.begin(), region.end(), change.str) + for window in sublime.windows(): + for view in window.views(): + if view.file_name() == path: + return view -# we call this command manually to have access to the edit token. -class CodempReplaceTextCommand(sublime_plugin.TextCommand): - def run(self, edit, start, end, content, change_id): - # we modify the region to account for any change that happened in the mean time - region = self.view.transform_region_from(sublime.Region(start, end), change_id) - self.view.replace(edit, region, content) +def cleanup_tags(view): + del view.settings()["codemp_buffer"] + view.erase_status("z_codemp_buffer") + view.erase_regions("codemp_cursors") -async def join_buffer_command(view, remote_name): - global _client - global _buffers - try: - buffer = CodempSublimeBuffer(view, remote_name) - await buffer.attach(_client) +def tag(view): + view.set_status("z_codemp_buffer", "[Codemp]") + view.settings()["codemp_buffer"] = True - ## we should receive all contents from the server upon joining. - except Exception as e: - sublime.error_message("Could not join buffer: {}".format(e)) - return +# The main workflow: +# Plugin loads and initialises an empty handle to the client +# The plugin calls connect and populates the handle with a client instance +# We use the client to authenticate and login (to a workspace) to obtain a token +# We join a workspace (either new or existing) -async def share_buffer_command(buffer_path, remote_name = "test"): - global _client - global _buffers - - view = get_view_from_local_path(buffer_path) - contents = get_contents(view) - - try: - await _client.create(remote_name, contents) - await join_buffer_command(view, remote_name) - except Exception as e: - sublime.error_message("Could not share buffer: {}".format(e)) - return - -async def disconnect_buffer_command(buffer): - global _client - await buffer.detach(_client) # Listeners ############################################################################## + class CodempClientViewEventListener(sublime_plugin.ViewEventListener): - @classmethod - def is_applicable(cls, settings): - return settings.get("codemp_buffer", False) + @classmethod + def is_applicable(cls, settings): + return settings.get("codemp_buffer", False) - @classmethod - def applies_to_primary_view_only(cls): - return False + @classmethod + def applies_to_primary_view_only(cls): + return False - def on_selection_modified_async(self): - send_cursor(self.view) + def on_selection_modified_async(self): + global _client + vbuff = _client.active_workspace.get_virtual_by_local(self.view.buffer_id()) + if vbuff is not None: + _client.send_cursor(vbuff) - # We only edit on one view at a time, therefore we only need one TextChangeListener - # Each time we focus a view to write on it, we first attach the listener to that buffer. - # When we defocus, we detach it. - def on_activated(self): - global _txt_change_listener - print("view {} activated".format(self.view.id())) - _txt_change_listener.attach(self.view.buffer()) + # We only edit on one view at a time, therefore we only need one TextChangeListener + # Each time we focus a view to write on it, we first attach the listener to that buffer. + # When we defocus, we detach it. + def on_activated(self): + global _txt_change_listener + print("view {} activated".format(self.view.id())) + _txt_change_listener.attach(self.view.buffer()) - def on_deactivated(self): - global _txt_change_listener - print("view {} deactivated".format(self.view.id())) - safe_listener_detach(_txt_change_listener) + def on_deactivated(self): + global _txt_change_listener + print("view {} deactivated".format(self.view.id())) + safe_listener_detach(_txt_change_listener) - def on_pre_close(self): - global _client - buffer = get_buffer_from_buffer_id(self.view.buffer_id()) - # have to run the detach logic in sync, to keep a valid reference to the view. - sublime_asyncio.sync(buffer.detach(_client)) + def on_text_command(self, command_name, args): + print(self.view.id(), command_name, args) + if command_name == "codemp_replace_text": + print("dry_run: detach text listener") + + def on_post_text_command(self, command_name, args): + print(command_name, args) + if command_name == "codemp_replace_text": + print("dry_run: attach text listener") + + # UPDATE ME + + def on_pre_close(self): + global _client + global _txt_change_listener + if is_active(self.view): + safe_listener_detach(_txt_change_listener) + + vbuff = _client.active_workspace.get_virtual_by_local(self.view.buffer_id()) + vbuff.cleanup() + + print(list(map(lambda x: x.get_name(), _client.tm.tasks))) + task = _client.tm.cancel_and_pop(f"buffer-ctl-{vbuff.codemp_id}") + print(list(map(lambda x: x.get_name(), _client.tm.tasks))) + print(task.cancelled()) + # have to run the detach logic in sync, to keep a valid reference to the view. + # sublime_asyncio.sync(buffer.detach(_client)) class CodempClientTextChangeListener(sublime_plugin.TextChangeListener): - @classmethod - def is_applicable(cls, buffer): - # don't attach this event listener automatically - # we'll do it by hand with .attach(buffer). - return False + @classmethod + def is_applicable(cls, buffer): + # don't attach this event listener automatically + # we'll do it by hand with .attach(buffer). + return False - # lets make this blocking :D - # def on_text_changed_async(self, changes): - def on_text_changed(self, changes): - subl_buffer = get_buffer_from_buffer_id(self.buffer.id()) - subl_buffer.send_buffer_change(changes) + # lets make this blocking :D + # def on_text_changed_async(self, changes): + def on_text_changed(self, changes): + global _client + if ( + self.buffer.primary_view() + .settings() + .get("codemp_ignore_next_on_modified_text_event", None) + ): + status_log("ignoring echoing back the change.") + self.view.settings()["codemp_ignore_next_on_modified_text_event"] = False + return + vbuff = _client.active_workspace.get_virtual_by_local(self.buffer.id()) + _client.send_buffer_change(changes, vbuff) # Commands: -# codemp_connect: connect to a server. -# codemp_join: join a workspace with a given name within the server. -# codemp_share: shares a buffer with a given name in the workspace. +# codemp_connect: connect to a server. +# codemp_join: join a workspace with a given name within the server. +# codemp_share: shares a buffer with a given name in the workspace. # # Internal commands: -# replace_text: swaps the content of a view with the given text. +# replace_text: swaps the content of a view with the given text. # # Connect Command ############################################################################# class CodempConnectCommand(sublime_plugin.WindowCommand): - def run(self, server_host, session): - sublime_asyncio.dispatch(connect_command(server_host, session)) + def run(self, server_host): + global _client + sublime_asyncio.dispatch(_client.connect(server_host)) - def input(self, args): - if 'server_host' not in args: - return ServerHostInputHandler() + def input(self, args): + if "server_host" not in args: + return ServerHostInputHandler() + + def input_description(self): + return "Server host:" - def input_description(self): - return 'Server host:' class ServerHostInputHandler(sublime_plugin.TextInputHandler): - def initial_text(self): - return "http://127.0.0.1:50051" - - def next_input(self, args): - if 'workspace' not in args: - return CodempWorkspaceInputHandler() - -class CodempWorkspaceInputHandler(sublime_plugin.TextInputHandler): - def name(self): - return 'session' - def initial_text(self): - return "default" + def initial_text(self): + return "http://127.0.0.1:50051" - -# Join Command +# Join Workspace Command ############################################################################# class CodempJoinCommand(sublime_plugin.WindowCommand): - def run(self, server_buffer): - view = self.window.new_file(flags=sublime.NewFileFlags.TRANSIENT) - sublime_asyncio.dispatch(join_buffer_command(view, server_buffer)) - - def input_description(self): - return 'Join Buffer:' + def run(self, workspace_id): + global _client + sublime_asyncio.dispatch(_client.join_workspace(workspace_id)) - def input(self, args): - if 'server_buffer' not in args: - return ServerBufferInputHandler() + def input_description(self): + return "Join Workspace:" -class ServerBufferInputHandler(sublime_plugin.TextInputHandler): - def initial_text(self): - return "What buffer should I join?" + def input(self, args): + if "workspace_id" not in args: + return WorkspaceIdInputHandler() + + +class WorkspaceIdInputHandler(sublime_plugin.TextInputHandler): + def initial_text(self): + return "What workspace should I join?" + + +# Join Buffer Command +############################################################################# +class CodempAttachCommand(sublime_plugin.WindowCommand): + def run(self, buffer_id): + global _client + if _client.active_workspace is not None: + sublime_asyncio.dispatch(_client.active_workspace.attach(buffer_id)) + else: + sublime.error_message( + "You haven't joined any worksapce yet. use `Codemp: Join Workspace`" + ) + + def input_description(self): + return "Join Buffer in workspace:" + + # This is awful, fix it + def input(self, args): + global _client + if _client.active_workspace is not None: + if "buffer_id" not in args: + existing_buffers = _client.active_workspace.handle.filetree() + if len(existing_buffers) == 0: + return BufferIdInputHandler() + else: + return ListBufferIdInputHandler() + else: + sublime.error_message( + "You haven't joined any worksapce yet. use `Codemp: Join Workspace`" + ) + return + + +class BufferIdInputHandler(sublime_plugin.TextInputHandler): + def initial_text(self): + return "Create New Buffer:" + + +class ListBufferIdInputHandler(sublime_plugin.ListInputHandler): + def name(self): + return "buffer_id" + + def list_items(self): + global _client + return _client.active_workspace.handle.filetree() + + def next_input(self, args): + if "buffer_id" not in args: + return BufferIdInputHandler() + + +# Text Change Command +############################################################################# +# we call this command manually to have access to the edit token. +class CodempReplaceTextCommand(sublime_plugin.TextCommand): + def run(self, edit, start, end, content, change_id): + # we modify the region to account for any change that happened in the mean time + print("running the replace command, launche manually.") + region = self.view.transform_region_from(sublime.Region(start, end), change_id) + self.view.replace(edit, region, content) # Share Command -############################################################################# -class CodempShareCommand(sublime_plugin.WindowCommand): - def run(self, sublime_buffer_path, server_id): - sublime_asyncio.dispatch(share_buffer_command(sublime_buffer_path, server_id)) - - def input(self, args): - if 'sublime_buffer' not in args: - return SublimeBufferPathInputHandler() +# ############################################################################# +# class CodempShareCommand(sublime_plugin.WindowCommand): +# def run(self, sublime_buffer_path, server_id): +# sublime_asyncio.dispatch(share_buffer_command(sublime_buffer_path, server_id)) - def input_description(self): - return 'Share Buffer:' +# def input(self, args): +# if "sublime_buffer" not in args: +# return SublimeBufferPathInputHandler() -class SublimeBufferPathInputHandler(sublime_plugin.ListInputHandler): - def list_items(self): - ret_list = [] +# def input_description(self): +# return "Share Buffer:" - for window in sublime.windows(): - for view in window.views(): - if view.file_name(): - ret_list.append(view.file_name()) - return ret_list +# class SublimeBufferPathInputHandler(sublime_plugin.ListInputHandler): +# def list_items(self): +# ret_list = [] - def next_input(self, args): - if 'server_id' not in args: - return ServerIdInputHandler() +# for window in sublime.windows(): +# for view in window.views(): +# if view.file_name(): +# ret_list.append(view.file_name()) -class ServerIdInputHandler(sublime_plugin.TextInputHandler): - def initial_text(self): - return "Buffer name on server" +# return ret_list -# Disconnect Buffer Command -############################################################################# -class CodempDisconnectBufferCommand(sublime_plugin.WindowCommand): - def run(self, remote_name): - buffer = get_buffer_from_remote_name(remote_name) - sublime_asyncio.dispatch(disconnect_buffer_command(buffer)) - - def input(self, args): - if 'remote_name' not in args: - return RemoteNameInputHandler() +# def next_input(self, args): +# if "server_id" not in args: +# return ServerIdInputHandler() - def input_description(self): - return 'Disconnect Buffer:' -class RemoteNameInputHandler(sublime_plugin.ListInputHandler): - def list_items(self): - global _buffers - ret_list = [] +# class ServerIdInputHandler(sublime_plugin.TextInputHandler): +# def initial_text(self): +# return "Buffer name on server" - for buff in _buffers: - ret_list.append(buff.remote_name) - - return ret_list # Disconnect Command ############################################################################# class CodempDisconnectCommand(sublime_plugin.WindowCommand): - def run(self): - sublime_asyncio.sync(disconnect_client()) + def run(self): + sublime_asyncio.sync(disconnect_client()) + # Proxy Commands ( NOT USED ) ############################################################################# # 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) +# # 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(self, args): +# if 'sublime_buffer' not in args: +# return SublimeBufferPathInputHandler() # -# def input_description(self): -# return 'Share Buffer:' +# def input_description(self): +# return 'Share Buffer:' # # class ProxyCodempJoinCommand(sublime_plugin.WindowCommand): -# def run(self, **kwargs): -# self.window.run_command("codemp_join", kwargs) +# def run(self, **kwargs): +# self.window.run_command("codemp_join", kwargs) # -# def input(self, args): -# if 'server_buffer' not in args: -# return ServerBufferInputHandler() +# def input(self, args): +# if 'server_buffer' not in args: +# return ServerBufferInputHandler() # -# def input_description(self): -# return 'Join Buffer:' +# def input_description(self): +# return 'Join Buffer:' # # class ProxyCodempConnectCommand(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_connect", kwargs) +# # 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_connect", kwargs) # -# def input(self, args): -# if 'server_host' not in args: -# return ServerHostInputHandler() +# def input(self, args): +# if 'server_host' not in args: +# return ServerHostInputHandler() # -# def input_description(self): -# return 'Server host:' +# def input_description(self): +# return 'Server host:' -## NOT NEEDED ANYMORE +# NOT NEEDED ANYMORE # def compress_change_region(changes): -# # the bounding region of all text changes. -# txt_a = float("inf") -# txt_b = 0 +# # the bounding region of all text changes. +# txt_a = float("inf") +# txt_b = 0 -# # the region in the original buffer subjected to the change. -# reg_a = float("inf") -# reg_b = 0 +# # the region in the original buffer subjected to the change. +# reg_a = float("inf") +# reg_b = 0 -# # we keep track of how much the changes move the indexing of the buffer -# buffer_shift = 0 # left - + right +# # we keep track of how much the changes move the indexing of the buffer +# buffer_shift = 0 # left - + right -# for change in changes: -# # the change in characters that the change would bring -# # len(str) and .len_utf8 are mutually exclusive -# # len(str) is when we insert new text at a position -# # .len_utf8 is the length of the deleted/canceled string in the buffer -# change_delta = len(change.str) - change.len_utf8 +# for change in changes: +# # the change in characters that the change would bring +# # len(str) and .len_utf8 are mutually exclusive +# # len(str) is when we insert new text at a position +# # .len_utf8 is the length of the deleted/canceled string in the buffer +# change_delta = len(change.str) - change.len_utf8 -# # the text region is enlarged to the left -# txt_a = min(txt_a, change.a.pt) +# # the text region is enlarged to the left +# txt_a = min(txt_a, change.a.pt) -# # On insertion, change.b.pt == change.a.pt -# # If we meet a new insertion further than the current window -# # we expand to the right by that change. -# # On deletion, change.a.pt == change.b.pt - change.len_utf8 -# # when we delete a selection and it is further than the current window -# # we enlarge to the right up until the begin of the deleted region. -# if change.b.pt > txt_b: -# txt_b = change.b.pt + change_delta -# else: -# # otherwise we just shift the window according to the change -# txt_b += change_delta +# # On insertion, change.b.pt == change.a.pt +# # If we meet a new insertion further than the current window +# # we expand to the right by that change. +# # On deletion, change.a.pt == change.b.pt - change.len_utf8 +# # when we delete a selection and it is further than the current window +# # we enlarge to the right up until the begin of the deleted region. +# if change.b.pt > txt_b: +# txt_b = change.b.pt + change_delta +# else: +# # otherwise we just shift the window according to the change +# txt_b += change_delta -# # the bounding region enlarged to the left -# reg_a = min(reg_a, change.a.pt) +# # the bounding region enlarged to the left +# reg_a = min(reg_a, change.a.pt) -# # In this bit, we want to look at the buffer BEFORE the modifications -# # but we are working on the buffer modified by all previous changes for each loop -# # we use buffer_shift to keep track of how the buffer shifts around -# # to map back to the correct index for each change in the unmodified buffer. -# if change.b.pt + buffer_shift > reg_b: -# # we only enlarge if we have changes that exceede on the right the current window -# reg_b = change.b.pt + buffer_shift +# # In this bit, we want to look at the buffer BEFORE the modifications +# # but we are working on the buffer modified by all previous changes for each loop +# # we use buffer_shift to keep track of how the buffer shifts around +# # to map back to the correct index for each change in the unmodified buffer. +# if change.b.pt + buffer_shift > reg_b: +# # we only enlarge if we have changes that exceede on the right the current window +# reg_b = change.b.pt + buffer_shift -# # after using the change delta, we archive it for the next iterations -# # the minus is just for being able to "add" the buffer shift with a +. -# # since we encode deleted text as negative in the change_delta, but that requires the shift to the -# # old position to be positive, and viceversa for text insertion. -# buffer_shift -= change_delta +# # after using the change delta, we archive it for the next iterations +# # the minus is just for being able to "add" the buffer shift with a +. +# # since we encode deleted text as negative in the change_delta, but that requires the shift to the +# # old position to be positive, and viceversa for text insertion. +# buffer_shift -= change_delta -# # print("\t[buff change]", change.a.pt, change.str, "(", change.len_utf8,")", change.b.pt) +# # print("\t[buff change]", change.a.pt, change.str, "(", change.len_utf8,")", change.b.pt) -# # print("[walking txt]", "[", txt_a, txt_b, "]", txt) -# # print("[walking reg]", "[", reg_a, reg_b, "]") -# return reg_a, reg_b +# # print("[walking txt]", "[", txt_a, txt_b, "]", txt) +# # print("[walking reg]", "[", reg_a, reg_b, "]") +# return reg_a, reg_b diff --git a/src/codemp_client.py b/src/codemp_client.py index 8a06ce9..75ce487 100644 --- a/src/codemp_client.py +++ b/src/codemp_client.py @@ -1,77 +1,508 @@ -import asyncio -import Codemp.bindings.codemp_client as libcodemp +from __future__ import annotations +from typing import Optional, Callable -# These are helper wrappers, not very interesting +import sublime +import sublime_plugin -class CodempClient(): +import Codemp.ext.sublime_asyncio as sublime_asyncio - def __init__(self): - self.handle = libcodemp.codemp_init() -## Bindings - async def connect(self, server_host): # -> None - await self.handle.connect(server_host) - - async def join(self, session): # -> CursorController - return CursorController(await self.handle.join(session)) +import asyncio # noqa: F401 +import typing # noqa: F401 +import tempfile +import os +from Codemp.bindings.codemp_client import codemp_init, PyCursorEvent, PyTextChange, PyId - async def create(self, path, content=None): # -> None - await self.handle.create(path, content) - - async def attach(self, path): # -> BufferController - return BufferController(await self.handle.attach(path)) - - async def get_cursor(self): # -> CursorController - return CursorController(await self.handle.get_cursor()) - - async def get_buffer(self, path): # -> BufferController - return BufferController(await self.handle.get_buffer()) - - async def leave_workspace(self): # -> None - await self.handle.leave_workspace() - - async def disconnect_buffer(self, path): # -> None - await self.handle.disconnect_buffer(path) - - async def select_buffer(self): # -> String - await self.handle.select_buffer() - -class CursorController(): - def __init__(self, handle): - self.handle = handle - - def send(self, buffer_id, start, end): # -> None - self.handle.send(buffer_id, start, end) - - def try_recv(self): # -> Optional[CursorEvent] - return self.handle.try_recv() - - async def recv(self): # -> CursorEvent - return await self.handle.recv() - - async def poll(self): # -> None - # await until new cursor event, then returns - return await self.handle.poll() - -class BufferController(): - def __init__(self, handle): - self.handle = handle - - def content(self): # -> String - return self.content() - - def send(self, start, end, txt): # -> None - self.handle.send(start, end, txt) - - def try_recv(self): # -> Optional[TextChange] - return self.handle.try_recv() - - async def recv(self): # -> TextChange - return await self.handle.recv() - - async def poll(self): # -> ?? - return await self.handle.poll() +# Some utility functions +def status_log(msg): + sublime.status_message("[codemp] {}".format(msg)) + print("[codemp] {}".format(msg)) +def rowcol_to_region(view, start, end): + a = view.text_point(start[0], start[1]) + b = view.text_point(end[0], end[1]) + return sublime.Region(a, b) + +def is_active(view): + if view.window().active_view() == view: + return True + return False + + +def safe_listener_detach(txt_listener): + if txt_listener.is_attached(): + txt_listener.detach() + + +############################################################################### + + +# This class is used as an abstraction between the local buffers (sublime side) and the +# remote buffers (codemp side), to handle the syncronicity. +# This class is mainly manipulated by a VirtualWorkspace, that manages its buffers +# using this abstract class +class VirtualBuffer: + def __init__( + self, + view: sublime.View, + remote_id: str, + workspace: VirtualWorkspace, + buffctl: BufferController, + ): + self.view = view + self.codemp_id = remote_id + self.sublime_id = view.buffer_id() + self.worker_task_name = "buffer-worker-{}".format(self.codemp_id) + + self.workspace = workspace + self.buffctl = buffctl + + self.tmpfile = os.path.join(workspace.rootdir, self.codemp_id) + + self.view.set_name(self.codemp_id) + open(self.tmpfile, "a").close() + self.view.retarget(self.tmpfile) + self.view.set_scratch(True) + + # mark the view as a codemp view + self.view.set_status("z_codemp_buffer", "[Codemp]") + self.view.settings()["codemp_buffer"] = True + + # # start the buffer worker that waits for text_changes in the worker thread + # sublime_asyncio.dispatch( + # self.apply_buffer_change_task(), store_task(self.worker_task_name) + # ) + + def cleanup(self): + os.remove(self.tmpfile) + # cleanup views + del self.view.settings()["codemp_buffer"] + self.view.erase_status("z_codemp_buffer") + self.view.erase_regions("codemp_cursors") + + # the text listener should be detached by the event listener + # on close and on_deactivated events. + + +# A virtual workspace is a bridge class that aims to translate +# events that happen to the codemp workspaces into sublime actions + + +class VirtualWorkspace: + def __init__(self, client: VirtualClient, workspace_id: str, handle: Workspace): + self.id = workspace_id + self.sublime_window = sublime.active_window() + self.client = client + self.handle = handle + self.curctl = handle.cursor() + + self.active_buffers: list[VirtualBuffer] = [] + + # REMEMBER TO DELETE THE TEMP STUFF! + # initialise the virtual filesystem + tmpdir = tempfile.mkdtemp(prefix="codemp_") + status_log("setting up virtual fs for workspace in: {} ".format(tmpdir)) + self.rootdir = tmpdir + + # and add a new "project folder" + proj_data = self.sublime_window.project_data() + if proj_data is None: + proj_data = {"folders": []} + proj_data["folders"].append( + {"name": "CODEMP::" + workspace_id, "path": self.rootdir} + ) + self.sublime_window.set_project_data(proj_data) + + # start the event listener? + + def cleanup(self): + # the worskpace only cares about closing the various open views on its buffers. + # the event listener calls the cleanup code for each buffer independently on its own. + for vbuff in self.active_buffers: + vbuff.view.close() + + d = self.sublime_window.project_data() + newf = filter(lambda F: not F["name"].startwith("CODEMP::"), d["folders"]) + d["folders"] = newf + self.sublime_window.set_project_data(d) + + os.removedirs(self.rootdir) + + def get_virtual_by_local(self, id: str) -> Optional[VirtualBuffer]: + return next( + (vbuff for vbuff in self.active_buffers if vbuff.sublime_id == id), None + ) + + def get_virtual_by_remote(self, id: str) -> Optional[VirtualBuffer]: + return next( + (vbuff for vbuff in self.active_buffers if vbuff.codemp_id == id), None + ) + + async def attach(self, id: str): + if id is None: + status_log("can't attach if buffer does not exist, aborting.") + return + + await self.handle.fetch_buffers() + existing_buffers = self.handle.filetree() + if id not in existing_buffers: + try: + await self.handle.create(id) + except Exception as e: + status_log(f"could not create buffer: {e}") + return + + try: + buff_ctl = await self.handle.attach(id) + except Exception as e: + status_log(f"error when attaching to buffer '{id}': {e}") + return + + # REMEMBER TO DEAL WITH DELETING THESE THINGS! + view = self.sublime_window.new_file() + vbuff = VirtualBuffer(view, id, self, buff_ctl) + self.active_buffers.append(vbuff) + + self.client.spawn_buffer_manager(vbuff) + + # if the view is already active calling focus_view() will not trigger the on_activate() + self.sublime_window.focus_view(view) + + +class VirtualClient: + def __init__(self, on_exit: Callable = None): + self.handle: Client = Client() + self.workspaces: list[VirtualWorkspace] = [] + self.active_workspace: VirtualWorkspace = None + self.tm = TaskManager(on_exit) + + self.change_clock = 0 + + def make_active(self, ws: VirtualWorkspace): + # TODO: Logic to deal with swapping to and from workspaces, + # what happens to the cursor tasks etc.. + if self.active_workspace is not None: + self.tm.stop_and_pop(f"move-cursor-{self.active_workspace.id}") + self.active_workspace = ws + self.spawn_cursor_manager(ws) + + def get_virtual_local(self, id: str) -> Optional[VirtualWorkspace]: + # get's the workspace that contains a buffer + next( + ( + vws + for vws in self.workspaces + if vws.get_virtual_by_local(id) is not None + ), + None, + ) + + def get_virtual_remote(self, id: str) -> Optional[VirtualWorkspace]: + # get's the workspace that contains a buffer + next( + ( + vws + for vws in self.workspaces + if vws.get_virtual_by_remote(id) is not None + ), + None, + ) + + async def connect(self, server_host: str): + status_log(f"Connecting to {server_host}") + try: + await self.handle.connect(server_host) + except Exception: + sublime.error_message("Could not connect:\n Make sure the server is up.") + return + + id = await self.handle.user_id() + print(f"TEST: {id}") + + async def join_workspace( + self, workspace_id: str, user="sublime", password="lmaodefaultpassword" + ): + try: + status_log(f"Logging into workspace: '{workspace_id}'") + await self.handle.login(user, password, workspace_id) + except Exception as e: + sublime.error_message(f"Failed to login to workspace '{workspace_id}': {e}") + return + + try: + status_log(f"Joining workspace: '{workspace_id}'") + workspace_handle = await self.handle.join_workspace(workspace_id) + except Exception as e: + sublime.error_message(f"Could not join workspace '{workspace_id}': {e}") + return + + print(workspace_handle.id()) + + # here we should also start the workspace event watcher task + vws = VirtualWorkspace(self, workspace_id, workspace_handle) + self.make_active(vws) + self.workspaces.append(vws) + + def spawn_cursor_manager(self, virtual_workspace: VirtualWorkspace): + async def move_cursor_task(vws): + global _regions_colors + global _palette + + status_log(f"spinning up cursor worker for workspace '{vws.id}'...") + # TODO: make the matching user/color more solid. now all users have one color cursor. + # Maybe make all cursors the same color and only use annotations as a discriminant. + # idea: use a user id hash map that maps to a color. + try: + while cursor_event := await vws.curctl.recv(): + vbuff = vws.get_virtual_by_remote(cursor_event.buffer) + + if vbuff is None: + status_log( + f"Received a cursor event for an unknown or inactive buffer: {cursor_event.buffer}" + ) + continue + + reg = rowcol_to_region( + vbuff.view, cursor_event.start, cursor_event.end + ) + reg_flags = sublime.RegionFlags.DRAW_EMPTY # show cursors. + + user_hash = hash(cursor_event.user) + + vbuff.view.add_regions( + f"codemp-cursors-{user_hash}", + [reg], + flags=reg_flags, + scope=_regions_colors[user_hash % len(_regions_colors)], + annotations=[cursor_event.user], + annotation_color=_palette[user_hash % len(_palette)], + ) + + except asyncio.CancelledError: + status_log(f"cursor worker for '{vws.id}' stopped...") + return + + self.tm.dispatch( + move_cursor_task(virtual_workspace), f"cursor-ctl-{virtual_workspace.id}" + ) + + def send_cursor(self, vbuff: VirtualBuffer): + # TODO: only the last placed cursor/selection. + status_log(f"sending cursor position in workspace: {vbuff.workspace.id}") + region = vbuff.view.sel()[0] + start = vbuff.view.rowcol(region.begin()) # only counts UTF8 chars + end = vbuff.view.rowcol(region.end()) + + vbuff.workspace.curctl.send(vbuff.codemp_id, start, end) + + def spawn_buffer_manager(self, vbuff: VirtualBuffer): + status_log("spawning buffer manager") + + async def apply_buffer_change_task(vb): + status_log(f"spinning up '{vb.codemp_id}' buffer worker...") + try: + while text_change := await vb.buffctl.recv(): + # 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. + if text_change.is_empty(): + status_log("change is empty. skipping.") + continue + + vb.view.settings()[ + "codemp_ignore_next_on_modified_text_event" + ] = True + + # we need to go through a sublime text command, since the method, + # view.replace needs an edit token, that is obtained only when calling + # a textcommand associated with a view. + vb.view.run_command( + "codemp_replace_text", + { + "start": text_change.start_incl, + "end": text_change.end_excl, + "content": text_change.content, + "change_id": vb.view.change_id(), + }, + ) + + except asyncio.CancelledError: + status_log("'{}' buffer worker stopped...".format(vb.codemp_id)) + + self.tm.dispatch( + apply_buffer_change_task(vbuff), f"buffer-ctl-{vbuff.codemp_id}" + ) + + def send_buffer_change(self, changes, vbuff: VirtualBuffer): + # we do not do any index checking, and trust sublime with providing the correct + # sequential indexing, assuming the changes are applied in the order they are received. + for change in changes: + region = sublime.Region(change.a.pt, change.b.pt) + status_log( + "sending txt change: Reg({} {}) -> '{}'".format( + region.begin(), region.end(), change.str + ) + ) + vbuff.buffctl.send(region.begin(), region.end(), change.str) + + +class TaskManager: + def __init__(self, exit_handler): + self.tasks = [] + self.exit_handler_id = sublime_asyncio.acquire(exit_handler) + + def release(self, at_exit): + sublime_asyncio.release(at_exit, self.exit_handler_id) + + def dispatch(self, coro, name): + sublime_asyncio.dispatch(coro, self.store_named_lambda(name)) + + def sync(self, coro): + sublime_asyncio.sync(coro) + + def store(self, task): + self.tasks.append(task) + + def store_named(self, task, name=None): + task.set_name(name) + self.store(task) + + def store_named_lambda(self, name): + def _store(task): + task.set_name(name) + self.store(task) + + return _store + + def get_task(self, name) -> Optional: + return next((t for t in self.tasks if t.get_name() == name), None) + + def get_task_idx(self, name) -> Optional: + return next( + (i for (i, t) in enumerate(self.tasks) if t.get_name() == name), None + ) + + def pop_task(self, name) -> Optional: + idx = self.get_task_idx(name) + if id is not None: + return self.task.pop(idx) + return None + + def stop(self, name): + t = self.get_task(name) + if t is not None: + t.cancel() + + def stop_and_pop(self, name) -> Optional: + idx, task = next( + ((i, t) for (i, t) in enumerate(self.tasks) if t.get_name() == name), + (None, None), + ) + if idx is not None: + task.cancel() + return self.tasks.pop(idx) + + def stop_all(self): + for task in self.tasks: + task.cancel() + +###################################################################################### +# These are helper wrappers, that wrap the coroutines returned from the +# pyo3 bindings into usable awaitable functions. +# These should not be directly extended but rather use the higher level "virtual" counterparts above. + +# All methods, without an explicit 'noexcept' are to be treated as failable +# and can throw an error + + +class CursorController: + def __init__(self, handle) -> None: # noexcept + self.handle = handle + + def send(self, path: str, start: tuple[int, int], end: tuple[int, int]) -> None: + self.handle.send(path, start, end) + + def try_recv(self) -> Optional[PyCursorEvent]: + return self.handle.try_recv() + + async def recv(self) -> PyCursorEvent: + return await self.handle.recv() + + async def poll(self) -> None: + # await until new cursor event, then returns + return await self.handle.poll() + + +class BufferController: + def __init__(self, handle) -> None: # noexcept + self.handle = handle + + def send(self, start: int, end: int, txt: str) -> None: + self.handle.send(start, end, txt) + + def try_recv(self) -> Optional[PyTextChange]: + return self.handle.try_recv() + + async def recv(self) -> PyTextChange: + return await self.handle.recv() + + async def poll(self) -> None: + return await self.handle.poll() + + +class Workspace: + def __init__(self, handle) -> None: # noexcept + self.handle = handle + + async def create(self, path: str) -> None: + await self.handle.create(path) + + async def attach(self, path: str) -> BufferController: + return BufferController(await self.handle.attach(path)) + + async def fetch_buffers(self) -> None: + await self.handle.fetch_buffers() + + async def fetch_users(self) -> None: + await self.handle.fetch_users() + + async def list_buffer_users(self, path: str) -> list[PyId]: + return await self.handle.list_buffer_users(path) + + async def delete(self, path) -> None: + await self.handle.delete(path) + + def id(self) -> str: # noexcept + return self.handle.id() + + def cursor(self) -> CursorController: + return CursorController(self.handle.cursor()) + + def buffer_by_name(self, path) -> BufferController: + return BufferController(self.handle.buffer_by_name(path)) + + def filetree(self) -> list[str]: # noexcept + return self.handle.filetree() + + +class Client: + def __init__(self) -> None: + self.handle = codemp_init() + + async def connect(self, server_host: str) -> None: + await self.handle.connect(server_host) + + async def login(self, user: str, password: str, workspace: Optional[str]) -> None: + await self.handle.login(user, password, workspace) + + async def join_workspace(self, workspace: str) -> Workspace: + return Workspace(await self.handle.join_workspace(workspace)) + + async def get_workspace(self, id: str) -> Optional[Workspace]: + return Workspace(await self.handle.get_workspace(id)) + + async def user_id(self) -> str: + return await self.handle.user_id()