2024-02-21 23:59:49 +01:00
|
|
|
from __future__ import annotations
|
2024-08-04 19:57:59 +02:00
|
|
|
from typing import Optional
|
2023-08-17 18:39:47 +02:00
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
import sublime
|
2024-02-24 16:56:22 +01:00
|
|
|
import asyncio
|
2024-02-21 23:59:49 +01:00
|
|
|
import tempfile
|
|
|
|
import os
|
2024-02-23 17:49:26 +01:00
|
|
|
import shutil
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-08-07 00:17:31 +02:00
|
|
|
from codemp import (
|
2024-08-09 09:17:38 +02:00
|
|
|
BufferController,
|
|
|
|
Workspace,
|
2024-08-07 00:17:31 +02:00
|
|
|
Client,
|
2024-08-09 09:17:38 +02:00
|
|
|
PyLogger,
|
2024-08-07 00:17:31 +02:00
|
|
|
)
|
2024-08-09 09:17:38 +02:00
|
|
|
from sublime_plugin import attach_buffer
|
2024-08-04 19:57:59 +02:00
|
|
|
from ..src import globals as g
|
|
|
|
from ..src.TaskManager import tm
|
|
|
|
from ..src.utils import status_log, rowcol_to_region
|
2023-11-24 10:36:06 +01:00
|
|
|
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-03-02 15:28:39 +01:00
|
|
|
class CodempLogger:
|
2024-08-07 00:17:31 +02:00
|
|
|
def __init__(self, debug: bool = False):
|
2024-08-09 14:22:12 +02:00
|
|
|
self.handle = None
|
|
|
|
self.started = False
|
2024-08-09 09:17:38 +02:00
|
|
|
try:
|
|
|
|
self.handle = PyLogger(debug)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2024-03-02 15:28:39 +01:00
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
async def log(self):
|
2024-08-09 14:22:12 +02:00
|
|
|
if self.started:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.started = True
|
2024-03-02 15:28:39 +01:00
|
|
|
status_log("spinning up the logger...")
|
|
|
|
try:
|
2024-08-09 09:17:38 +02:00
|
|
|
while msg := await self.handle.listen():
|
2024-03-02 15:28:39 +01:00
|
|
|
print(msg)
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
status_log("stopping logger")
|
2024-08-09 14:22:12 +02:00
|
|
|
self.started = False
|
2024-03-02 15:28:39 +01:00
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
status_log(f"logger crashed unexpectedly:\n{e}")
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
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,
|
2024-08-09 09:17:38 +02:00
|
|
|
buffctl: BufferController,
|
2024-02-21 23:59:49 +01:00
|
|
|
):
|
2024-08-04 19:57:59 +02:00
|
|
|
self.view = sublime.active_window().new_file()
|
2024-02-21 23:59:49 +01:00
|
|
|
self.codemp_id = remote_id
|
2024-08-04 19:57:59 +02:00
|
|
|
self.sublime_id = self.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-08-04 19:57:59 +02:00
|
|
|
tm.dispatch(
|
|
|
|
self.apply_bufferchange_task(),
|
|
|
|
f"{g.BUFFCTL_TASK_PREFIX}-{self.codemp_id}",
|
|
|
|
)
|
|
|
|
|
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)
|
2024-08-04 19:57:59 +02:00
|
|
|
|
|
|
|
tm.stop(f"{g.BUFFCTL_TASK_PREFIX}-{self.codemp_id}")
|
2024-02-23 13:25:01 +01:00
|
|
|
status_log(f"cleaning up virtual buffer '{self.codemp_id}'")
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
async def apply_bufferchange_task(self):
|
|
|
|
status_log(f"spinning up '{self.codemp_id}' buffer worker...")
|
|
|
|
try:
|
|
|
|
while text_change := await self.buffctl.recv():
|
2024-08-09 09:17:38 +02:00
|
|
|
change_id = self.view.change_id()
|
2024-08-04 19:57:59 +02:00
|
|
|
if text_change.is_empty():
|
|
|
|
status_log("change is empty. skipping.")
|
|
|
|
continue
|
|
|
|
# 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 self.view.id() == g.ACTIVE_CODEMP_VIEW:
|
|
|
|
self.view.settings()[g.CODEMP_IGNORE_NEXT_TEXT_CHANGE] = 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.
|
|
|
|
self.view.run_command(
|
|
|
|
"codemp_replace_text",
|
|
|
|
{
|
2024-08-09 09:17:38 +02:00
|
|
|
"start": text_change.start,
|
|
|
|
"end": text_change.end,
|
2024-08-04 19:57:59 +02:00
|
|
|
"content": text_change.content,
|
2024-08-09 09:17:38 +02:00
|
|
|
"change_id": change_id,
|
2024-08-07 00:17:31 +02:00
|
|
|
}, # pyright: ignore
|
2024-08-04 19:57:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
status_log(f"'{self.codemp_id}' buffer worker stopped...")
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
status_log(f"buffer worker '{self.codemp_id}' crashed:\n{e}")
|
|
|
|
raise
|
|
|
|
|
|
|
|
def send_buffer_change(self, changes):
|
|
|
|
# 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
|
|
|
|
)
|
|
|
|
)
|
2024-08-09 09:17:38 +02:00
|
|
|
self.buffctl.send(region.begin(), region.end(), change.str)
|
2024-08-04 19:57:59 +02:00
|
|
|
|
|
|
|
def send_cursor(self, vws: VirtualWorkspace):
|
|
|
|
# TODO: only the last placed cursor/selection.
|
|
|
|
# status_log(f"sending cursor position in workspace: {vbuff.workspace.id}")
|
|
|
|
region = self.view.sel()[0]
|
|
|
|
start = self.view.rowcol(region.begin()) # only counts UTF8 chars
|
|
|
|
end = self.view.rowcol(region.end())
|
|
|
|
|
|
|
|
vws.curctl.send(self.codemp_id, start, end)
|
|
|
|
|
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:
|
2024-08-09 09:17:38 +02:00
|
|
|
def __init__(self, handle: Workspace):
|
2024-02-21 23:59:49 +01:00
|
|
|
self.handle = handle
|
2024-08-09 09:17:38 +02:00
|
|
|
self.id = self.handle.id()
|
|
|
|
self.sublime_window = sublime.active_window()
|
2024-02-21 23:59:49 +01:00
|
|
|
self.curctl = handle.cursor()
|
2024-08-09 14:23:29 +02:00
|
|
|
self.materialized = False
|
2024-08-04 19:57:59 +02:00
|
|
|
self.isactive = False
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-02-23 17:49:26 +01:00
|
|
|
# mapping remote ids -> local ids
|
2024-08-07 00:17:31 +02:00
|
|
|
self.id_map: dict[str, int] = {}
|
|
|
|
self.active_buffers: dict[int, VirtualBuffer] = {} # local_id -> VBuff
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
def cleanup(self):
|
2024-08-04 19:57:59 +02:00
|
|
|
self.deactivate()
|
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
# 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()
|
|
|
|
|
2024-02-27 00:06:58 +01:00
|
|
|
self.active_buffers = {} # drop all buffers, let them be garbace collected (hopefully)
|
|
|
|
|
2024-08-09 14:23:29 +02:00
|
|
|
if not self.materialized:
|
|
|
|
return # nothing to delete
|
|
|
|
|
2024-08-07 00:17:31 +02:00
|
|
|
d: dict = self.sublime_window.project_data() # pyright: ignore
|
2024-02-23 13:25:01 +01:00
|
|
|
newf = list(
|
|
|
|
filter(
|
2024-08-04 19:57:59 +02:00
|
|
|
lambda f: f.get("name", "") != f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}",
|
2024-02-23 13:25:01 +01:00
|
|
|
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-08-09 09:17:38 +02:00
|
|
|
self.curctl.stop()
|
|
|
|
|
2024-02-27 00:06:58 +01:00
|
|
|
s = self.sublime_window.settings()
|
|
|
|
del s[g.CODEMP_WINDOW_TAG]
|
|
|
|
del s[g.CODEMP_WINDOW_WORKSPACES]
|
|
|
|
|
2024-08-09 14:23:29 +02:00
|
|
|
self.materialized = False
|
|
|
|
|
|
|
|
def materialize(self):
|
|
|
|
# attach the workspace to the editor, tagging windows and populating
|
|
|
|
# virtual file systems
|
|
|
|
if self.materialized:
|
|
|
|
return # no op, we already are in the editor
|
|
|
|
|
|
|
|
# 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: dict = self.sublime_window.project_data() # pyright: ignore
|
|
|
|
if proj_data is None:
|
|
|
|
proj_data = {"folders": []}
|
|
|
|
|
|
|
|
proj_data["folders"].append(
|
|
|
|
{"name": f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}", "path": self.rootdir}
|
|
|
|
)
|
|
|
|
self.sublime_window.set_project_data(proj_data)
|
|
|
|
|
|
|
|
s: dict = self.sublime_window.settings() # pyright: ignore
|
|
|
|
if s.get(g.CODEMP_WINDOW_TAG, False):
|
|
|
|
s[g.CODEMP_WINDOW_WORKSPACES].append(self.id)
|
|
|
|
else:
|
|
|
|
s[g.CODEMP_WINDOW_TAG] = True
|
|
|
|
s[g.CODEMP_WINDOW_WORKSPACES] = [self.id]
|
|
|
|
|
|
|
|
self.materialized = True
|
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
def activate(self):
|
|
|
|
tm.dispatch(
|
|
|
|
self.move_cursor_task(),
|
|
|
|
f"{g.CURCTL_TASK_PREFIX}-{self.id}",
|
|
|
|
)
|
|
|
|
self.isactive = True
|
|
|
|
|
|
|
|
def deactivate(self):
|
|
|
|
if self.isactive:
|
|
|
|
tm.stop(f"{g.CURCTL_TASK_PREFIX}-{self.id}")
|
2024-08-09 14:23:29 +02:00
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
self.isactive = False
|
|
|
|
|
|
|
|
def add_buffer(self, remote_id: str, vbuff: VirtualBuffer):
|
|
|
|
self.id_map[remote_id] = vbuff.view.buffer_id()
|
|
|
|
self.active_buffers[vbuff.view.buffer_id()] = vbuff
|
|
|
|
|
2024-08-07 00:17:31 +02:00
|
|
|
def get_by_local(self, local_id: int) -> 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-08-04 19:57:59 +02:00
|
|
|
local_id = self.id_map.get(remote_id)
|
|
|
|
if local_id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
vbuff = self.active_buffers.get(local_id)
|
|
|
|
if vbuff is None:
|
|
|
|
status_log(
|
2024-08-09 09:17:38 +02:00
|
|
|
"[WARN] a local-remote buffer id pair was found but \
|
|
|
|
not the matching virtual buffer."
|
2024-08-04 19:57:59 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
return vbuff
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-08-09 09:17:38 +02:00
|
|
|
# A workspace has some buffers inside of it (filetree)
|
|
|
|
# some of those you are already attached to (buffers_by_name)
|
|
|
|
# If already attached to it return the same alredy existing bufferctl
|
|
|
|
# if existing but not attached (attach)
|
|
|
|
# if not existing ask for creation (create + attach)
|
2024-02-21 23:59:49 +01:00
|
|
|
async def attach(self, id: str):
|
|
|
|
if id is None:
|
|
|
|
return
|
|
|
|
|
2024-08-09 09:17:38 +02:00
|
|
|
attached_buffers = self.handle.buffer_by_name(id)
|
|
|
|
if attached_buffers is not None:
|
|
|
|
return self.get_by_remote(id)
|
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
await self.handle.fetch_buffers()
|
|
|
|
existing_buffers = self.handle.filetree()
|
|
|
|
if id not in existing_buffers:
|
2024-08-09 09:17:38 +02:00
|
|
|
create = sublime.ok_cancel_dialog(
|
|
|
|
"There is no buffer named '{id}' in the workspace.\n\
|
|
|
|
Do you want to create it?",
|
|
|
|
ok_title="yes",
|
|
|
|
title="Create Buffer?",
|
|
|
|
)
|
|
|
|
if create:
|
|
|
|
try:
|
|
|
|
await self.handle.create(id)
|
|
|
|
except Exception as e:
|
|
|
|
status_log(f"could not create buffer:\n\n {e}", True)
|
|
|
|
return
|
|
|
|
else:
|
2024-02-21 23:59:49 +01:00
|
|
|
return
|
|
|
|
|
2024-08-09 09:17:38 +02:00
|
|
|
# now either we created it or it exists already
|
2024-02-21 23:59:49 +01:00
|
|
|
try:
|
|
|
|
buff_ctl = await self.handle.attach(id)
|
|
|
|
except Exception as e:
|
2024-08-09 09:17:38 +02:00
|
|
|
status_log(f"error when attaching to buffer '{id}':\n\n {e}", True)
|
2024-02-21 23:59:49 +01:00
|
|
|
return
|
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
vbuff = VirtualBuffer(self, id, buff_ctl)
|
2024-02-23 13:25:01 +01:00
|
|
|
self.add_buffer(id, vbuff)
|
2024-02-21 23:59:49 +01:00
|
|
|
|
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-08-04 19:57:59 +02:00
|
|
|
self.sublime_window.focus_view(vbuff.view)
|
|
|
|
|
2024-08-09 09:17:38 +02:00
|
|
|
def detach(self, id: str):
|
|
|
|
if id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
attached_buffers = self.handle.buffer_by_name(id)
|
|
|
|
if attached_buffers is None:
|
|
|
|
status_log(f"You are not attached to the buffer '{id}'", True)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.handle.detach(id)
|
|
|
|
|
|
|
|
async def delete(self, id: str):
|
|
|
|
if id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# delete a non existent buffer
|
|
|
|
await self.handle.fetch_buffers()
|
|
|
|
existing_buffers = self.handle.filetree()
|
|
|
|
if id not in existing_buffers:
|
|
|
|
status_log(f"The buffer '{id}' does not exists.", True)
|
|
|
|
return
|
|
|
|
# delete a buffer that exists but you are not attached to
|
|
|
|
attached_buffers = self.handle.buffer_by_name(id)
|
|
|
|
if attached_buffers is None:
|
|
|
|
delete = sublime.ok_cancel_dialog(
|
|
|
|
"Confirm you want to delete the buffer '{id}'",
|
|
|
|
ok_title="delete",
|
|
|
|
title="Delete Buffer?",
|
|
|
|
)
|
|
|
|
if delete:
|
|
|
|
try:
|
|
|
|
await self.handle.delete(id)
|
|
|
|
except Exception as e:
|
|
|
|
status_log(f"error when deleting the buffer '{id}':\n\n {e}", True)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
# delete buffer that you are attached to
|
|
|
|
delete = sublime.ok_cancel_dialog(
|
|
|
|
"Confirm you want to delete the buffer '{id}'.\n\
|
|
|
|
You will be disconnected from it.",
|
|
|
|
ok_title="delete",
|
|
|
|
title="Delete Buffer?",
|
|
|
|
)
|
|
|
|
if delete:
|
|
|
|
self.detach(id)
|
|
|
|
try:
|
|
|
|
await self.handle.delete(id)
|
|
|
|
except Exception as e:
|
|
|
|
status_log(f"error when deleting the buffer '{id}':\n\n {e}", True)
|
|
|
|
return
|
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
async def move_cursor_task(self):
|
|
|
|
status_log(f"spinning up cursor worker for workspace '{self.id}'...")
|
|
|
|
try:
|
|
|
|
while cursor_event := await self.curctl.recv():
|
|
|
|
vbuff = self.get_by_remote(cursor_event.buffer)
|
|
|
|
|
|
|
|
if vbuff is None:
|
|
|
|
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"{g.SUBLIME_REGIONS_PREFIX}-{user_hash}",
|
|
|
|
[reg],
|
|
|
|
flags=reg_flags,
|
|
|
|
scope=g.REGIONS_COLORS[user_hash % len(g.REGIONS_COLORS)],
|
|
|
|
annotations=[cursor_event.user],
|
|
|
|
annotation_color=g.PALETTE[user_hash % len(g.PALETTE)],
|
|
|
|
)
|
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
status_log(f"cursor worker for '{self.id}' stopped...")
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
status_log(f"cursor worker '{self.id}' crashed:\n{e}")
|
|
|
|
raise
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
class VirtualClient:
|
2024-08-04 19:57:59 +02:00
|
|
|
def __init__(self):
|
2024-08-09 09:17:38 +02:00
|
|
|
self.handle = None
|
2024-02-23 13:25:01 +01:00
|
|
|
self.workspaces: dict[str, VirtualWorkspace] = {}
|
2024-08-04 19:57:59 +02:00
|
|
|
self.active_workspace: Optional[VirtualWorkspace] = None
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-02-23 17:49:26 +01:00
|
|
|
def __getitem__(self, key: str):
|
|
|
|
return self.workspaces.get(key)
|
|
|
|
|
2024-08-09 09:17:38 +02:00
|
|
|
def connect(self, host: str, user: str, password: str):
|
|
|
|
status_log(f"Connecting to {host} with user {user}")
|
|
|
|
try:
|
|
|
|
self.handle = Client(host, user, password)
|
|
|
|
except Exception as e:
|
|
|
|
sublime.error_message(
|
|
|
|
f"Could not connect:\n Make sure the server is up.\n\
|
|
|
|
or your credentials are correct\n\nerror: {e}"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
id = self.handle.user_id()
|
|
|
|
status_log(f"Connected to '{host}' with user {user} and id: {id}")
|
|
|
|
|
|
|
|
async def join_workspace(
|
|
|
|
self,
|
|
|
|
workspace_id: str,
|
|
|
|
) -> Optional[VirtualWorkspace]:
|
|
|
|
if self.handle is None:
|
|
|
|
status_log("Connect to a server first!", True)
|
|
|
|
return
|
|
|
|
|
|
|
|
status_log(f"Joining workspace: '{workspace_id}'")
|
|
|
|
try:
|
|
|
|
workspace = await self.handle.join_workspace(workspace_id)
|
|
|
|
except Exception as e:
|
|
|
|
status_log(
|
|
|
|
f"Could not join workspace '{workspace_id}'.\n\nerror: {e}", True
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
vws = VirtualWorkspace(workspace)
|
|
|
|
self.workspaces[workspace_id] = vws
|
2024-08-09 14:23:29 +02:00
|
|
|
# self.make_active(vws)
|
2024-08-09 09:17:38 +02:00
|
|
|
|
|
|
|
return vws
|
|
|
|
|
|
|
|
def leave_workspace(self, id: str):
|
|
|
|
if self.handle is None:
|
|
|
|
status_log("Connect to a server first!", True)
|
|
|
|
return False
|
|
|
|
status_log(f"Leaving workspace: '{id}'")
|
2024-08-09 14:23:29 +02:00
|
|
|
if self.handle.leave_workspace(id):
|
|
|
|
self.workspaces[id].cleanup()
|
|
|
|
del self.workspaces[id]
|
2024-08-09 09:17:38 +02:00
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
def get_workspace(self, view):
|
|
|
|
tag_id = view.settings().get(g.CODEMP_WORKSPACE_ID)
|
|
|
|
if tag_id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
ws = self.workspaces.get(tag_id)
|
|
|
|
if ws is None:
|
|
|
|
status_log(
|
|
|
|
"[WARN] a tag on the view was found but not a matching workspace."
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
return ws
|
|
|
|
|
2024-08-09 09:17:38 +02:00
|
|
|
def active_workspaces(self):
|
|
|
|
return self.handle.active_workspaces() if self.handle else []
|
|
|
|
|
|
|
|
def user_id(self):
|
|
|
|
return self.handle.user_id() if self.handle else None
|
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
def get_buffer(self, view):
|
|
|
|
ws = self.get_workspace(view)
|
|
|
|
return None if ws is None else ws.get_by_local(view.buffer_id())
|
|
|
|
|
|
|
|
def make_active(self, ws: VirtualWorkspace | None):
|
|
|
|
if self.active_workspace == ws:
|
|
|
|
return
|
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
if self.active_workspace is not None:
|
2024-08-04 19:57:59 +02:00
|
|
|
self.active_workspace.deactivate()
|
|
|
|
|
|
|
|
if ws is not None:
|
|
|
|
ws.activate()
|
|
|
|
|
2024-02-21 23:59:49 +01:00
|
|
|
self.active_workspace = ws
|
|
|
|
|
|
|
|
|
2024-08-09 14:22:12 +02:00
|
|
|
DEBUG = False
|
|
|
|
logger = CodempLogger(DEBUG)
|
|
|
|
logger.log()
|
2024-08-04 19:57:59 +02:00
|
|
|
client = VirtualClient()
|