feat: added last two commands: create and delete buffer.

we need testing now.

Former-commit-id: 0a35d398abdd0c39837902aaab3b5842f428c137
This commit is contained in:
cschen 2024-08-25 20:27:29 +02:00
parent abc976e3e5
commit 296ef0ad36
2 changed files with 101 additions and 57 deletions

108
plugin.py
View file

@ -3,7 +3,7 @@ import sublime
import sublime_plugin
import logging
import random
from typing import List, Tuple
from typing import Tuple
import codemp
from Codemp.src.client import client
@ -28,6 +28,9 @@ logger = logging.getLogger(__name__)
TEXT_LISTENER = None
# the actual client gets initialized upon plugin loading as a singleton
# in its own submodule.
# Initialisation and Deinitialisation
##############################################################################
@ -238,7 +241,7 @@ class CodempJoinWorkspaceCommand(sublime_plugin.WindowCommand):
def input(self, args):
if "workspace_id" not in args:
return WorkspaceIdText()
return SimpleTextInput(("workspace_id", ""))
# To allow for having a selection and choosing non existing workspaces
@ -363,6 +366,8 @@ class CodempLeaveBufferCommand(sublime_plugin.WindowCommand):
vws = client.workspace_from_id(workspace_id)
if vbuff is None or vws is None:
sublime.error_message(f"You are not attached to the buffer '{id}'")
logging.warning(f"You are not attached to the buffer '{id}'")
return
def defer_detach():
@ -382,6 +387,100 @@ class CodempLeaveBufferCommand(sublime_plugin.WindowCommand):
return BufferIdList(args["workspace_id"])
# Leave Buffer Comand
class CodempCreateBufferCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return len(client.all_workspaces(self.window)) > 0
def run(self, workspace_id, buffer_id):
vws = client.workspace_from_id(workspace_id)
if vws is None:
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
vws.codemp.create(buffer_id)
logging.info(
"created buffer '{buffer_id}' in the workspace '{workspace_id}'.\n\
To interact with it you need to attach to it with Codemp: Attach."
)
def input_description(self) -> str:
return "Create Buffer: "
def input(self, args):
if "workspace_id" not in args:
return ActiveWorkspacesIdList(self.window, get_buffer=True)
if "buffer_id" not in args:
return SimpleTextInput(("buffer_id", "new buffer"))
class CodempDeleteBufferCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return len(client.all_buffers()) > 0
def run(self, workspace_id, buffer_id):
vws = client.workspace_from_id(workspace_id)
if vws is None:
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
fetch_promise = vws.codemp.fetch_buffers()
delete = sublime.ok_cancel_dialog(
f"Confirm you want to delete the buffer '{buffer_id}'",
ok_title="delete",
title="Delete Buffer?",
)
if not delete:
return
fetch_promise.wait()
existing = vws.codemp.filetree(buffer_id)
if len(existing) == 0:
sublime.error_message(
f"The buffer '{buffer_id}' does not exists in the workspace."
)
logging.info(f"The buffer '{buffer_id}' does not exists in the workspace.")
return
def deferred_delete():
try:
vws.codemp.delete(buffer_id).wait()
except Exception as e:
logging.error(f"error when deleting the buffer '{id}':\n\n {e}", True)
return
vbuff = client.buffer_from_id(buffer_id)
if vbuff is None:
# we are not attached to it!
sublime.set_timeout_async(deferred_delete)
else:
if vws.codemp.detach(buffer_id):
vws.uninstall_buffer(vbuff)
sublime.set_timeout_async(deferred_delete)
else:
logging.error(
f"error while detaching from buffer '{buffer_id}', aborting the delete."
)
return
def input_description(self) -> str:
return "Delete buffer: "
def input(self, args):
if "workspace_id" not in args:
return ActiveWorkspacesIdList(self.window, get_buffer=True)
if "buffer_id" not in args:
return BufferIdList(args["workspace_id"])
# Text Change Command
#############################################################################
class CodempReplaceTextCommand(sublime_plugin.TextCommand):
@ -412,11 +511,6 @@ class SimpleTextInput(sublime_plugin.TextInputHandler):
return SimpleTextInput(*self.next_inputs)
class WorkspaceIdText(sublime_plugin.TextInputHandler):
def name(self):
return "workspace_id"
class ActiveWorkspacesIdList(sublime_plugin.ListInputHandler):
def __init__(self, window=None, get_buffer=False):
self.window = window

View file

@ -152,56 +152,6 @@ class VirtualWorkspace:
del self.__id2buff[vbuff.id]
buffview.close()
# def detach(self, id: str):
# attached_Buffer = self.codemp.buffer_by_name(id)
# if attached_Buffer is None:
# sublime.error_message(f"You are not attached to the buffer '{id}'")
# logging.warning(f"You are not attached to the buffer '{id}'")
# return
# self.codemp.detach(id)
# def delete(self, id: str):
# self.codemp.fetch_buffers()
# existing_Buffer = self.codemp.filetree(filter=None)
# if id not in existing_Buffer:
# sublime.error_message(f"The buffer '{id}' does not exists.")
# logging.info(f"The buffer '{id}' does not exists.")
# return
# # delete a buffer that exists but you are not attached to
# attached_Buffer = self.codemp.buffer_by_name(id)
# if attached_Buffer is None:
# delete = sublime.ok_cancel_dialog(
# "Confirm you want to delete the buffer '{id}'",
# ok_title="delete",
# title="Delete Buffer?",
# )
# if delete:
# try:
# self.codemp.delete(id).wait()
# except Exception as e:
# logging.error(
# f"error when deleting the buffer '{id}':\n\n {e}", True
# )
# return
# else:
# return
# # delete buffer that you are attached to
# delete = sublime.ok_cancel_dialog(
# "Confirm you want to delete the buffer '{id}'.\n\
# You will be disconnected from it.",
# ok_title="delete",
# title="Delete Buffer?",
# )
# if delete:
# self.codemp.detach(id)
# try:
# self.codemp.delete(id).wait()
# except Exception as e:
# logging.error(f"error when deleting the buffer '{id}':\n\n {e}", True)
# return
def send_cursor(self, id: str, start: Tuple[int, int], end: Tuple[int, int]):
# we can safely ignore the promise, we don't really care if everything
# is ok for now with the cursor.