mirror of
https://github.com/hexedtech/codemp-sublime.git
synced 2024-12-04 20:04:52 +01:00
Major code refactor, to support v0.6 codemp. Should work, minor details to go through, like internal buffer mappings.
Former-commit-id: 3602917d52fd33e9eb77fb5a9fe9a87010e94a03
This commit is contained in:
parent
6831c07a64
commit
a986aa18c6
3 changed files with 845 additions and 577 deletions
|
@ -38,12 +38,19 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"caption": "Codemp: Join",
|
"caption": "Codemp: Join Workspace",
|
||||||
"command": "codemp_join",
|
"command": "codemp_join",
|
||||||
"arg": {
|
"arg": {
|
||||||
// 'server_buffer' : 'test'
|
// 'server_buffer' : 'test'
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"caption": "Codemp: Join buffer",
|
||||||
|
"command": "codemp_attach",
|
||||||
|
"arg": {
|
||||||
|
// 'server_buffer' : 'test'
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"caption": "Codemp: Disconnect Buffer",
|
"caption": "Codemp: Disconnect Buffer",
|
||||||
"command": "codemp_disconnect_buffer",
|
"command": "codemp_disconnect_buffer",
|
||||||
|
|
846
plugin.py
846
plugin.py
|
@ -2,621 +2,451 @@ import sublime
|
||||||
import sublime_plugin
|
import sublime_plugin
|
||||||
|
|
||||||
# import Codemp.codemp_client as codemp
|
# import Codemp.codemp_client as codemp
|
||||||
from Codemp.src.codemp_client import *
|
from Codemp.src.codemp_client import (
|
||||||
|
VirtualClient,
|
||||||
# we import the PyTextChange type to be able to access its @classmethods: from_diff and index_to_rowcol
|
status_log,
|
||||||
# PyTextChange instances are not meant to be created from python, but only received immutable from codemp.
|
safe_listener_detach,
|
||||||
from Codemp.bindings.codemp_client import PyTextChange
|
is_active,
|
||||||
|
)xs
|
||||||
import Codemp.ext.sublime_asyncio as sublime_asyncio
|
|
||||||
import asyncio
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
|
|
||||||
# UGLYYYY, find a way to not have global variables laying around.
|
# UGLYYYY, find a way to not have global variables laying around.
|
||||||
_tasks = []
|
|
||||||
_buffers = []
|
|
||||||
_client = None
|
_client = None
|
||||||
_cursor_controller = None
|
|
||||||
_txt_change_listener = None
|
_txt_change_listener = None
|
||||||
_exit_handler_id = None
|
|
||||||
|
|
||||||
_palette = [
|
_palette = [
|
||||||
"var(--redish)",
|
"var(--redish)",
|
||||||
"var(--orangish)",
|
"var(--orangish)",
|
||||||
"var(--yellowish)",
|
"var(--yellowish)",
|
||||||
"var(--greenish)",
|
"var(--greenish)",
|
||||||
"var(--cyanish)",
|
"var(--cyanish)",
|
||||||
"var(--bluish)",
|
"var(--bluish)",
|
||||||
"var(--purplish)",
|
"var(--purplish)",
|
||||||
"var(--pinkish)",
|
"var(--pinkish)",
|
||||||
]
|
]
|
||||||
|
|
||||||
_regions_colors = [
|
_regions_colors = [
|
||||||
"region.redish",
|
"region.redish",
|
||||||
"region.orangeish",
|
"region.orangeish",
|
||||||
"region.yellowish",
|
"region.yellowish",
|
||||||
"region.greenish",
|
"region.greenish",
|
||||||
"region.cyanish",
|
"region.cyanish",
|
||||||
"region.bluish",
|
"region.bluish",
|
||||||
"region.purplish",
|
"region.purplish",
|
||||||
"region.pinkish"
|
"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():
|
def plugin_loaded():
|
||||||
global _client
|
global _client
|
||||||
global _txt_change_listener
|
global _txt_change_listener
|
||||||
global _exit_handler_id
|
|
||||||
_client = CodempClient() # create an empty instance of the codemp client.
|
# instantiate and start a global asyncio event loop.
|
||||||
_txt_change_listener = CodempClientTextChangeListener() # instantiate the listener to attach around.
|
# pass in the exit_handler coroutine that will be called upon relasing the event loop.
|
||||||
|
_client = VirtualClient(disconnect_client)
|
||||||
# instantiate and start a global asyncio event loop.
|
_txt_change_listener = CodempClientTextChangeListener()
|
||||||
# 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")
|
||||||
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():
|
def plugin_unloaded():
|
||||||
sublime_asyncio.release(False, _exit_handler_id)
|
global _client
|
||||||
# disconnect the client.
|
# releasing the runtime, runs the disconnect callback defined when acquiring the event loop.
|
||||||
status_log("unloading")
|
_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):
|
def get_contents(view):
|
||||||
r = sublime.Region(0, view.size())
|
r = sublime.Region(0, view.size())
|
||||||
return view.substr(r)
|
return view.substr(r)
|
||||||
|
|
||||||
|
|
||||||
def populate_view(view, content):
|
def populate_view(view, content):
|
||||||
view.run_command("codemp_replace_text", {
|
view.run_command(
|
||||||
"start": 0,
|
"codemp_replace_text",
|
||||||
"end": view.size(),
|
{
|
||||||
"content": content,
|
"start": 0,
|
||||||
"change_id": view.change_id(),
|
"end": view.size(),
|
||||||
})
|
"content": content,
|
||||||
|
"change_id": view.change_id(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_view_from_local_path(path):
|
def get_view_from_local_path(path):
|
||||||
for window in sublime.windows():
|
for window in sublime.windows():
|
||||||
for view in window.views():
|
for view in window.views():
|
||||||
if view.file_name() == path:
|
if view.file_name() == path:
|
||||||
return view
|
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)
|
|
||||||
|
|
||||||
|
|
||||||
# we call this command manually to have access to the edit token.
|
def cleanup_tags(view):
|
||||||
class CodempReplaceTextCommand(sublime_plugin.TextCommand):
|
del view.settings()["codemp_buffer"]
|
||||||
def run(self, edit, start, end, content, change_id):
|
view.erase_status("z_codemp_buffer")
|
||||||
# we modify the region to account for any change that happened in the mean time
|
view.erase_regions("codemp_cursors")
|
||||||
region = self.view.transform_region_from(sublime.Region(start, end), change_id)
|
|
||||||
self.view.replace(edit, region, content)
|
|
||||||
|
|
||||||
async def join_buffer_command(view, remote_name):
|
|
||||||
global _client
|
|
||||||
global _buffers
|
|
||||||
|
|
||||||
try:
|
def tag(view):
|
||||||
buffer = CodempSublimeBuffer(view, remote_name)
|
view.set_status("z_codemp_buffer", "[Codemp]")
|
||||||
await buffer.attach(_client)
|
view.settings()["codemp_buffer"] = True
|
||||||
|
|
||||||
## we should receive all contents from the server upon joining.
|
# The main workflow:
|
||||||
except Exception as e:
|
# Plugin loads and initialises an empty handle to the client
|
||||||
sublime.error_message("Could not join buffer: {}".format(e))
|
# The plugin calls connect and populates the handle with a client instance
|
||||||
return
|
# 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
|
# Listeners
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
|
|
||||||
class CodempClientViewEventListener(sublime_plugin.ViewEventListener):
|
class CodempClientViewEventListener(sublime_plugin.ViewEventListener):
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_applicable(cls, settings):
|
def is_applicable(cls, settings):
|
||||||
return settings.get("codemp_buffer", False)
|
return settings.get("codemp_buffer", False)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def applies_to_primary_view_only(cls):
|
def applies_to_primary_view_only(cls):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def on_selection_modified_async(self):
|
def on_selection_modified_async(self):
|
||||||
send_cursor(self.view)
|
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
|
# 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.
|
# Each time we focus a view to write on it, we first attach the listener to that buffer.
|
||||||
# When we defocus, we detach it.
|
# When we defocus, we detach it.
|
||||||
def on_activated(self):
|
def on_activated(self):
|
||||||
global _txt_change_listener
|
global _txt_change_listener
|
||||||
print("view {} activated".format(self.view.id()))
|
print("view {} activated".format(self.view.id()))
|
||||||
_txt_change_listener.attach(self.view.buffer())
|
_txt_change_listener.attach(self.view.buffer())
|
||||||
|
|
||||||
def on_deactivated(self):
|
def on_deactivated(self):
|
||||||
global _txt_change_listener
|
global _txt_change_listener
|
||||||
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_pre_close(self):
|
def on_text_command(self, command_name, args):
|
||||||
global _client
|
print(self.view.id(), command_name, args)
|
||||||
buffer = get_buffer_from_buffer_id(self.view.buffer_id())
|
if command_name == "codemp_replace_text":
|
||||||
# have to run the detach logic in sync, to keep a valid reference to the view.
|
print("dry_run: detach text listener")
|
||||||
sublime_asyncio.sync(buffer.detach(_client))
|
|
||||||
|
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):
|
class CodempClientTextChangeListener(sublime_plugin.TextChangeListener):
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_applicable(cls, buffer):
|
def is_applicable(cls, buffer):
|
||||||
# don't attach this event listener automatically
|
# don't attach this event listener automatically
|
||||||
# we'll do it by hand with .attach(buffer).
|
# we'll do it by hand with .attach(buffer).
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# lets make this blocking :D
|
# lets make this blocking :D
|
||||||
# def on_text_changed_async(self, changes):
|
# def on_text_changed_async(self, changes):
|
||||||
def on_text_changed(self, changes):
|
def on_text_changed(self, changes):
|
||||||
subl_buffer = get_buffer_from_buffer_id(self.buffer.id())
|
global _client
|
||||||
subl_buffer.send_buffer_change(changes)
|
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:
|
# Commands:
|
||||||
# codemp_connect: connect to a server.
|
# codemp_connect: connect to a server.
|
||||||
# codemp_join: join a workspace with a given name within the 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_share: shares a buffer with a given name in the workspace.
|
||||||
#
|
#
|
||||||
# Internal commands:
|
# 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
|
# Connect Command
|
||||||
#############################################################################
|
#############################################################################
|
||||||
class CodempConnectCommand(sublime_plugin.WindowCommand):
|
class CodempConnectCommand(sublime_plugin.WindowCommand):
|
||||||
def run(self, server_host, session):
|
def run(self, server_host):
|
||||||
sublime_asyncio.dispatch(connect_command(server_host, session))
|
global _client
|
||||||
|
sublime_asyncio.dispatch(_client.connect(server_host))
|
||||||
|
|
||||||
def input(self, args):
|
def input(self, args):
|
||||||
if 'server_host' not in args:
|
if "server_host" not in args:
|
||||||
return ServerHostInputHandler()
|
return ServerHostInputHandler()
|
||||||
|
|
||||||
|
def input_description(self):
|
||||||
|
return "Server host:"
|
||||||
|
|
||||||
def input_description(self):
|
|
||||||
return 'Server host:'
|
|
||||||
|
|
||||||
class ServerHostInputHandler(sublime_plugin.TextInputHandler):
|
class ServerHostInputHandler(sublime_plugin.TextInputHandler):
|
||||||
def initial_text(self):
|
def initial_text(self):
|
||||||
return "http://127.0.0.1:50051"
|
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"
|
|
||||||
|
|
||||||
|
|
||||||
|
# Join Workspace Command
|
||||||
# Join Command
|
|
||||||
#############################################################################
|
#############################################################################
|
||||||
class CodempJoinCommand(sublime_plugin.WindowCommand):
|
class CodempJoinCommand(sublime_plugin.WindowCommand):
|
||||||
def run(self, server_buffer):
|
def run(self, workspace_id):
|
||||||
view = self.window.new_file(flags=sublime.NewFileFlags.TRANSIENT)
|
global _client
|
||||||
sublime_asyncio.dispatch(join_buffer_command(view, server_buffer))
|
sublime_asyncio.dispatch(_client.join_workspace(workspace_id))
|
||||||
|
|
||||||
def input_description(self):
|
|
||||||
return 'Join Buffer:'
|
|
||||||
|
|
||||||
def input(self, args):
|
def input_description(self):
|
||||||
if 'server_buffer' not in args:
|
return "Join Workspace:"
|
||||||
return ServerBufferInputHandler()
|
|
||||||
|
|
||||||
class ServerBufferInputHandler(sublime_plugin.TextInputHandler):
|
def input(self, args):
|
||||||
def initial_text(self):
|
if "workspace_id" not in args:
|
||||||
return "What buffer should I join?"
|
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
|
# Share Command
|
||||||
#############################################################################
|
# #############################################################################
|
||||||
class CodempShareCommand(sublime_plugin.WindowCommand):
|
# class CodempShareCommand(sublime_plugin.WindowCommand):
|
||||||
def run(self, sublime_buffer_path, server_id):
|
# def run(self, sublime_buffer_path, server_id):
|
||||||
sublime_asyncio.dispatch(share_buffer_command(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()
|
|
||||||
|
|
||||||
def input_description(self):
|
# def input(self, args):
|
||||||
return 'Share Buffer:'
|
# if "sublime_buffer" not in args:
|
||||||
|
# return SublimeBufferPathInputHandler()
|
||||||
|
|
||||||
class SublimeBufferPathInputHandler(sublime_plugin.ListInputHandler):
|
# def input_description(self):
|
||||||
def list_items(self):
|
# return "Share Buffer:"
|
||||||
ret_list = []
|
|
||||||
|
|
||||||
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):
|
# for window in sublime.windows():
|
||||||
if 'server_id' not in args:
|
# for view in window.views():
|
||||||
return ServerIdInputHandler()
|
# if view.file_name():
|
||||||
|
# ret_list.append(view.file_name())
|
||||||
|
|
||||||
class ServerIdInputHandler(sublime_plugin.TextInputHandler):
|
# return ret_list
|
||||||
def initial_text(self):
|
|
||||||
return "Buffer name on server"
|
|
||||||
|
|
||||||
# Disconnect Buffer Command
|
# def next_input(self, args):
|
||||||
#############################################################################
|
# if "server_id" not in args:
|
||||||
class CodempDisconnectBufferCommand(sublime_plugin.WindowCommand):
|
# return ServerIdInputHandler()
|
||||||
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 input_description(self):
|
|
||||||
return 'Disconnect Buffer:'
|
|
||||||
|
|
||||||
class RemoteNameInputHandler(sublime_plugin.ListInputHandler):
|
# class ServerIdInputHandler(sublime_plugin.TextInputHandler):
|
||||||
def list_items(self):
|
# def initial_text(self):
|
||||||
global _buffers
|
# return "Buffer name on server"
|
||||||
ret_list = []
|
|
||||||
|
|
||||||
for buff in _buffers:
|
|
||||||
ret_list.append(buff.remote_name)
|
|
||||||
|
|
||||||
return ret_list
|
|
||||||
|
|
||||||
# Disconnect Command
|
# Disconnect Command
|
||||||
#############################################################################
|
#############################################################################
|
||||||
class CodempDisconnectCommand(sublime_plugin.WindowCommand):
|
class CodempDisconnectCommand(sublime_plugin.WindowCommand):
|
||||||
def run(self):
|
def run(self):
|
||||||
sublime_asyncio.sync(disconnect_client())
|
sublime_asyncio.sync(disconnect_client())
|
||||||
|
|
||||||
|
|
||||||
# Proxy Commands ( NOT USED )
|
# Proxy Commands ( NOT USED )
|
||||||
#############################################################################
|
#############################################################################
|
||||||
# class ProxyCodempShareCommand(sublime_plugin.WindowCommand):
|
# class ProxyCodempShareCommand(sublime_plugin.WindowCommand):
|
||||||
# # 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
|
||||||
# def run(self, **kwargs):
|
# def run(self, **kwargs):
|
||||||
# self.window.run_command("codemp_share", kwargs)
|
# self.window.run_command("codemp_share", kwargs)
|
||||||
#
|
#
|
||||||
# def input(self, args):
|
# def input(self, args):
|
||||||
# if 'sublime_buffer' not in args:
|
# if 'sublime_buffer' not in args:
|
||||||
# return SublimeBufferPathInputHandler()
|
# return SublimeBufferPathInputHandler()
|
||||||
#
|
#
|
||||||
# def input_description(self):
|
# def input_description(self):
|
||||||
# return 'Share Buffer:'
|
# return 'Share Buffer:'
|
||||||
#
|
#
|
||||||
# class ProxyCodempJoinCommand(sublime_plugin.WindowCommand):
|
# class ProxyCodempJoinCommand(sublime_plugin.WindowCommand):
|
||||||
# def run(self, **kwargs):
|
# def run(self, **kwargs):
|
||||||
# self.window.run_command("codemp_join", kwargs)
|
# self.window.run_command("codemp_join", kwargs)
|
||||||
#
|
#
|
||||||
# def input(self, args):
|
# def input(self, args):
|
||||||
# if 'server_buffer' not in args:
|
# if 'server_buffer' not in args:
|
||||||
# return ServerBufferInputHandler()
|
# return ServerBufferInputHandler()
|
||||||
#
|
#
|
||||||
# def input_description(self):
|
# def input_description(self):
|
||||||
# return 'Join Buffer:'
|
# return 'Join Buffer:'
|
||||||
#
|
#
|
||||||
# class ProxyCodempConnectCommand(sublime_plugin.WindowCommand):
|
# class ProxyCodempConnectCommand(sublime_plugin.WindowCommand):
|
||||||
# # 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
|
||||||
# def run(self, **kwargs):
|
# def run(self, **kwargs):
|
||||||
# self.window.run_command("codemp_connect", kwargs)
|
# self.window.run_command("codemp_connect", kwargs)
|
||||||
#
|
#
|
||||||
# def input(self, args):
|
# def input(self, args):
|
||||||
# if 'server_host' not in args:
|
# if 'server_host' not in args:
|
||||||
# return ServerHostInputHandler()
|
# return ServerHostInputHandler()
|
||||||
#
|
#
|
||||||
# def input_description(self):
|
# def input_description(self):
|
||||||
# return 'Server host:'
|
# return 'Server host:'
|
||||||
|
|
||||||
|
|
||||||
## NOT NEEDED ANYMORE
|
# NOT NEEDED ANYMORE
|
||||||
# def compress_change_region(changes):
|
# def compress_change_region(changes):
|
||||||
# # the bounding region of all text changes.
|
# # the bounding region of all text changes.
|
||||||
# txt_a = float("inf")
|
# txt_a = float("inf")
|
||||||
# txt_b = 0
|
# txt_b = 0
|
||||||
|
|
||||||
# # the region in the original buffer subjected to the change.
|
# # the region in the original buffer subjected to the change.
|
||||||
# reg_a = float("inf")
|
# reg_a = float("inf")
|
||||||
# reg_b = 0
|
# reg_b = 0
|
||||||
|
|
||||||
# # we keep track of how much the changes move the indexing of the buffer
|
# # we keep track of how much the changes move the indexing of the buffer
|
||||||
# buffer_shift = 0 # left - + right
|
# buffer_shift = 0 # left - + right
|
||||||
|
|
||||||
# for change in changes:
|
# for change in changes:
|
||||||
# # the change in characters that the change would bring
|
# # the change in characters that the change would bring
|
||||||
# # len(str) and .len_utf8 are mutually exclusive
|
# # len(str) and .len_utf8 are mutually exclusive
|
||||||
# # len(str) is when we insert new text at a position
|
# # len(str) is when we insert new text at a position
|
||||||
# # .len_utf8 is the length of the deleted/canceled string in the buffer
|
# # .len_utf8 is the length of the deleted/canceled string in the buffer
|
||||||
# change_delta = len(change.str) - change.len_utf8
|
# change_delta = len(change.str) - change.len_utf8
|
||||||
|
|
||||||
# # the text region is enlarged to the left
|
# # the text region is enlarged to the left
|
||||||
# txt_a = min(txt_a, change.a.pt)
|
# txt_a = min(txt_a, change.a.pt)
|
||||||
|
|
||||||
# # On insertion, change.b.pt == change.a.pt
|
# # On insertion, change.b.pt == change.a.pt
|
||||||
# # If we meet a new insertion further than the current window
|
# # If we meet a new insertion further than the current window
|
||||||
# # we expand to the right by that change.
|
# # we expand to the right by that change.
|
||||||
# # On deletion, change.a.pt == change.b.pt - change.len_utf8
|
# # On deletion, change.a.pt == change.b.pt - change.len_utf8
|
||||||
# # when we delete a selection and it is further than the current window
|
# # 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.
|
# # we enlarge to the right up until the begin of the deleted region.
|
||||||
# if change.b.pt > txt_b:
|
# if change.b.pt > txt_b:
|
||||||
# txt_b = change.b.pt + change_delta
|
# txt_b = change.b.pt + change_delta
|
||||||
# else:
|
# else:
|
||||||
# # otherwise we just shift the window according to the change
|
# # otherwise we just shift the window according to the change
|
||||||
# txt_b += change_delta
|
# txt_b += change_delta
|
||||||
|
|
||||||
# # the bounding region enlarged to the left
|
# # the bounding region enlarged to the left
|
||||||
# reg_a = min(reg_a, change.a.pt)
|
# reg_a = min(reg_a, change.a.pt)
|
||||||
|
|
||||||
# # In this bit, we want to look at the buffer BEFORE the modifications
|
# # 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
|
# # 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
|
# # 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.
|
# # to map back to the correct index for each change in the unmodified buffer.
|
||||||
# if change.b.pt + buffer_shift > reg_b:
|
# if change.b.pt + buffer_shift > reg_b:
|
||||||
# # we only enlarge if we have changes that exceede on the right the current window
|
# # we only enlarge if we have changes that exceede on the right the current window
|
||||||
# reg_b = change.b.pt + buffer_shift
|
# reg_b = change.b.pt + buffer_shift
|
||||||
|
|
||||||
# # after using the change delta, we archive it for the next iterations
|
# # 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 +.
|
# # 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
|
# # 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.
|
# # old position to be positive, and viceversa for text insertion.
|
||||||
# buffer_shift -= change_delta
|
# 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 txt]", "[", txt_a, txt_b, "]", txt)
|
||||||
# # print("[walking reg]", "[", reg_a, reg_b, "]")
|
# # print("[walking reg]", "[", reg_a, reg_b, "]")
|
||||||
# return reg_a, reg_b
|
# return reg_a, reg_b
|
||||||
|
|
|
@ -1,77 +1,508 @@
|
||||||
import asyncio
|
from __future__ import annotations
|
||||||
import Codemp.bindings.codemp_client as libcodemp
|
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):
|
import asyncio # noqa: F401
|
||||||
self.handle = libcodemp.codemp_init()
|
import typing # noqa: F401
|
||||||
## Bindings
|
import tempfile
|
||||||
async def connect(self, server_host): # -> None
|
import os
|
||||||
await self.handle.connect(server_host)
|
from Codemp.bindings.codemp_client import codemp_init, PyCursorEvent, PyTextChange, PyId
|
||||||
|
|
||||||
async def join(self, session): # -> CursorController
|
|
||||||
return CursorController(await self.handle.join(session))
|
|
||||||
|
|
||||||
async def create(self, path, content=None): # -> None
|
# Some utility functions
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
|
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="***REMOVED***"
|
||||||
|
):
|
||||||
|
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()
|
||||||
|
|
Loading…
Reference in a new issue