2024-09-17 16:23:19 +02:00
|
|
|
import sublime
|
|
|
|
import sublime_plugin
|
|
|
|
import logging
|
|
|
|
import random
|
|
|
|
|
2024-09-28 19:55:20 +02:00
|
|
|
import codemp
|
2024-09-17 22:20:00 +02:00
|
|
|
from ..core.session import session
|
2024-09-28 19:55:20 +02:00
|
|
|
from ..core.workspace import workspaces
|
|
|
|
from ..core.buffers import buffers
|
2024-09-17 22:20:00 +02:00
|
|
|
|
2024-09-17 16:23:19 +02:00
|
|
|
from input_handlers import SimpleTextInput
|
|
|
|
from input_handlers import SimpleListInput
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class CodempConnectCommand(sublime_plugin.WindowCommand):
|
|
|
|
def run(self, server_host, user_name, password): # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
def _():
|
|
|
|
try:
|
2024-09-28 19:55:20 +02:00
|
|
|
config = codemp.Config(
|
|
|
|
username = user_name,
|
|
|
|
password = password,
|
|
|
|
host = server_host)
|
2024-09-17 22:20:00 +02:00
|
|
|
session.connect(config)
|
2024-09-17 16:23:19 +02:00
|
|
|
except Exception as e:
|
|
|
|
sublime.error_message(
|
|
|
|
"Could not connect:\n Make sure the server is up\n\
|
|
|
|
and your credentials are correct."
|
|
|
|
)
|
|
|
|
sublime.set_timeout_async(_)
|
|
|
|
|
|
|
|
def input_description(self):
|
|
|
|
return "Server host:"
|
|
|
|
|
|
|
|
def input(self, args):
|
|
|
|
if "server_host" not in args:
|
|
|
|
return SimpleTextInput(
|
|
|
|
("server_host", "http://code.mp:50053"),
|
|
|
|
("user_name", f"user-{random.random()}"),
|
|
|
|
("password", "password?"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if "user_name" not in args:
|
|
|
|
return SimpleTextInput(
|
|
|
|
("user_name", f"user-{random.random()}"),
|
|
|
|
("password", "password?"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if "password" not in args:
|
|
|
|
return SimpleTextInput(
|
|
|
|
("password", "password?"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Disconnect Command
|
|
|
|
class CodempDisconnectCommand(sublime_plugin.WindowCommand):
|
|
|
|
def is_enabled(self):
|
2024-09-17 22:20:00 +02:00
|
|
|
return session.is_active()
|
2024-09-17 16:23:19 +02:00
|
|
|
|
|
|
|
def run(self):
|
2024-09-18 16:57:26 +02:00
|
|
|
cli = session.client
|
|
|
|
assert cli is not None
|
|
|
|
|
2024-09-17 22:20:00 +02:00
|
|
|
for ws in workspaces.lookup():
|
2024-09-18 16:57:26 +02:00
|
|
|
if cli.leave_workspace(ws.id):
|
|
|
|
workspaces.remove(ws)
|
2024-09-17 22:20:00 +02:00
|
|
|
|
2024-09-18 16:57:26 +02:00
|
|
|
session.drop_client()
|
2024-09-17 16:23:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Join Workspace Command
|
|
|
|
class CodempJoinWorkspaceCommand(sublime_plugin.WindowCommand):
|
|
|
|
def is_enabled(self) -> bool:
|
2024-09-28 19:55:20 +02:00
|
|
|
return session.is_active()
|
2024-09-17 16:23:19 +02:00
|
|
|
|
|
|
|
def run(self, workspace_id): # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
if workspace_id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.info(f"Joining workspace: '{workspace_id}'...")
|
2024-09-28 19:55:20 +02:00
|
|
|
promise = session.client.join_workspace(workspace_id)
|
2024-09-17 16:23:19 +02:00
|
|
|
active_window = sublime.active_window()
|
|
|
|
|
2024-09-17 17:20:00 +02:00
|
|
|
def _():
|
2024-09-17 16:23:19 +02:00
|
|
|
try:
|
|
|
|
workspace = promise.wait()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
|
|
|
f"Could not join workspace '{workspace_id}'.\n\nerror: {e}"
|
|
|
|
)
|
|
|
|
sublime.error_message(f"Could not join workspace '{workspace_id}'")
|
|
|
|
return
|
2024-09-28 19:55:20 +02:00
|
|
|
workspaces.add(workspace)
|
2024-09-17 17:20:00 +02:00
|
|
|
sublime.set_timeout_async(_)
|
2024-09-17 16:23:19 +02:00
|
|
|
|
|
|
|
def input_description(self):
|
|
|
|
return "Join:"
|
|
|
|
|
|
|
|
def input(self, args):
|
|
|
|
if "workspace_id" not in args:
|
2024-09-28 19:55:20 +02:00
|
|
|
list = session.client.list_workspaces(True, True)
|
2024-09-17 16:23:19 +02:00
|
|
|
return SimpleListInput(
|
|
|
|
("workspace_id", list.wait()),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Leave Workspace Command
|
|
|
|
class CodempLeaveWorkspaceCommand(sublime_plugin.WindowCommand):
|
|
|
|
def is_enabled(self):
|
2024-09-17 22:20:00 +02:00
|
|
|
return client.codemp is not None and \
|
|
|
|
len(client.all_workspaces(self.window)) > 0
|
2024-09-17 16:23:19 +02:00
|
|
|
|
|
|
|
def run(self, workspace_id: str): # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
assert client.codemp is not None
|
|
|
|
if client.codemp.leave_workspace(workspace_id):
|
|
|
|
vws = client.workspace_from_id(workspace_id)
|
|
|
|
if vws is not None:
|
|
|
|
client.uninstall_workspace(vws)
|
|
|
|
else:
|
|
|
|
logger.error(f"could not leave the workspace '{workspace_id}'")
|
|
|
|
|
|
|
|
def input(self, args):
|
|
|
|
if "workspace_id" not in args:
|
|
|
|
return ActiveWorkspacesIdList()
|
|
|
|
|
|
|
|
|
|
|
|
class CodempInviteToWorkspaceCommand(sublime_plugin.WindowCommand):
|
|
|
|
def is_enabled(self) -> bool:
|
|
|
|
return client.codemp is not None and len(client.all_workspaces(self.window)) > 0
|
|
|
|
|
|
|
|
def run(self, workspace_id: str, user: str): # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
assert client.codemp is not None
|
|
|
|
client.codemp.invite_to_workspace(workspace_id, user)
|
|
|
|
logger.debug(f"invite sent to user {user} for workspace {workspace_id}.")
|
|
|
|
|
|
|
|
def input(self, args):
|
|
|
|
assert client.codemp is not None
|
|
|
|
if "workspace_id" not in args:
|
|
|
|
wslist = client.codemp.list_workspaces(True, False)
|
|
|
|
return SimpleListInput(
|
|
|
|
("workspace_id", wslist.wait()), ("user", "invitee's username")
|
|
|
|
)
|
|
|
|
|
|
|
|
if "user" not in args:
|
|
|
|
return SimpleTextInput(("user", "invitee's username"))
|
|
|
|
|
|
|
|
|
|
|
|
class CodempCreateWorkspaceCommand(sublime_plugin.WindowCommand):
|
|
|
|
def is_enabled(self):
|
|
|
|
return client.codemp is not None
|
|
|
|
|
|
|
|
def run(self, workspace_id: str): # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
assert client.codemp is not None
|
|
|
|
client.codemp.create_workspace(workspace_id)
|
|
|
|
|
|
|
|
def input(self, args):
|
|
|
|
if "workspace_id" not in args:
|
|
|
|
return SimpleTextInput(("workspace_id", "new workspace"))
|
|
|
|
|
|
|
|
|
|
|
|
class CodempDeleteWorkspaceCommand(sublime_plugin.WindowCommand):
|
|
|
|
def is_enabled(self):
|
|
|
|
return client.codemp is not None
|
|
|
|
|
|
|
|
def run(self, workspace_id: str): # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
assert client.codemp is not None
|
|
|
|
|
|
|
|
vws = client.workspace_from_id(workspace_id)
|
|
|
|
if vws is not None:
|
|
|
|
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
|
|
|
|
if not client.codemp.leave_workspace(workspace_id):
|
|
|
|
logger.debug("error while leaving the workspace:")
|
2024-09-17 22:20:00 +02:00
|
|
|
raise RuntimeError("error while leaving the workspace")
|
|
|
|
|
2024-09-17 16:23:19 +02:00
|
|
|
client.uninstall_workspace(vws)
|
|
|
|
|
|
|
|
client.codemp.delete_workspace(workspace_id)
|
|
|
|
|
|
|
|
def input(self, args):
|
|
|
|
assert client.codemp is not None
|
|
|
|
workspaces = client.codemp.list_workspaces(True, False) # noqa: F841
|
|
|
|
if "workspace_id" not in args:
|
|
|
|
return SimpleListInput(("workspace_id", workspaces.wait()))
|