mirror of
https://github.com/hexedtech/codemp-sublime.git
synced 2024-11-21 22:34:48 +01:00
wip: moved to the promise model from the glue
Former-commit-id: be08024cd256b631697b6c8fda6d99f8ccb4ece8
This commit is contained in:
parent
62ed439b41
commit
507fca5057
8 changed files with 159 additions and 186 deletions
|
@ -1 +1 @@
|
||||||
def6ad401dc56ab672f6c77a3e0dada48efd8f9e
|
999fd917360a8fa68970c12c70bf5decf30c84a4
|
79
plugin.py
79
plugin.py
|
@ -5,12 +5,12 @@ import sublime_plugin
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from Codemp.src.task_manager import rt
|
# from Codemp.src.task_manager import rt
|
||||||
from Codemp.src.client import client, VirtualClient
|
from Codemp.src.client import client
|
||||||
from Codemp.src.logger import inner_logger
|
|
||||||
from Codemp.src.utils import safe_listener_detach
|
from Codemp.src.utils import safe_listener_detach
|
||||||
from Codemp.src.utils import safe_listener_attach
|
from Codemp.src.utils import safe_listener_attach
|
||||||
from Codemp.src import globals as g
|
from Codemp.src import globals as g
|
||||||
|
from codemp import register_logger
|
||||||
|
|
||||||
LOG_LEVEL = logging.DEBUG
|
LOG_LEVEL = logging.DEBUG
|
||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
|
@ -27,49 +27,37 @@ package_logger.propagate = False
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# returns false if logger already exists
|
||||||
|
register_logger(lambda msg: logger.log(logger.level, msg), False)
|
||||||
|
|
||||||
TEXT_LISTENER = None
|
TEXT_LISTENER = None
|
||||||
# rt.dispatch(inner_logger.listen(), "codemp-logger")
|
|
||||||
|
|
||||||
|
|
||||||
# Initialisation and Deinitialisation
|
# Initialisation and Deinitialisation
|
||||||
##############################################################################
|
##############################################################################
|
||||||
def plugin_loaded():
|
def plugin_loaded():
|
||||||
global TEXT_LISTENER
|
global TEXT_LISTENER
|
||||||
|
|
||||||
# instantiate and start a global asyncio event loop.
|
|
||||||
# pass in the exit_handler coroutine that will be called upon relasing the event loop.
|
|
||||||
# tm.acquire(disconnect_client)
|
|
||||||
|
|
||||||
TEXT_LISTENER = CodempClientTextChangeListener()
|
TEXT_LISTENER = CodempClientTextChangeListener()
|
||||||
|
|
||||||
logger.debug("plugin loaded")
|
logger.debug("plugin loaded")
|
||||||
|
|
||||||
|
|
||||||
def disconnect_client():
|
def plugin_unloaded():
|
||||||
|
logger.debug("unloading")
|
||||||
global TEXT_LISTENER
|
global TEXT_LISTENER
|
||||||
|
|
||||||
if TEXT_LISTENER is not None:
|
if TEXT_LISTENER is not None:
|
||||||
safe_listener_detach(TEXT_LISTENER)
|
safe_listener_detach(TEXT_LISTENER)
|
||||||
|
|
||||||
for vws in client.workspaces.values():
|
|
||||||
vws.cleanup()
|
|
||||||
|
|
||||||
client.handle = None # drop
|
|
||||||
|
|
||||||
|
|
||||||
def plugin_unloaded():
|
|
||||||
# releasing the runtime, runs the disconnect callback defined when acquiring the event loop.
|
|
||||||
logger.debug("unloading")
|
|
||||||
package_logger.removeHandler(handler)
|
package_logger.removeHandler(handler)
|
||||||
disconnect_client()
|
client.disconnect()
|
||||||
rt.stop_loop()
|
# rt.stop_loop()
|
||||||
|
|
||||||
|
|
||||||
# Listeners
|
# Listeners
|
||||||
##############################################################################
|
##############################################################################
|
||||||
class EventListener(sublime_plugin.EventListener):
|
class EventListener(sublime_plugin.EventListener):
|
||||||
def on_exit(self):
|
def on_exit(self):
|
||||||
disconnect_client()
|
client.disconnect()
|
||||||
|
|
||||||
def on_pre_close_window(self, window):
|
def on_pre_close_window(self, window):
|
||||||
if client.active_workspace is None:
|
if client.active_workspace is None:
|
||||||
|
@ -220,32 +208,29 @@ class ConnectUserName(sublime_plugin.TextInputHandler):
|
||||||
|
|
||||||
# Generic Join Command
|
# Generic Join Command
|
||||||
#############################################################################
|
#############################################################################
|
||||||
async def JoinCommand(client: VirtualClient, workspace_id: str, buffer_id: str):
|
|
||||||
if workspace_id == "":
|
|
||||||
return
|
|
||||||
|
|
||||||
vws = client.workspaces.get(workspace_id)
|
|
||||||
if vws is None:
|
|
||||||
try:
|
|
||||||
vws = await client.join_workspace(workspace_id)
|
|
||||||
except Exception as e:
|
|
||||||
raise e
|
|
||||||
|
|
||||||
if vws is None:
|
|
||||||
logger.warning("The client returned a void workspace.")
|
|
||||||
return
|
|
||||||
|
|
||||||
vws.materialize()
|
|
||||||
|
|
||||||
if buffer_id != "":
|
|
||||||
await vws.attach(buffer_id)
|
|
||||||
|
|
||||||
|
|
||||||
class CodempJoinCommand(sublime_plugin.WindowCommand):
|
class CodempJoinCommand(sublime_plugin.WindowCommand):
|
||||||
def run(self, workspace_id, buffer_id):
|
def run(self, workspace_id, buffer_id):
|
||||||
|
if workspace_id == "":
|
||||||
|
return
|
||||||
|
|
||||||
|
vws = client.workspaces.get(workspace_id)
|
||||||
|
if vws is None:
|
||||||
|
try:
|
||||||
|
vws = client.join_workspace(workspace_id)
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
if vws is None:
|
||||||
|
logger.warning("The client returned a void workspace.")
|
||||||
|
return
|
||||||
|
|
||||||
|
vws.materialize()
|
||||||
|
|
||||||
if buffer_id == "* Don't Join Any":
|
if buffer_id == "* Don't Join Any":
|
||||||
buffer_id = ""
|
buffer_id = ""
|
||||||
rt.dispatch(JoinCommand(client, workspace_id, buffer_id))
|
|
||||||
|
if buffer_id != "":
|
||||||
|
vws.attach(buffer_id)
|
||||||
|
|
||||||
def is_enabled(self) -> bool:
|
def is_enabled(self) -> bool:
|
||||||
return client.handle is not None
|
return client.handle is not None
|
||||||
|
@ -287,7 +272,7 @@ class JoinWorkspaceIdList(sublime_plugin.ListInputHandler):
|
||||||
|
|
||||||
wid = args["workspace_id"]
|
wid = args["workspace_id"]
|
||||||
if wid != "":
|
if wid != "":
|
||||||
vws = rt.block_on(client.join_workspace(wid))
|
vws = client.join_workspace(wid)
|
||||||
else:
|
else:
|
||||||
vws = None
|
vws = None
|
||||||
try:
|
try:
|
||||||
|
@ -385,7 +370,7 @@ class CodempDisconnectCommand(sublime_plugin.WindowCommand):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
disconnect_client()
|
client.disconnect()
|
||||||
|
|
||||||
|
|
||||||
# Leave Workspace Command
|
# Leave Workspace Command
|
||||||
|
|
|
@ -58,6 +58,7 @@ class VirtualBuffer:
|
||||||
self.view.erase_status(g.SUBLIME_STATUS_ID)
|
self.view.erase_status(g.SUBLIME_STATUS_ID)
|
||||||
|
|
||||||
rt.stop_task(f"{g.BUFFCTL_TASK_PREFIX}-{self.codemp_id}")
|
rt.stop_task(f"{g.BUFFCTL_TASK_PREFIX}-{self.codemp_id}")
|
||||||
|
self.buffctl.stop()
|
||||||
logger.info(f"cleaning up virtual buffer '{self.codemp_id}'")
|
logger.info(f"cleaning up virtual buffer '{self.codemp_id}'")
|
||||||
|
|
||||||
async def apply_bufferchange_task(self):
|
async def apply_bufferchange_task(self):
|
||||||
|
@ -94,7 +95,7 @@ class VirtualBuffer:
|
||||||
logger.error(f"buffer worker '{self.codemp_id}' crashed:\n{e}")
|
logger.error(f"buffer worker '{self.codemp_id}' crashed:\n{e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def send_buffer_change(self, changes):
|
def send_buffer_change(self, changes):
|
||||||
# we do not do any index checking, and trust sublime with providing the correct
|
# 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.
|
# sequential indexing, assuming the changes are applied in the order they are received.
|
||||||
for change in changes:
|
for change in changes:
|
||||||
|
@ -104,7 +105,7 @@ class VirtualBuffer:
|
||||||
region.begin(), region.end(), change.str
|
region.begin(), region.end(), change.str
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
await self.buffctl.send(region.begin(), region.end(), change.str)
|
self.buffctl.send(region.begin(), region.end(), change.str)
|
||||||
|
|
||||||
def send_cursor(self, vws): # pyright: ignore # noqa: F821
|
def send_cursor(self, vws): # pyright: ignore # noqa: F821
|
||||||
# TODO: only the last placed cursor/selection.
|
# TODO: only the last placed cursor/selection.
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import sublime
|
import sublime
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from codemp import Client
|
import codemp
|
||||||
from Codemp.src import globals as g
|
from Codemp.src import globals as g
|
||||||
from Codemp.src.workspace import VirtualWorkspace
|
from Codemp.src.workspace import VirtualWorkspace
|
||||||
|
|
||||||
|
@ -11,43 +12,61 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class VirtualClient:
|
class VirtualClient:
|
||||||
|
handle: Optional[codemp.Client]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.handle = None
|
self.driver = codemp.init(lambda msg: logger.log(logger.level, msg), False)
|
||||||
self.workspaces: dict[str, VirtualWorkspace] = {}
|
self.workspaces: dict[str, VirtualWorkspace] = {}
|
||||||
self.active_workspace: VirtualWorkspace | None = None
|
self.active_workspace: Optional[None] = None
|
||||||
|
|
||||||
def __getitem__(self, key: str):
|
def __getitem__(self, key: str):
|
||||||
return self.workspaces.get(key)
|
return self.workspaces.get(key)
|
||||||
|
|
||||||
|
def disconnect(self):
|
||||||
|
if self.handle is None:
|
||||||
|
return
|
||||||
|
logger.info("disconnecting from the current client")
|
||||||
|
for vws in self.workspaces.values():
|
||||||
|
vws.cleanup()
|
||||||
|
|
||||||
|
self.handle = None
|
||||||
|
|
||||||
def connect(self, host: str, user: str, password: str):
|
def connect(self, host: str, user: str, password: str):
|
||||||
|
if self.handle is not None:
|
||||||
|
logger.info("Disconnecting from previous client.")
|
||||||
|
return self.disconnect()
|
||||||
|
|
||||||
logger.info(f"Connecting to {host} with user {user}")
|
logger.info(f"Connecting to {host} with user {user}")
|
||||||
try:
|
try:
|
||||||
self.handle = Client(host, user, password)
|
self.handle = codemp.Client(host, user, password)
|
||||||
|
|
||||||
|
if self.handle is not None:
|
||||||
|
id = self.handle.user_id()
|
||||||
|
logger.debug(f"Connected to '{host}' with user {user} and id: {id}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Could not connect: {e}")
|
logger.error(f"Could not connect: {e}")
|
||||||
sublime.error_message(
|
sublime.error_message(
|
||||||
"Could not connect:\n Make sure the server is up.\n\
|
"Could not connect:\n Make sure the server is up.\n\
|
||||||
or your credentials are correct."
|
or your credentials are correct."
|
||||||
)
|
)
|
||||||
return
|
raise
|
||||||
|
|
||||||
id = self.handle.user_id() # pyright: ignore
|
|
||||||
logger.debug(f"Connected to '{host}' with user {user} and id: {id}")
|
|
||||||
|
|
||||||
def join_workspace(
|
def join_workspace(
|
||||||
self,
|
self,
|
||||||
workspace_id: str,
|
workspace_id: str,
|
||||||
) -> VirtualWorkspace | None:
|
) -> VirtualWorkspace:
|
||||||
if self.handle is None:
|
if self.handle is None:
|
||||||
return
|
sublime.error_message("Connect to a server first.")
|
||||||
|
raise
|
||||||
|
|
||||||
logger.info(f"Joining workspace: '{workspace_id}'")
|
logger.info(f"Joining workspace: '{workspace_id}'")
|
||||||
try:
|
try:
|
||||||
workspace = self.handle.join_workspace(workspace_id)
|
workspace = self.handle.join_workspace(workspace_id).wait()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Could not join workspace '{workspace_id}'.\n\nerror: {e}")
|
logger.error(f"Could not join workspace '{workspace_id}'.\n\nerror: {e}")
|
||||||
sublime.error_message(f"Could not join workspace '{workspace_id}'")
|
sublime.error_message(f"Could not join workspace '{workspace_id}'")
|
||||||
return
|
raise
|
||||||
|
|
||||||
vws = VirtualWorkspace(workspace)
|
vws = VirtualWorkspace(workspace)
|
||||||
self.workspaces[workspace_id] = vws
|
self.workspaces[workspace_id] = vws
|
||||||
|
@ -56,9 +75,10 @@ class VirtualClient:
|
||||||
|
|
||||||
def leave_workspace(self, id: str):
|
def leave_workspace(self, id: str):
|
||||||
if self.handle is None:
|
if self.handle is None:
|
||||||
return False
|
raise
|
||||||
logger.info(f"Leaving workspace: '{id}'")
|
|
||||||
if self.handle.leave_workspace(id):
|
if self.handle.leave_workspace(id):
|
||||||
|
logger.info(f"Leaving workspace: '{id}'")
|
||||||
self.workspaces[id].cleanup()
|
self.workspaces[id].cleanup()
|
||||||
del self.workspaces[id]
|
del self.workspaces[id]
|
||||||
|
|
||||||
|
@ -84,7 +104,7 @@ class VirtualClient:
|
||||||
ws = self.get_workspace(view)
|
ws = self.get_workspace(view)
|
||||||
return None if ws is None else ws.get_by_local(view.buffer_id())
|
return None if ws is None else ws.get_by_local(view.buffer_id())
|
||||||
|
|
||||||
def make_active(self, ws: VirtualWorkspace | None):
|
def make_active(self, ws: Optional[VirtualWorkspace]):
|
||||||
if self.active_workspace == ws:
|
if self.active_workspace == ws:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -94,7 +114,7 @@ class VirtualClient:
|
||||||
if ws is not None:
|
if ws is not None:
|
||||||
ws.activate()
|
ws.activate()
|
||||||
|
|
||||||
self.active_workspace = ws
|
self.active_workspace = ws # pyright: ignore
|
||||||
|
|
||||||
|
|
||||||
client = VirtualClient()
|
client = VirtualClient()
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
import logging
|
|
||||||
from asyncio import CancelledError
|
|
||||||
from codemp import PyLogger
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class CodempLogger:
|
|
||||||
def __init__(self, log_level):
|
|
||||||
self.logger = logger
|
|
||||||
self.level = log_level
|
|
||||||
self.internal_logger = None
|
|
||||||
self.started = False
|
|
||||||
|
|
||||||
try:
|
|
||||||
# PyLogger spins a tracing_subscriber rust side with a
|
|
||||||
# .try_init() and errors out if already initialized.
|
|
||||||
# initialize only once
|
|
||||||
self.internal_logger = PyLogger(self.level == logging.DEBUG)
|
|
||||||
except Exception:
|
|
||||||
if self.internal_logger is None:
|
|
||||||
raise
|
|
||||||
|
|
||||||
async def listen(self):
|
|
||||||
if self.started:
|
|
||||||
return
|
|
||||||
self.started = True
|
|
||||||
self.logger.debug("spinning up internal logger listener...")
|
|
||||||
|
|
||||||
assert self.internal_logger is not None
|
|
||||||
try:
|
|
||||||
while msg := await self.internal_logger.listen():
|
|
||||||
if msg is not None:
|
|
||||||
logger.log(logging.DEBUG, msg)
|
|
||||||
else:
|
|
||||||
logger.log(logging.DEBUG, "logger sender dropped.")
|
|
||||||
break
|
|
||||||
except CancelledError:
|
|
||||||
self.logger.debug("inner logger stopped.")
|
|
||||||
self.started = False
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
self.logger.error(f"inner logger crashed unexpectedly: \n {e}")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def log(self, msg):
|
|
||||||
self.logger.log(self.level, msg)
|
|
||||||
|
|
||||||
|
|
||||||
inner_logger = CodempLogger(logging.INFO)
|
|
|
@ -1,6 +1,5 @@
|
||||||
from typing import Optional, Callable, Any
|
from typing import Optional, Callable, Any
|
||||||
|
|
||||||
from asyncio.coroutines import functools
|
|
||||||
import sublime
|
import sublime
|
||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -147,4 +146,4 @@ class Runtime:
|
||||||
|
|
||||||
# store a global in the module so it acts as a singleton
|
# store a global in the module so it acts as a singleton
|
||||||
# (modules are loaded only once)
|
# (modules are loaded only once)
|
||||||
rt = Runtime()
|
# rt = Runtime()
|
||||||
|
|
20
src/utils.py
20
src/utils.py
|
@ -1,5 +1,6 @@
|
||||||
import sublime
|
import sublime
|
||||||
import sublime_plugin
|
import sublime_plugin
|
||||||
|
from Codemp.src import globals as g
|
||||||
|
|
||||||
|
|
||||||
def status_log(msg, popup=False):
|
def status_log(msg, popup=False):
|
||||||
|
@ -47,3 +48,22 @@ def get_view_from_local_path(path):
|
||||||
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 draw_cursor_region(view, cursor):
|
||||||
|
reg = rowcol_to_region(view, cursor.start, cursor.end)
|
||||||
|
reg_flags = sublime.RegionFlags.DRAW_EMPTY
|
||||||
|
|
||||||
|
user_hash = hash(cursor.user)
|
||||||
|
|
||||||
|
def draw():
|
||||||
|
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.user], # pyright: ignore
|
||||||
|
annotation_color=g.PALETTE[user_hash % len(g.PALETTE)],
|
||||||
|
)
|
||||||
|
|
||||||
|
sublime.set_timeout_async(draw)
|
||||||
|
|
134
src/workspace.py
134
src/workspace.py
|
@ -1,4 +1,5 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import sublime
|
import sublime
|
||||||
import shutil
|
import shutil
|
||||||
|
@ -6,12 +7,14 @@ import tempfile
|
||||||
import logging
|
import logging
|
||||||
from asyncio import CancelledError
|
from asyncio import CancelledError
|
||||||
|
|
||||||
from codemp import Workspace
|
from codemp import Workspace, Promise, CursorController
|
||||||
|
|
||||||
from Codemp.src import globals as g
|
from Codemp.src import globals as g
|
||||||
from Codemp.src.buffers import VirtualBuffer
|
from Codemp.src.buffers import VirtualBuffer
|
||||||
from Codemp.src.task_manager import rt
|
|
||||||
from Codemp.src.utils import rowcol_to_region
|
# from Codemp.src.task_manager import rt
|
||||||
|
from Codemp.src.utils import draw_cursor_region
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -20,31 +23,35 @@ logger = logging.getLogger(__name__)
|
||||||
# events that happen to the codemp workspaces into sublime actions
|
# events that happen to the codemp workspaces into sublime actions
|
||||||
class VirtualWorkspace:
|
class VirtualWorkspace:
|
||||||
def __init__(self, handle: Workspace):
|
def __init__(self, handle: Workspace):
|
||||||
self.handle = handle
|
self.handle: Workspace = handle
|
||||||
self.id = self.handle.id()
|
self.id: str = self.handle.id()
|
||||||
self.sublime_window = sublime.active_window()
|
self.sublime_window: sublime.Window = sublime.active_window()
|
||||||
self.curctl = handle.cursor()
|
self.curctl: CursorController = handle.cursor()
|
||||||
self.materialized = False
|
self.materialized = False
|
||||||
self.isactive = False
|
self.isactive = False
|
||||||
|
|
||||||
# mapping remote ids -> local ids
|
# mapping remote ids -> local ids
|
||||||
self.id_map: dict[str, int] = {}
|
self.id_map: dict[str, int] = {}
|
||||||
self.active_Buffer: dict[int, VirtualBuffer] = {} # local_id -> VBuff
|
self.active_buffers: dict[int, VirtualBuffer] = {} # local_id -> VBuff
|
||||||
|
|
||||||
def cleanup(self):
|
def _cleanup(self):
|
||||||
self.deactivate()
|
self._deactivate()
|
||||||
|
|
||||||
# the worskpace only cares about closing the various open views on its Buffer.
|
# the worskpace only cares about closing the various open views on its Buffer.
|
||||||
# the event listener calls the cleanup code for each buffer independently on its own.
|
# the event listener calls the cleanup code for each buffer independently on its own
|
||||||
for vbuff in self.active_Buffer.values():
|
# upon closure.
|
||||||
|
for vbuff in self.active_buffers.values():
|
||||||
vbuff.view.close()
|
vbuff.view.close()
|
||||||
|
|
||||||
self.active_Buffer = {} # drop all Buffer, let them be garbace collected (hopefully)
|
self.active_buffers = {} # drop all Buffer, let them be garbage collected (hopefully)
|
||||||
|
|
||||||
if not self.materialized:
|
if not self.materialized:
|
||||||
return # nothing to delete
|
return # nothing to delete
|
||||||
|
|
||||||
|
# remove from the "virtual" project folders
|
||||||
d: dict = self.sublime_window.project_data() # pyright: ignore
|
d: dict = self.sublime_window.project_data() # pyright: ignore
|
||||||
|
if d is None:
|
||||||
|
raise
|
||||||
newf = list(
|
newf = list(
|
||||||
filter(
|
filter(
|
||||||
lambda f: f.get("name", "") != f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}",
|
lambda f: f.get("name", "") != f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}",
|
||||||
|
@ -56,23 +63,25 @@ class VirtualWorkspace:
|
||||||
logger.info(f"cleaning up virtual workspace '{self.id}'")
|
logger.info(f"cleaning up virtual workspace '{self.id}'")
|
||||||
shutil.rmtree(self.rootdir, ignore_errors=True)
|
shutil.rmtree(self.rootdir, ignore_errors=True)
|
||||||
|
|
||||||
|
# stop the controller
|
||||||
self.curctl.stop()
|
self.curctl.stop()
|
||||||
|
|
||||||
|
# clean the window form the tags
|
||||||
s = self.sublime_window.settings()
|
s = self.sublime_window.settings()
|
||||||
del s[g.CODEMP_WINDOW_TAG]
|
del s[g.CODEMP_WINDOW_TAG]
|
||||||
del s[g.CODEMP_WINDOW_WORKSPACES]
|
del s[g.CODEMP_WINDOW_WORKSPACES]
|
||||||
|
|
||||||
self.materialized = False
|
self.materialized = False
|
||||||
|
|
||||||
def materialize(self):
|
def _materialize(self):
|
||||||
# attach the workspace to the editor, tagging windows and populating
|
# attach the workspace to the editor, tagging windows and populating
|
||||||
# virtual file systems
|
# virtual file systems
|
||||||
if self.materialized:
|
if self.materialized:
|
||||||
return # no op, we already are in the editor
|
return # no op, we already have materialized the workspace in the editor
|
||||||
|
|
||||||
# initialise the virtual filesystem
|
# initialise the virtual filesystem
|
||||||
tmpdir = tempfile.mkdtemp(prefix="codemp_")
|
tmpdir = tempfile.mkdtemp(prefix="codemp_")
|
||||||
logging.debug("setting up virtual fs for workspace in: {} ".format(tmpdir))
|
logging.debug(f"setting up virtual fs for workspace in: {tmpdir}")
|
||||||
self.rootdir = tmpdir
|
self.rootdir = tmpdir
|
||||||
|
|
||||||
# and add a new "project folder"
|
# and add a new "project folder"
|
||||||
|
@ -94,32 +103,32 @@ class VirtualWorkspace:
|
||||||
|
|
||||||
self.materialized = True
|
self.materialized = True
|
||||||
|
|
||||||
def activate(self):
|
# def _activate(self):
|
||||||
rt.dispatch(
|
# rt.dispatch(
|
||||||
self.move_cursor_task(),
|
# self.move_cursor_task(),
|
||||||
f"{g.CURCTL_TASK_PREFIX}-{self.id}",
|
# f"{g.CURCTL_TASK_PREFIX}-{self.id}",
|
||||||
)
|
# )
|
||||||
self.isactive = True
|
# self.isactive = True
|
||||||
|
|
||||||
def deactivate(self):
|
def _deactivate(self):
|
||||||
if self.isactive:
|
if self.isactive:
|
||||||
rt.stop_task(f"{g.CURCTL_TASK_PREFIX}-{self.id}")
|
rt.stop_task(f"{g.CURCTL_TASK_PREFIX}-{self.id}")
|
||||||
|
|
||||||
self.isactive = False
|
self.isactive = False
|
||||||
|
|
||||||
def add_buffer(self, remote_id: str, vbuff: VirtualBuffer):
|
def _add_buffer(self, remote_id: str, vbuff: VirtualBuffer):
|
||||||
self.id_map[remote_id] = vbuff.view.buffer_id()
|
self.id_map[remote_id] = vbuff.view.buffer_id()
|
||||||
self.active_Buffer[vbuff.view.buffer_id()] = vbuff
|
self.active_buffers[vbuff.view.buffer_id()] = vbuff
|
||||||
|
|
||||||
def get_by_local(self, local_id: int) -> VirtualBuffer | None:
|
def _get_by_local(self, local_id: int) -> Optional[VirtualBuffer]:
|
||||||
return self.active_Buffer.get(local_id)
|
return self.active_buffers.get(local_id)
|
||||||
|
|
||||||
def get_by_remote(self, remote_id: str) -> VirtualBuffer | None:
|
def _get_by_remote(self, remote_id: str) -> Optional[VirtualBuffer]:
|
||||||
local_id = self.id_map.get(remote_id)
|
local_id = self.id_map.get(remote_id)
|
||||||
if local_id is None:
|
if local_id is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
vbuff = self.active_Buffer.get(local_id)
|
vbuff = self.active_buffers.get(local_id)
|
||||||
if vbuff is None:
|
if vbuff is None:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
"a local-remote buffer id pair was found but \
|
"a local-remote buffer id pair was found but \
|
||||||
|
@ -129,21 +138,24 @@ class VirtualWorkspace:
|
||||||
|
|
||||||
return vbuff
|
return vbuff
|
||||||
|
|
||||||
|
def create(self, id: str) -> Promise[None]:
|
||||||
|
return self.handle.create(id)
|
||||||
|
|
||||||
# A workspace has some Buffer inside of it (filetree)
|
# A workspace has some Buffer inside of it (filetree)
|
||||||
# some of those you are already attached to (Buffer_by_name)
|
# some of those you are already attached to (Buffer_by_name)
|
||||||
# If already attached to it return the same alredy existing bufferctl
|
# If already attached to it return the same alredy existing bufferctl
|
||||||
# if existing but not attached (attach)
|
# if existing but not attached (attach)
|
||||||
# if not existing ask for creation (create + attach)
|
# if not existing ask for creation (create + attach)
|
||||||
async def attach(self, id: str):
|
def attach(self, id: str):
|
||||||
if id is None:
|
if id is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
attached_Buffer = self.handle.buffer_by_name(id)
|
attached_Buffer = self.handle.buffer_by_name(id)
|
||||||
if attached_Buffer is not None:
|
if attached_Buffer is not None:
|
||||||
return self.get_by_remote(id)
|
return self._get_by_remote(id)
|
||||||
|
|
||||||
await self.handle.fetch_buffers()
|
self.handle.fetch_buffers()
|
||||||
existing_Buffer = self.handle.filetree()
|
existing_Buffer = self.handle.filetree(filter=None)
|
||||||
if id not in existing_Buffer:
|
if id not in existing_Buffer:
|
||||||
create = sublime.ok_cancel_dialog(
|
create = sublime.ok_cancel_dialog(
|
||||||
"There is no buffer named '{id}' in the workspace.\n\
|
"There is no buffer named '{id}' in the workspace.\n\
|
||||||
|
@ -153,46 +165,42 @@ class VirtualWorkspace:
|
||||||
)
|
)
|
||||||
if create:
|
if create:
|
||||||
try:
|
try:
|
||||||
await self.handle.create(id)
|
create_promise = self.create(id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"could not create buffer:\n\n {e}", True)
|
logging.error(f"could not create buffer:\n\n {e}")
|
||||||
return
|
return
|
||||||
|
create_promise.wait()
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
# now either we created it or it exists already
|
# now either we created it or it exists already
|
||||||
try:
|
try:
|
||||||
buff_ctl = await self.handle.attach(id)
|
buff_ctl = self.handle.attach(id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"error when attaching to buffer '{id}':\n\n {e}", True)
|
logging.error(f"error when attaching to buffer '{id}':\n\n {e}")
|
||||||
return
|
return
|
||||||
|
|
||||||
vbuff = VirtualBuffer(self.id, self.rootdir, id, buff_ctl)
|
vbuff = VirtualBuffer(self.id, self.rootdir, id, buff_ctl.wait())
|
||||||
self.add_buffer(id, vbuff)
|
self._add_buffer(id, vbuff)
|
||||||
|
|
||||||
# TODO! if the view is already active calling focus_view() will not trigger the on_activate
|
# TODO! if the view is already active calling focus_view() will not trigger the on_activate
|
||||||
self.sublime_window.focus_view(vbuff.view)
|
self.sublime_window.focus_view(vbuff.view)
|
||||||
|
|
||||||
def detach(self, id: str):
|
def detach(self, id: str):
|
||||||
if id is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
attached_Buffer = self.handle.buffer_by_name(id)
|
attached_Buffer = self.handle.buffer_by_name(id)
|
||||||
if attached_Buffer is None:
|
if attached_Buffer is None:
|
||||||
logging.warning(f"You are not attached to the buffer '{id}'", True)
|
sublime.error_message(f"You are not attached to the buffer '{id}'")
|
||||||
|
logging.warning(f"You are not attached to the buffer '{id}'")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.handle.detach(id)
|
self.handle.detach(id)
|
||||||
|
|
||||||
async def delete(self, id: str):
|
def delete(self, id: str):
|
||||||
if id is None:
|
self.handle.fetch_buffers()
|
||||||
return
|
existing_Buffer = self.handle.filetree(filter=None)
|
||||||
|
|
||||||
# delete a non existent buffer
|
|
||||||
await self.handle.fetch_buffers()
|
|
||||||
existing_Buffer = self.handle.filetree()
|
|
||||||
if id not in existing_Buffer:
|
if id not in existing_Buffer:
|
||||||
logging.info(f"The buffer '{id}' does not exists.", True)
|
sublime.error_message(f"The buffer '{id}' does not exists.")
|
||||||
|
logging.info(f"The buffer '{id}' does not exists.")
|
||||||
return
|
return
|
||||||
# delete a buffer that exists but you are not attached to
|
# delete a buffer that exists but you are not attached to
|
||||||
attached_Buffer = self.handle.buffer_by_name(id)
|
attached_Buffer = self.handle.buffer_by_name(id)
|
||||||
|
@ -204,7 +212,7 @@ class VirtualWorkspace:
|
||||||
)
|
)
|
||||||
if delete:
|
if delete:
|
||||||
try:
|
try:
|
||||||
await self.handle.delete(id)
|
self.handle.delete(id).wait()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"error when deleting the buffer '{id}':\n\n {e}", True
|
f"error when deleting the buffer '{id}':\n\n {e}", True
|
||||||
|
@ -221,9 +229,9 @@ class VirtualWorkspace:
|
||||||
title="Delete Buffer?",
|
title="Delete Buffer?",
|
||||||
)
|
)
|
||||||
if delete:
|
if delete:
|
||||||
self.detach(id)
|
self.handle.detach(id)
|
||||||
try:
|
try:
|
||||||
await self.handle.delete(id)
|
self.handle.delete(id).wait()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"error when deleting the buffer '{id}':\n\n {e}", True)
|
logging.error(f"error when deleting the buffer '{id}':\n\n {e}", True)
|
||||||
return
|
return
|
||||||
|
@ -231,24 +239,14 @@ class VirtualWorkspace:
|
||||||
async def move_cursor_task(self):
|
async def move_cursor_task(self):
|
||||||
logger.debug(f"spinning up cursor worker for workspace '{self.id}'...")
|
logger.debug(f"spinning up cursor worker for workspace '{self.id}'...")
|
||||||
try:
|
try:
|
||||||
while cursor_event := await self.curctl.recv():
|
# blocking for now ...
|
||||||
vbuff = self.get_by_remote(cursor_event.buffer)
|
while cursor_event := self.curctl.recv().wait():
|
||||||
|
vbuff = self._get_by_remote(cursor_event.buffer)
|
||||||
|
|
||||||
if vbuff is None:
|
if vbuff is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
reg = rowcol_to_region(vbuff.view, cursor_event.start, cursor_event.end)
|
draw_cursor_region(vbuff.view, cursor_event)
|
||||||
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], # pyright: ignore
|
|
||||||
annotation_color=g.PALETTE[user_hash % len(g.PALETTE)],
|
|
||||||
)
|
|
||||||
|
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
logger.debug(f"cursor worker for '{self.id}' stopped...")
|
logger.debug(f"cursor worker for '{self.id}' stopped...")
|
||||||
|
|
Loading…
Reference in a new issue