From 0a25a5f7c1dd9421a59a32562c60a3eccf9d8b16 Mon Sep 17 00:00:00 2001 From: cschen <camillo.schenone@gmail.com> Date: Tue, 18 Feb 2025 20:25:26 +0100 Subject: [PATCH] Make create buffer and delete buffer, be internal commands. Dealing with the input handlers sheningans is too much. Also improves the logic in the qpbrowser for the creation and deletion of buffers. and simplify the logic in the internal commands. --- Codemp.sublime-commands | 32 +++++++-------- plugin/commands/workspace.py | 73 ++++++++-------------------------- plugin/core/buffers.py | 1 - plugin/input_handlers.py | 1 + plugin/quickpanel/qpbrowser.py | 23 +++++++++-- 5 files changed, 53 insertions(+), 77 deletions(-) diff --git a/Codemp.sublime-commands b/Codemp.sublime-commands index 47f7c59..42523c6 100644 --- a/Codemp.sublime-commands +++ b/Codemp.sublime-commands @@ -94,22 +94,22 @@ // 'buffer_id': 'test' } }, - { - "caption": "Codemp: Create Buffer", - "command": "codemp_create_buffer", - "args": { - // 'workspace_id': 'asd' - // 'buffer_id': 'test' - } - }, - { - "caption": "Codemp: Delete Buffer", - "command": "codemp_delete_buffer", - "args": { - // 'workspace_id': 'asd' - // 'buffer_id': 'test' - } - }, + // { + // "caption": "Codemp: Create Buffer", + // "command": "codemp_create_buffer", + // "args": { + // // 'workspace_id': 'asd' + // // 'buffer_id': 'test' + // } + // }, + // { + // "caption": "Codemp: Delete Buffer", + // "command": "codemp_delete_buffer", + // "args": { + // // 'workspace_id': 'asd' + // // 'buffer_id': 'test' + // } + // }, { "caption": "Codemp: Sync", "command": "codemp_sync_buffer", diff --git a/plugin/commands/workspace.py b/plugin/commands/workspace.py index a2012f7..7da978f 100644 --- a/plugin/commands/workspace.py +++ b/plugin/commands/workspace.py @@ -116,28 +116,10 @@ class CodempCreateBufferCommand(sublime_plugin.WindowCommand): def is_enabled(self): return len(workspaces.lookup()) > 0 - def input_description(self) -> str: - return "Create Buffer: " - - def input(self, args): - missingargs = [arg for arg in ["workspace_id", "buffer_id"] if arg not in args] - for arg in missingargs: - if arg == "workspace_id": - return SimpleListInput([ - ("workspace_id", session.client.active_workspaces()), - ("buffer_id", "new buffer name") - ]) - if arg == "buffer_id": - return SimpleTextInput( - ("buffer_id", "new buffer name"), - ) - def run(self, workspace_id, buffer_id):# pyright: ignore[reportIncompatibleMethodOverride] try: vws = workspaces.lookupId(workspace_id) except KeyError: - sublime.error_message( - f"You are not attached to the workspace '{workspace_id}'" - ) + sublime.error_message(f"You are not attached to the workspace '{workspace_id}'") logging.warning(f"You are not attached to the workspace '{workspace_id}'") return @@ -151,53 +133,30 @@ class CodempDeleteBufferCommand(sublime_plugin.WindowCommand): def is_enabled(self): return len(workspaces.lookup()) > 0 - def input_description(self) -> str: - return "Delete buffer: " - - def input(self, args): - # FIXME: THIS DOES NOT WORK SORRY - if "workspace_id" not in args: - return SimpleListInput( - ("workspace_id", session.get_workspaces(owned=True, invited=False)), - ) - - if "buffer_id" not in args: - try: ws = workspaces.lookupId(args["workspace_id"]) - except KeyError: - sublime.error_message("Workspace does not exists or is not attached.") - return sublime_plugin.BackInputHandler() - - bflist = ws.handle.fetch_buffers().wait() - return SimpleListInput( - ("buffer_id", bflist), - ) - def run(self, workspace_id, buffer_id):# pyright: ignore[reportIncompatibleMethodOverride] + try: vws = workspaces.lookupId(workspace_id) except KeyError: - sublime.error_message( - f"You are not attached to the workspace '{workspace_id}'" - ) + sublime.error_message(f"You are not attached to the workspace '{workspace_id}'") logging.warning(f"You are not attached to the workspace '{workspace_id}'") return - if not sublime.ok_cancel_dialog( - f"Confirm you want to delete the buffer '{buffer_id}'", - ok_title="delete", title="Delete Buffer?", - ): return + if buffer_id in buffers: - try: - buffers.lookupId(buffer_id) if not sublime.ok_cancel_dialog( "You are currently attached to '{buffer_id}'.\n\ Do you want to detach and delete it?", ok_title="yes", title="Delete Buffer?", - ): - return - self.window.run_command( - "codemp_leave_buffer", - { "workspace_id": workspace_id, "buffer_id": buffer_id }) - except KeyError: pass - finally: - vws.handle.delete_buffer(buffer_id).wait() + ): return + + self.window.run_command( + "codemp_leave_buffer", {"buffer_id": buffer_id }) + + else: + if not sublime.ok_cancel_dialog( + f"Confirm you want to delete the buffer '{buffer_id}'", + ok_title="delete", title="Delete Buffer?", + ): return + + vws.handle.delete_buffer(buffer_id).wait() diff --git a/plugin/core/buffers.py b/plugin/core/buffers.py index 52dbdf4..b535a80 100644 --- a/plugin/core/buffers.py +++ b/plugin/core/buffers.py @@ -175,7 +175,6 @@ class BufferRegistry(): del self._buffers[bf] - buffers = BufferRegistry() diff --git a/plugin/input_handlers.py b/plugin/input_handlers.py index 3af16b5..8943ab9 100644 --- a/plugin/input_handlers.py +++ b/plugin/input_handlers.py @@ -1,5 +1,6 @@ import sublime_plugin + # Input handlers ############################################################ class SimpleTextInput(sublime_plugin.TextInputHandler): diff --git a/plugin/quickpanel/qpbrowser.py b/plugin/quickpanel/qpbrowser.py index 8abeed5..6f60180 100644 --- a/plugin/quickpanel/qpbrowser.py +++ b/plugin/quickpanel/qpbrowser.py @@ -108,6 +108,7 @@ class QPServerBrowser(): def create_workspace(name): self.window.run_command( "codemp_create_workspace", {"workspace_id": name}) + self.run() self.window.show_input_panel("New Workspace Name", "", create_workspace, None, self.edit_server) if index == 2: @@ -201,21 +202,37 @@ class QPWorkspaceBrowser(): "workspace_id": self.workspace_id, "buffer_id": name }) - self.window.show_input_panel("New Buffer Name", "", create_buffer, None, self.edit_workspace) + + self.window.run_command( + "codemp_browse_workspace", {"workspace_id": self.workspace_id}) + + panel = self.window.show_input_panel("New Buffer Name", "", create_buffer, None, self.edit_workspace) + panel.settings().set("password", False) elif index == 4: def delete_buffer(index): if index == -1 or index == 0: self.edit_workspace() # same warning as the server browser. Check your indexed 3 times - selected = self.entries[index] + selected = self.entries[index+1] self.window.run_command( "codemp_delete_buffer", { "workspace_id": self.workspace_id, "buffer_id": selected.trigger }) - show_qp(self.window, self.entries, delete_buffer, self.qp_placeholder()) + + def _(): + buffers = workspaces.lookupId(self.workspace_id).handle.fetch_buffers() + QPWorkspaceBrowser(self.window, self.workspace_id, buffers.wait()).run() + sublime.set_timeout(_) + + if len(self.entries) < 2: + sublime.message_dialog("The workspace is empty!") + sublime.set_timeout(self.run, 10) + else: + show_qp(self.window, self.entries[1:], delete_buffer, self.qp_placeholder()) + elif index == 5: sublime.message_dialog("renaming is not yet implemented.") self.edit_workspace() \ No newline at end of file