2024-08-24 01:58:03 +02:00
|
|
|
local cc = require("neo-tree.sources.common.commands")
|
|
|
|
local utils = require("neo-tree.utils")
|
|
|
|
local manager = require("neo-tree.sources.manager")
|
|
|
|
local session = require("codemp.session")
|
|
|
|
local buf_manager = require("codemp.buffers")
|
2024-09-05 05:48:31 +02:00
|
|
|
local ws_manager = require("codemp.workspace")
|
2024-08-24 01:58:03 +02:00
|
|
|
local client_manager = require("codemp.client")
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.refresh = require("neo-tree.utils").wrap(manager.refresh, "codemp")
|
|
|
|
|
|
|
|
M.open = function(state, path, extra)
|
|
|
|
local selected = state.tree:get_node()
|
|
|
|
if selected.type == "spacer" then return end
|
|
|
|
if selected.type == "root" then
|
|
|
|
if session.client ~= nil then
|
|
|
|
print(" +-+ connected to codemp as " .. session.client.username)
|
|
|
|
else
|
|
|
|
client_manager.connect()
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if selected.type == "workspace" then
|
|
|
|
if selected:is_expanded() then
|
|
|
|
vim.ui.input({ prompt = "disconnect from workspace?" }, function (input)
|
|
|
|
if input == nil then return end
|
|
|
|
if input ~= "y" then return end
|
|
|
|
ws_manager.leave()
|
|
|
|
selected:collapse()
|
|
|
|
manager.refresh("codemp")
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
if session.workspace ~= nil and session.workspace.name ~= selected.name then
|
|
|
|
error("must leave current workspace first")
|
|
|
|
end
|
|
|
|
if session.workspace == nil then
|
|
|
|
ws_manager.join(selected.name)
|
|
|
|
end
|
|
|
|
selected:expand()
|
|
|
|
manager.refresh("codemp")
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if selected.type == "buffer" then
|
|
|
|
local window = utils.get_appropriate_window(state)
|
|
|
|
vim.api.nvim_set_current_win(window)
|
2024-09-06 20:01:15 +02:00
|
|
|
if buf_manager.map_rev[selected.name] ~= nil then
|
|
|
|
vim.api.nvim_win_set_buf(window, buf_manager.map_rev[selected.name])
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local buf = vim.api.nvim_create_buf(true, false)
|
2024-09-06 19:49:52 +02:00
|
|
|
vim.api.nvim_win_set_buf(window, buf)
|
2024-09-06 19:11:34 +02:00
|
|
|
buf_manager.attach(selected.name, buf)
|
2024-08-24 01:58:03 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if selected.type == "user" then
|
|
|
|
print("another remote user")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
error("unrecognized node type")
|
|
|
|
end
|
|
|
|
|
2024-09-06 19:49:52 +02:00
|
|
|
M.move = function(state, path, extra)
|
|
|
|
local selected = state.tree:get_node()
|
|
|
|
if selected.type == "buffer" then
|
|
|
|
local window = utils.get_appropriate_window(state)
|
|
|
|
local buf = vim.api.nvim_win_get_buf(window)
|
|
|
|
buf_manager.attach(selected.name, buf)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
error("only buffers can be moved to current file")
|
|
|
|
end
|
|
|
|
|
2024-09-07 01:31:04 +02:00
|
|
|
M.delete = function(state, path, extra)
|
|
|
|
local selected = state.tree:get_node()
|
|
|
|
if selected.type == "buffer" then
|
|
|
|
if session.workspace == nil then error("join a workspace first") end
|
|
|
|
session.workspace:delete_buffer(selected.name):await()
|
|
|
|
print("deleted buffer " .. selected.name)
|
|
|
|
manager.refresh("codemp")
|
|
|
|
elseif selected.type == "workspace" then
|
|
|
|
if session.client == nil then error("connect to server first") end
|
|
|
|
session.client:delete_workspace(selected.name):await()
|
|
|
|
print("deleted workspace " .. selected.name)
|
|
|
|
manager.refresh("codemp")
|
|
|
|
else
|
|
|
|
print("/!\\ can only delete buffers and workspaces")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-24 01:58:03 +02:00
|
|
|
M.add = function(_state)
|
2024-09-06 19:11:18 +02:00
|
|
|
if session.client == nil then
|
|
|
|
vim.ui.input({ prompt = "server address" }, function(input)
|
|
|
|
if input == nil or input == "" then return end
|
|
|
|
client_manager.connect(input)
|
|
|
|
end)
|
|
|
|
elseif session.workspace == nil then
|
|
|
|
vim.ui.input({ prompt = "workspace name" }, function(input)
|
|
|
|
if input == nil or input == "" then return end
|
|
|
|
session.client:create_workspace(input):await()
|
|
|
|
manager.refresh("codemp")
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
vim.ui.input({ prompt = "buffer path" }, function(input)
|
|
|
|
if input == nil or input == "" then return end
|
|
|
|
session.workspace:create_buffer(input):await()
|
|
|
|
manager.refresh("codemp")
|
|
|
|
end)
|
|
|
|
end
|
2024-09-07 01:31:04 +02:00
|
|
|
manager.refresh("codemp")
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
cc._add_common_commands(M)
|
|
|
|
return M
|