From ce87c5334d595608f7ca4e07b5dd443b970e5bfe Mon Sep 17 00:00:00 2001
From: cschen <camillo.schenone@gmail.com>
Date: Tue, 18 Feb 2025 22:36:14 +0100
Subject: [PATCH] made create and delete workspaces as internal commands as
 well. Fixed the same issues that the workspace browser had for the creation
 and deletion also in the server browser.

---
 Codemp.sublime-commands        | 28 ++++++++++++++--------------
 plugin/commands/client.py      | 29 +++++++++++++++--------------
 plugin/quickpanel/qpbrowser.py | 27 +++++++++++++++++++--------
 3 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/Codemp.sublime-commands b/Codemp.sublime-commands
index 42523c6..c6c0858 100644
--- a/Codemp.sublime-commands
+++ b/Codemp.sublime-commands
@@ -64,20 +64,20 @@
       // "user": 'lupo'
     }
   },
-  {
-    "caption": "Codemp: Create Workspace",
-    "command": "codemp_create_workspace",
-    "args": {
-      // "id": 'lmaaaao'
-    }
-  },
-  {
-    "caption": "Codemp: Delete Workspace",
-    "command": "codemp_delete_workspace",
-    "args": {
-      // "id": 'lmaaaao'
-    }
-  },
+  // {
+  //   "caption": "Codemp: Create Workspace",
+  //   "command": "codemp_create_workspace",
+  //   "args": {
+  //     // "id": 'lmaaaao'
+  //   }
+  // },
+  // {
+  //   "caption": "Codemp: Delete Workspace",
+  //   "command": "codemp_delete_workspace",
+  //   "args": {
+  //     // "id": 'lmaaaao'
+  //   }
+  // },
   {
     "caption": "Codemp: Join Buffer",
     "command": "codemp_join_buffer",
diff --git a/plugin/commands/client.py b/plugin/commands/client.py
index bdadae7..16714fc 100644
--- a/plugin/commands/client.py
+++ b/plugin/commands/client.py
@@ -208,9 +208,9 @@ class CodempCreateWorkspaceCommand(sublime_plugin.WindowCommand):
     def is_enabled(self):
         return session.is_active()
 
-    def input(self, args):
-        if "workspace_id" not in args:
-            return SimpleTextInput(("workspace_id", "new workspace name"))
+    # def input(self, args):
+    #     if "workspace_id" not in args:
+    #         return SimpleTextInput(("workspace_id", "new workspace name"))
 
     def run(self, workspace_id: str):  # pyright: ignore[reportIncompatibleMethodOverride]
         try:
@@ -224,25 +224,26 @@ class CodempDeleteWorkspaceCommand(sublime_plugin.WindowCommand):
     def is_enabled(self):
         return session.is_active()
 
-    def input(self, args):
-        workspaces = session.get_workspaces(owned=True, invited=False)  # noqa: F841
-        if "workspace_id" not in args:
-            return SimpleListInput(("workspace_id", workspaces))
+    # def input(self, args):
+    #     workspaces = session.get_workspaces(owned=True, invited=False)  # noqa: F841
+    #     if "workspace_id" not in args:
+    #         return SimpleListInput(("workspace_id", workspaces))
 
     def run(self, workspace_id: str):  # pyright: ignore[reportIncompatibleMethodOverride]
-        try:
-            vws = workspaces.lookupId(workspace_id)
+        if workspace_id in workspaces:
             if not sublime.ok_cancel_dialog(
                 "You are currently attached to '{workspace_id}'.\n\
                 Do you want to detach and delete it?",
                 ok_title="yes", title="Delete Workspace?",
-            ):
-                return
+            ): return
             self.window.run_command(
                 "codemp_leave_workspace",
                 {"workspace_id": workspace_id})
+        else:
+            if not sublime.ok_cancel_dialog(
+                f"Confirm you want to delete the workspace '{workspace_id}'",
+                ok_title="delete", title="Delete Workspace?",
+            ): return
 
-        except KeyError: pass
-        finally:
-            session.client.delete_workspace(workspace_id)
+        session.client.delete_workspace(workspace_id).wait()
 
diff --git a/plugin/quickpanel/qpbrowser.py b/plugin/quickpanel/qpbrowser.py
index 21a7326..ef4dec3 100644
--- a/plugin/quickpanel/qpbrowser.py
+++ b/plugin/quickpanel/qpbrowser.py
@@ -71,6 +71,7 @@ class QPServerBrowser():
     def edit_server(self):
         actions = [
             qpi("Back", color=qpg.QP_COLOR_CYANISH, letter=qpg.QP_BACK),
+            qpi("Disconnect", color=qpg.QP_COLOR_REDISH, letter=qpg.QP_BACK),
             qpi("New Workspace", color=qpg.QP_COLOR_GREENISH, letter=qpg.QP_ADD),
             qpi("Delete Workspace", color=qpg.QP_COLOR_REDISH, letter=qpg.QP_NO)
         ]
@@ -84,27 +85,37 @@ class QPServerBrowser():
             self.run()
 
         if index == 1:
+            self.window.run_command("codemp_disconnect", {})
+
+        if index == 2:
             def create_workspace(name):
                 self.window.run_command(
                     "codemp_create_workspace", {"workspace_id": name})
-                self.run()
+                self.window.run_command(
+                    "codemp_browse_server", {})
+
             self.window.show_input_panel("New Workspace Name", "", create_workspace, None, self.edit_server)
 
-        if index == 2:
+        if index == 3:
             def delete_workspace(index):
                 if index == -1 or index == 0:
                     self.edit_server()
-                # we must be careful here. here with index 1 we are selecting the correct
-                # workspace, because the index zero in the entries is the workspace action submenu.
-                # which is occupied by the back action.
-                # if we add extra non workspace entries, then we must shift the index accordingly.
-                # Do this differently?
+                    return
+
                 selected = self.entries[index]
                 self.window.run_command(
                     "codemp_delete_workspace",
                     {"workspace_id": selected.trigger})
+                self.window.run_command(
+                    "codemp_browse_server", {})
 
-
+            if len(self.entries) < 2:
+                sublime.message_dialog("You don't have workspaces to delete!")
+                sublime.set_timeout(self.run, 10)
+            else:
+                selentries = self.entries
+                selentries[0] = qpi("Back", color=qpg.QP_COLOR_CYANISH, letter=qpg.QP_BACK)
+                show_qp(self.window, selentries, delete_workspace, self.qp_placeholder(), keepopen=False)
             show_qp(self.window, self.entries, delete_workspace, self.qp_placeholder())