chore: minor cleanup and remove arbitrary names from lambdas

Former-commit-id: 2c474ea008d1b287b8e7daf293d4029a6f9f3527
This commit is contained in:
cschen 2024-09-17 17:20:00 +02:00
parent 058e12075b
commit bace9ca674
4 changed files with 18 additions and 37 deletions

View file

@ -25,7 +25,6 @@ class CodempConnectCommand(sublime_plugin.WindowCommand):
try: try:
client.connect(server_host, user_name, password) client.connect(server_host, user_name, password)
except Exception as e: except Exception as 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\
and your credentials are correct." and your credentials are correct."
@ -79,7 +78,7 @@ class CodempJoinWorkspaceCommand(sublime_plugin.WindowCommand):
promise = client.codemp.join_workspace(workspace_id) promise = client.codemp.join_workspace(workspace_id)
active_window = sublime.active_window() active_window = sublime.active_window()
def defer_instantiation(promise): def _():
try: try:
workspace = promise.wait() workspace = promise.wait()
except Exception as e: except Exception as e:
@ -90,7 +89,7 @@ class CodempJoinWorkspaceCommand(sublime_plugin.WindowCommand):
return return
client.install_workspace(workspace, active_window) client.install_workspace(workspace, active_window)
sublime.set_timeout_async(lambda: defer_instantiation(promise)) sublime.set_timeout_async(_)
# the else shouldn't really happen, and if it does, it should already be instantiated. # the else shouldn't really happen, and if it does, it should already be instantiated.
# ignore. # ignore.

View file

@ -10,10 +10,9 @@ from ..lib import codemp
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def make_bufferchange_cb(buff: VirtualBuffer): def make_bufferchange_cb(buff: VirtualBuffer):
def __callback(bufctl: codemp.BufferController): def __callback(bufctl: codemp.BufferController):
def get_change_and_apply(): def _():
change_id = buff.view.change_id() change_id = buff.view.change_id()
while change := bufctl.try_recv().wait(): while change := bufctl.try_recv().wait():
logger.debug("received remote buffer change!") logger.debug("received remote buffer change!")
@ -43,15 +42,10 @@ def make_bufferchange_cb(buff: VirtualBuffer):
}, # pyright: ignore }, # pyright: ignore
) )
sublime.set_timeout(get_change_and_apply) sublime.set_timeout(_)
return __callback return __callback
# 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: class VirtualBuffer:
def __init__( def __init__(
self, self,
@ -61,7 +55,7 @@ class VirtualBuffer:
): ):
self.buffctl = buffctl self.buffctl = buffctl
self.view = view self.view = view
self.id = self.buffctl.name() self.id = self.buffctl.path()
self.tmpfile = os.path.join(rootdir, self.id) self.tmpfile = os.path.join(rootdir, self.id)
open(self.tmpfile, "a").close() open(self.tmpfile, "a").close()
@ -70,9 +64,8 @@ class VirtualBuffer:
self.view.set_name(self.id) self.view.set_name(self.id)
self.view.retarget(self.tmpfile) self.view.retarget(self.tmpfile)
s = self.view.settings() self.view.settings().set(g.CODEMP_BUFFER_TAG, True)
self.view.set_status(g.SUBLIME_STATUS_ID, "[Codemp]") self.view.set_status(g.SUBLIME_STATUS_ID, "[Codemp]")
s[g.CODEMP_BUFFER_TAG] = True
logger.info(f"registering a callback for buffer: {self.id}") logger.info(f"registering a callback for buffer: {self.id}")
self.buffctl.callback(make_bufferchange_cb(self)) self.buffctl.callback(make_bufferchange_cb(self))
@ -103,13 +96,13 @@ class VirtualBuffer:
def sync(self, text_listener): def sync(self, text_listener):
promise = self.buffctl.content() promise = self.buffctl.content()
def defer_sync(promise): def _():
content = promise.wait() content = promise.wait()
safe_listener_detach(text_listener) safe_listener_detach(text_listener)
populate_view(self.view, content) populate_view(self.view, content)
safe_listener_attach(text_listener, self.view.buffer()) safe_listener_attach(text_listener, self.view.buffer())
sublime.set_timeout_async(lambda: defer_sync(promise)) sublime.set_timeout_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

View file

@ -32,20 +32,7 @@ class VirtualClient:
self._view2buff: dict[sublime.View, VirtualBuffer] = {} self._view2buff: dict[sublime.View, VirtualBuffer] = {}
self._buff2workspace: bidict[VirtualBuffer, VirtualWorkspace] = bidict() self._buff2workspace: bidict[VirtualBuffer, VirtualWorkspace] = bidict()
self._workspace2window: dict[VirtualWorkspace, sublime.Window] = bidict() self._workspace2window: dict[VirtualWorkspace, sublime.Window] = {}
def dump(self):
logger.debug("CLIENT STATUS:")
logger.debug(f"codemp: {self.codemp is not None}")
logger.debug(f"drived: {self.driver is not None}")
logger.debug("WORKSPACES:")
logger.debug(f"{self._id2workspace}")
logger.debug(f"{self._workspace2window}")
logger.debug(f"{self._buff2workspace}")
logger.debug(f"{self._buff2workspace.inverse}")
logger.debug("VIEWS")
logger.debug(f"{self._view2buff}")
logger.debug(f"{self._id2buffer}")
def all_workspaces( def all_workspaces(
self, window: Optional[sublime.Window] = None self, window: Optional[sublime.Window] = None
@ -127,7 +114,12 @@ class VirtualClient:
"could not register the logger... If reconnecting it's ok, the previous logger is still registered" "could not register the logger... If reconnecting it's ok, the previous logger is still registered"
) )
self.codemp = codemp.connect(host, user, password).wait() config = codemp.get_default_config()
config.username = user
config.host = host
config.password = password
self.codemp = codemp.connect(config).wait()
id = self.codemp.user_id() id = self.codemp.user_id()
logger.debug(f"Connected to '{host}' as user {user} (id: {id})") logger.debug(f"Connected to '{host}' as user {user} (id: {id})")
@ -148,8 +140,6 @@ class VirtualClient:
self.unregister_buffer(vbuff) self.unregister_buffer(vbuff)
vws.uninstall() vws.uninstall()
# self._buff2workspace.inverse_del(vws) - if we delete all straight
# keys the last delete will remove also the empty key.
def unregister_buffer(self, buffer: VirtualBuffer): def unregister_buffer(self, buffer: VirtualBuffer):
del self._buff2workspace[buffer] del self._buff2workspace[buffer]

View file

@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Optional, Tuple from typing import Optional, Tuple
from listeners import CodempClientTextChangeListener from ..listeners import CodempClientTextChangeListener
import sublime import sublime
import shutil import shutil
import tempfile import tempfile
@ -17,7 +17,7 @@ logger = logging.getLogger(__name__)
def make_cursor_callback(workspace: VirtualWorkspace): def make_cursor_callback(workspace: VirtualWorkspace):
def _callback(ctl: codemp.CursorController): def _callback(ctl: codemp.CursorController):
def get_event_and_draw(): def _():
while event := ctl.try_recv().wait(): while event := ctl.try_recv().wait():
logger.debug("received remote cursor movement!") logger.debug("received remote cursor movement!")
if event is None: if event is None:
@ -32,7 +32,7 @@ def make_cursor_callback(workspace: VirtualWorkspace):
draw_cursor_region(vbuff.view, event.start, event.end, event.user) draw_cursor_region(vbuff.view, event.start, event.end, event.user)
sublime.set_timeout_async(get_event_and_draw) sublime.set_timeout_async(_)
return _callback return _callback
@ -53,7 +53,6 @@ class VirtualWorkspace:
self._id2buff: dict[str, VirtualBuffer] = {} self._id2buff: dict[str, VirtualBuffer] = {}
tmpdir = tempfile.mkdtemp(prefix="codemp_") tmpdir = tempfile.mkdtemp(prefix="codemp_")
logging.debug(f"setting up virtual fs for workspace in: {tmpdir}")
self.rootdir = tmpdir self.rootdir = tmpdir
proj: dict = self.window.project_data() # pyright: ignore proj: dict = self.window.project_data() # pyright: ignore