feat: leave workspace command and separation between activating a workspace and materializing it on disk and in the editor

Former-commit-id: 425996d1a0ab49fee972a4a9263eae30bf6091d7
This commit is contained in:
cschen 2024-08-09 14:23:29 +02:00
parent 1cca6ede6e
commit cba895bcbb
2 changed files with 50 additions and 41 deletions

View file

@ -45,18 +45,11 @@
} }
}, },
{ {
"caption": "Codemp: Join Workspace", "caption": "Codemp: Leave Workspace",
"command": "codemp_join_workspace", "command": "codemp_leave_workspace",
"arg": { "arg": {
// 'workspace_id' : 'asd' // "id": 'lmaaaao'
}, }
},
{
"caption": "Codemp: Join buffer",
"command": "codemp_join_buffer",
"arg": {
// 'buffer_id' : 'test'
},
}, },
{ {
"caption": "Codemp: Disconnect Client", "caption": "Codemp: Disconnect Client",

View file

@ -2,7 +2,6 @@ from __future__ import annotations
from typing import Optional from typing import Optional
import sublime import sublime
import random
import asyncio import asyncio
import tempfile import tempfile
import os import os
@ -160,12 +159,52 @@ class VirtualWorkspace:
self.id = self.handle.id() self.id = self.handle.id()
self.sublime_window = sublime.active_window() self.sublime_window = sublime.active_window()
self.curctl = handle.cursor() self.curctl = handle.cursor()
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_buffers: dict[int, VirtualBuffer] = {} # local_id -> VBuff 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 # initialise the virtual filesystem
tmpdir = tempfile.mkdtemp(prefix="codemp_") tmpdir = tempfile.mkdtemp(prefix="codemp_")
status_log("setting up virtual fs for workspace in: {} ".format(tmpdir)) 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_TAG] = True
s[g.CODEMP_WINDOW_WORKSPACES] = [self.id] s[g.CODEMP_WINDOW_WORKSPACES] = [self.id]
def cleanup(self): self.materialized = True
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]
def activate(self): def activate(self):
tm.dispatch( tm.dispatch(
@ -226,6 +239,7 @@ class VirtualWorkspace:
def deactivate(self): def deactivate(self):
if self.isactive: if self.isactive:
tm.stop(f"{g.CURCTL_TASK_PREFIX}-{self.id}") tm.stop(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):
@ -419,7 +433,7 @@ class VirtualClient:
vws = VirtualWorkspace(workspace) vws = VirtualWorkspace(workspace)
self.workspaces[workspace_id] = vws self.workspaces[workspace_id] = vws
self.make_active(vws) # self.make_active(vws)
return vws return vws
@ -428,7 +442,9 @@ class VirtualClient:
status_log("Connect to a server first!", True) status_log("Connect to a server first!", True)
return False return False
status_log(f"Leaving workspace: '{id}'") 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): def get_workspace(self, view):
tag_id = view.settings().get(g.CODEMP_WORKSPACE_ID) tag_id = view.settings().get(g.CODEMP_WORKSPACE_ID)