2024-02-21 23:59:49 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional, Callable
|
2023-08-17 18:39:47 +02:00
|
|
|
|
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
import sublime
|
2024-02-21 23:59:49 +01:00
|
|
|
import asyncio # noqa: F401
|
|
|
|
import typing # noqa: F401
|
|
|
|
import tempfile
|
|
|
|
import os
|
2024-02-23 17:49:26 +01:00
|
|
|
import shutil
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2023-08-17 18:39:47 +02:00
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
import Codemp.src.globals as g
|
|
|
|
from Codemp.src.wrappers import BufferController, Workspace, Client
|
|
|
|
from Codemp.src.utils import status_log, is_active, rowcol_to_region
|
|
|
|
from Codemp.src.TaskManager import TaskManager
|
2023-11-24 10:36:06 +01:00
|
|
|
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
# 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,
|
|
|
|
workspace: VirtualWorkspace,
|
2024-02-23 13:25:01 +01:00
|
|
|
remote_id: str,
|
|
|
|
view: sublime.View,
|
2024-02-21 23:59:49 +01:00
|
|
|
buffctl: BufferController,
|
|
|
|
):
|
|
|
|
self.view = view
|
|
|
|
self.codemp_id = remote_id
|
|
|
|
self.sublime_id = view.buffer_id()
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
self.workspace = workspace
|
|
|
|
self.buffctl = buffctl
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
self.tmpfile = os.path.join(workspace.rootdir, self.codemp_id)
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
self.view.set_name(self.codemp_id)
|
|
|
|
open(self.tmpfile, "a").close()
|
|
|
|
self.view.retarget(self.tmpfile)
|
|
|
|
self.view.set_scratch(True)
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
# mark the view as a codemp view
|
2024-02-23 17:49:26 +01:00
|
|
|
s = self.view.settings()
|
2024-02-23 13:25:01 +01:00
|
|
|
self.view.set_status(g.SUBLIME_STATUS_ID, "[Codemp]")
|
2024-02-23 17:49:26 +01:00
|
|
|
s[g.CODEMP_BUFFER_TAG] = True
|
|
|
|
s[g.CODEMP_REMOTE_ID] = self.codemp_id
|
|
|
|
s[g.CODEMP_WORKSPACE_ID] = self.workspace.id
|
2023-08-17 18:39:47 +02:00
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
def cleanup(self):
|
|
|
|
os.remove(self.tmpfile)
|
|
|
|
# cleanup views
|
2024-02-23 17:49:26 +01:00
|
|
|
s = self.view.settings()
|
|
|
|
del s[g.CODEMP_BUFFER_TAG]
|
|
|
|
del s[g.CODEMP_REMOTE_ID]
|
|
|
|
del s[g.CODEMP_WORKSPACE_ID]
|
2024-02-23 13:25:01 +01:00
|
|
|
self.view.erase_status(g.SUBLIME_STATUS_ID)
|
|
|
|
# this does nothing for now. figure out a way later
|
|
|
|
# self.view.erase_regions(g.SUBLIME_REGIONS_PREFIX)
|
|
|
|
status_log(f"cleaning up virtual buffer '{self.codemp_id}'")
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
2024-02-23 17:49:26 +01:00
|
|
|
# mapping remote ids -> local ids
|
2024-02-23 13:25:01 +01:00
|
|
|
self.id_map: dict[str, str] = {}
|
|
|
|
self.active_buffers: dict[str, VirtualBuffer] = {}
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
# 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": []}
|
2024-02-23 13:25:01 +01:00
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
proj_data["folders"].append(
|
2024-02-23 13:25:01 +01:00
|
|
|
{"name": f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}", "path": self.rootdir}
|
2024-02-21 23:59:49 +01:00
|
|
|
)
|
|
|
|
self.sublime_window.set_project_data(proj_data)
|
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
def add_buffer(self, remote_id: str, vbuff: VirtualBuffer):
|
2024-02-23 17:49:26 +01:00
|
|
|
self.id_map[remote_id] = vbuff.view.buffer_id()
|
|
|
|
self.active_buffers[vbuff.view.buffer_id()] = vbuff
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
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.
|
2024-02-23 13:25:01 +01:00
|
|
|
for vbuff in self.active_buffers.values():
|
2024-02-21 23:59:49 +01:00
|
|
|
vbuff.view.close()
|
|
|
|
|
|
|
|
d = self.sublime_window.project_data()
|
2024-02-23 13:25:01 +01:00
|
|
|
newf = list(
|
|
|
|
filter(
|
|
|
|
lambda F: F["name"] != f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}",
|
|
|
|
d["folders"],
|
|
|
|
)
|
|
|
|
)
|
2024-02-21 23:59:49 +01:00
|
|
|
d["folders"] = newf
|
|
|
|
self.sublime_window.set_project_data(d)
|
2024-02-23 13:25:01 +01:00
|
|
|
status_log(f"cleaning up virtual workspace '{self.id}'")
|
2024-02-23 17:49:26 +01:00
|
|
|
shutil.rmtree(self.rootdir, ignore_errors=True)
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
def get_by_local(self, local_id: str) -> Optional[VirtualBuffer]:
|
2024-02-23 17:49:26 +01:00
|
|
|
return self.active_buffers.get(local_id)
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
def get_by_remote(self, remote_id: str) -> Optional[VirtualBuffer]:
|
2024-02-23 17:49:26 +01:00
|
|
|
return self.active_buffers.get(self.id_map.get(remote_id))
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
view = self.sublime_window.new_file()
|
2024-02-23 13:25:01 +01:00
|
|
|
vbuff = VirtualBuffer(self, id, view, buff_ctl)
|
|
|
|
self.add_buffer(id, vbuff)
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
self.client.spawn_buffer_manager(vbuff)
|
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
# TODO! if the view is already active calling focus_view() will not trigger the on_activate
|
2024-02-21 23:59:49 +01:00
|
|
|
self.sublime_window.focus_view(view)
|
|
|
|
|
|
|
|
|
|
|
|
class VirtualClient:
|
|
|
|
def __init__(self, on_exit: Callable = None):
|
|
|
|
self.handle: Client = Client()
|
2024-02-23 13:25:01 +01:00
|
|
|
self.workspaces: dict[str, VirtualWorkspace] = {}
|
2024-02-21 23:59:49 +01:00
|
|
|
self.active_workspace: VirtualWorkspace = None
|
|
|
|
self.tm = TaskManager(on_exit)
|
|
|
|
|
2024-02-23 17:49:26 +01:00
|
|
|
def __getitem__(self, key: str):
|
|
|
|
return self.workspaces.get(key)
|
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
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:
|
2024-02-23 13:25:01 +01:00
|
|
|
self.tm.stop_and_pop(f"{g.CURCTL_TASK_PREFIX}-{self.active_workspace.id}")
|
2024-02-21 23:59:49 +01:00
|
|
|
self.active_workspace = ws
|
|
|
|
self.spawn_cursor_manager(ws)
|
|
|
|
|
2024-02-23 17:49:26 +01:00
|
|
|
def get_by_local(self, id):
|
|
|
|
for vws in self.workspaces.values():
|
|
|
|
vbuff = vws.get_by_local(id)
|
|
|
|
if vbuff is not None:
|
|
|
|
return
|
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
vws = VirtualWorkspace(self, workspace_id, workspace_handle)
|
|
|
|
self.make_active(vws)
|
2024-02-23 13:25:01 +01:00
|
|
|
|
|
|
|
self.workspaces[workspace_id] = vws
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
def spawn_cursor_manager(self, virtual_workspace: VirtualWorkspace):
|
|
|
|
async def move_cursor_task(vws):
|
|
|
|
status_log(f"spinning up cursor worker for workspace '{vws.id}'...")
|
|
|
|
try:
|
|
|
|
while cursor_event := await vws.curctl.recv():
|
2024-02-23 13:25:01 +01:00
|
|
|
vbuff = vws.get_by_remote(cursor_event.buffer)
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
if vbuff is None:
|
|
|
|
status_log(
|
2024-02-23 13:25:01 +01:00
|
|
|
f"Received a cursor event for an unknown \
|
|
|
|
or inactive buffer: {cursor_event.buffer}"
|
2024-02-21 23:59:49 +01:00
|
|
|
)
|
|
|
|
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(
|
2024-02-23 13:25:01 +01:00
|
|
|
f"{g.SUBLIME_REGIONS_PREFIX}-{user_hash}",
|
2024-02-21 23:59:49 +01:00
|
|
|
[reg],
|
|
|
|
flags=reg_flags,
|
2024-02-23 13:25:01 +01:00
|
|
|
scope=g.REGIONS_COLORS[user_hash % len(g.REGIONS_COLORS)],
|
2024-02-21 23:59:49 +01:00
|
|
|
annotations=[cursor_event.user],
|
2024-02-23 13:25:01 +01:00
|
|
|
annotation_color=g.PALETTE[user_hash % len(g.PALETTE)],
|
2024-02-21 23:59:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
status_log(f"cursor worker for '{vws.id}' stopped...")
|
|
|
|
return
|
|
|
|
|
|
|
|
self.tm.dispatch(
|
2024-02-23 13:25:01 +01:00
|
|
|
move_cursor_task(virtual_workspace),
|
|
|
|
f"{g.CURCTL_TASK_PREFIX}-{virtual_workspace.id}",
|
2024-02-21 23:59:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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):
|
|
|
|
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():
|
|
|
|
if text_change.is_empty():
|
|
|
|
status_log("change is empty. skipping.")
|
|
|
|
continue
|
2024-02-23 13:25:01 +01:00
|
|
|
# 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.
|
2024-02-23 17:49:26 +01:00
|
|
|
if vb.view.id() == g.ACTIVE_CODEMP_VIEW:
|
|
|
|
status_log(
|
|
|
|
"received a text change with view active, stopping the echo."
|
|
|
|
)
|
2024-02-23 13:25:01 +01:00
|
|
|
vb.view.settings()[g.CODEMP_IGNORE_NEXT_TEXT_CHANGE] = True
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
# 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(
|
2024-02-23 13:25:01 +01:00
|
|
|
apply_buffer_change_task(vbuff),
|
|
|
|
f"{g.BUFFCTL_TASK_PREFIX}-{vbuff.codemp_id}",
|
2024-02-21 23:59:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|