From cba895bcbb9a571ade3d310b92c7dd002944d213 Mon Sep 17 00:00:00 2001 From: cschen Date: Fri, 9 Aug 2024 14:23:29 +0200 Subject: [PATCH] feat: leave workspace command and separation between activating a workspace and materializing it on disk and in the editor Former-commit-id: 425996d1a0ab49fee972a4a9263eae30bf6091d7 --- Codemp.sublime-commands | 15 +++----- src/client.py | 76 +++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/Codemp.sublime-commands b/Codemp.sublime-commands index 7db5f09..0ce1ac1 100644 --- a/Codemp.sublime-commands +++ b/Codemp.sublime-commands @@ -45,18 +45,11 @@ } }, { - "caption": "Codemp: Join Workspace", - "command": "codemp_join_workspace", + "caption": "Codemp: Leave Workspace", + "command": "codemp_leave_workspace", "arg": { - // 'workspace_id' : 'asd' - }, - }, - { - "caption": "Codemp: Join buffer", - "command": "codemp_join_buffer", - "arg": { - // 'buffer_id' : 'test' - }, + // "id": 'lmaaaao' + } }, { "caption": "Codemp: Disconnect Client", diff --git a/src/client.py b/src/client.py index 3765cb6..66b2b85 100644 --- a/src/client.py +++ b/src/client.py @@ -2,7 +2,6 @@ from __future__ import annotations from typing import Optional import sublime -import random import asyncio import tempfile import os @@ -160,12 +159,52 @@ class VirtualWorkspace: self.id = self.handle.id() self.sublime_window = sublime.active_window() self.curctl = handle.cursor() + self.materialized = False self.isactive = False # mapping remote ids -> local ids self.id_map: dict[str, int] = {} self.active_buffers: dict[int, VirtualBuffer] = {} # local_id -> VBuff + def cleanup(self): + self.deactivate() + + # 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.values(): + vbuff.view.close() + + self.active_buffers = {} # drop all buffers, let them be garbace collected (hopefully) + + if not self.materialized: + return # nothing to delete + + d: dict = self.sublime_window.project_data() # pyright: ignore + newf = list( + filter( + lambda f: f.get("name", "") != f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}", + d["folders"], + ) + ) + d["folders"] = newf + self.sublime_window.set_project_data(d) + status_log(f"cleaning up virtual workspace '{self.id}'") + shutil.rmtree(self.rootdir, ignore_errors=True) + + self.curctl.stop() + + s = self.sublime_window.settings() + del s[g.CODEMP_WINDOW_TAG] + del s[g.CODEMP_WINDOW_WORKSPACES] + + 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)) @@ -188,33 +227,7 @@ class VirtualWorkspace: s[g.CODEMP_WINDOW_TAG] = True s[g.CODEMP_WINDOW_WORKSPACES] = [self.id] - def cleanup(self): - self.deactivate() - - # 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.values(): - vbuff.view.close() - - self.active_buffers = {} # drop all buffers, let them be garbace collected (hopefully) - - d: dict = self.sublime_window.project_data() # pyright: ignore - newf = list( - filter( - lambda f: f.get("name", "") != f"{g.WORKSPACE_FOLDER_PREFIX}{self.id}", - d["folders"], - ) - ) - d["folders"] = newf - self.sublime_window.set_project_data(d) - status_log(f"cleaning up virtual workspace '{self.id}'") - shutil.rmtree(self.rootdir, ignore_errors=True) - - self.curctl.stop() - - s = self.sublime_window.settings() - del s[g.CODEMP_WINDOW_TAG] - del s[g.CODEMP_WINDOW_WORKSPACES] + self.materialized = True def activate(self): tm.dispatch( @@ -226,6 +239,7 @@ class VirtualWorkspace: def deactivate(self): if self.isactive: tm.stop(f"{g.CURCTL_TASK_PREFIX}-{self.id}") + self.isactive = False def add_buffer(self, remote_id: str, vbuff: VirtualBuffer): @@ -419,7 +433,7 @@ class VirtualClient: vws = VirtualWorkspace(workspace) self.workspaces[workspace_id] = vws - self.make_active(vws) + # self.make_active(vws) return vws @@ -428,7 +442,9 @@ class VirtualClient: status_log("Connect to a server first!", True) return False status_log(f"Leaving workspace: '{id}'") - return self.handle.leave_workspace(id) + if self.handle.leave_workspace(id): + self.workspaces[id].cleanup() + del self.workspaces[id] def get_workspace(self, view): tag_id = view.settings().get(g.CODEMP_WORKSPACE_ID)