2024-08-24 01:58:03 +02:00
|
|
|
local cc = require("neo-tree.sources.common.commands")
|
|
|
|
local utils = require("neo-tree.utils")
|
2024-09-15 12:43:59 +02:00
|
|
|
local codemp_utils = require("codemp.utils")
|
2024-08-24 01:58:03 +02:00
|
|
|
local manager = require("neo-tree.sources.manager")
|
|
|
|
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 = {}
|
|
|
|
|
2024-09-07 05:03:34 +02:00
|
|
|
local function toggle(node)
|
|
|
|
if node:is_expanded() then
|
|
|
|
node:collapse()
|
|
|
|
else
|
|
|
|
node:expand()
|
|
|
|
end
|
2024-09-07 05:10:58 +02:00
|
|
|
manager.refresh("codemp")
|
2024-09-07 05:03:34 +02:00
|
|
|
end
|
|
|
|
|
2024-08-24 01:58:03 +02:00
|
|
|
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
|
2024-09-07 05:03:34 +02:00
|
|
|
if selected.type == "entry" then return end
|
2024-09-07 05:07:06 +02:00
|
|
|
if selected.type == "root" then return toggle(selected) end
|
2024-09-26 16:21:14 +02:00
|
|
|
if selected.type == "title" then
|
|
|
|
if CODEMP.client ~= nil then
|
|
|
|
print(" :: refreshing workspace list")
|
|
|
|
ws_manager.list()
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2024-09-07 04:26:21 +02:00
|
|
|
if selected.type == "button" then
|
2024-09-17 17:26:23 +02:00
|
|
|
if selected.name == "[connect]" and CODEMP.client == nil then
|
2024-08-24 01:58:03 +02:00
|
|
|
client_manager.connect()
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if selected.type == "workspace" then
|
2024-10-26 18:15:30 +02:00
|
|
|
if CODEMP.workspace ~= nil and CODEMP.workspace:id() ~= selected.name then
|
2024-09-07 05:03:34 +02:00
|
|
|
error("must leave current workspace first")
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.workspace == nil then
|
2024-09-07 05:03:34 +02:00
|
|
|
ws_manager.join(selected.name)
|
|
|
|
end
|
2024-08-24 01:58:03 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if selected.type == "buffer" then
|
|
|
|
local window = utils.get_appropriate_window(state)
|
2024-09-06 20:01:15 +02:00
|
|
|
if buf_manager.map_rev[selected.name] ~= nil then
|
2024-09-26 03:29:13 +02:00
|
|
|
vim.api.nvim_set_current_win(window)
|
2024-09-06 20:01:15 +02:00
|
|
|
vim.api.nvim_win_set_buf(window, buf_manager.map_rev[selected.name])
|
2024-09-26 03:29:13 +02:00
|
|
|
else
|
|
|
|
local buf = vim.api.nvim_create_buf(true, false)
|
|
|
|
buf_manager.attach(selected.name, { buffer = buf, window = window })
|
2024-09-06 20:01:15 +02:00
|
|
|
end
|
2024-08-24 01:58:03 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if selected.type == "user" then
|
2024-09-25 04:00:46 +02:00
|
|
|
if CODEMP.workspace == nil then error("cannot follow while not in a workspace") end
|
2024-09-16 20:10:54 +02:00
|
|
|
local usr = ws_manager.map[selected.name]
|
2024-09-25 04:00:46 +02:00
|
|
|
print(" /\\/ following " .. selected.name)
|
|
|
|
CODEMP.following = selected.name
|
2024-10-26 18:15:30 +02:00
|
|
|
local _ = CODEMP.workspace:cursor():send({
|
2024-09-25 04:00:46 +02:00
|
|
|
buffer = "",
|
2024-10-26 18:15:30 +02:00
|
|
|
start_row = 0,
|
|
|
|
start_col = 0,
|
|
|
|
end_row = 0,
|
|
|
|
end_col = 0,
|
2024-09-25 04:00:46 +02:00
|
|
|
}) -- clear current cursor
|
2024-09-16 20:10:54 +02:00
|
|
|
if usr ~= nil then
|
|
|
|
local buf_name = buf_manager.users[selected.name]
|
|
|
|
local buf_id = buf_manager.map_rev[buf_name]
|
|
|
|
if buf_id ~= nil then
|
2024-09-16 20:26:20 +02:00
|
|
|
local win = utils.get_appropriate_window(state)
|
2024-09-25 04:00:46 +02:00
|
|
|
CODEMP.ignore_following_action = true
|
2024-09-16 20:26:20 +02:00
|
|
|
vim.api.nvim_set_current_win(win)
|
2024-09-16 20:10:54 +02:00
|
|
|
vim.api.nvim_win_set_buf(win, buf_id)
|
|
|
|
vim.api.nvim_win_set_cursor(win, { usr.pos[1] + 1, usr.pos[2] })
|
|
|
|
end
|
|
|
|
end
|
2024-09-25 04:10:48 +02:00
|
|
|
manager.refresh("codemp")
|
2024-08-24 01:58:03 +02:00
|
|
|
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
|
2024-09-15 12:43:59 +02:00
|
|
|
return vim.ui.input({ prompt = "move content into open buffer?" }, function (input)
|
2024-09-26 16:38:52 +02:00
|
|
|
if not input or not vim.startswith(string.lower(input), "y") then return end
|
2024-09-15 12:43:59 +02:00
|
|
|
local window = utils.get_appropriate_window(state)
|
|
|
|
local buf = vim.api.nvim_win_get_buf(window)
|
2024-09-27 23:46:35 +02:00
|
|
|
buf_manager.attach(selected.name, { buffer = buf, skip_exists_check = true })
|
2024-09-15 12:43:59 +02:00
|
|
|
end)
|
2024-09-06 19:49:52 +02:00
|
|
|
end
|
|
|
|
error("only buffers can be moved to current file")
|
|
|
|
end
|
|
|
|
|
2024-09-15 12:43:59 +02:00
|
|
|
M.copy = function(state, path, extra)
|
|
|
|
local selected = state.tree:get_node()
|
|
|
|
if selected.type == "buffer" then
|
|
|
|
return vim.ui.input({ prompt = "copy content to remote buffer?" }, function (input)
|
2024-09-26 16:38:52 +02:00
|
|
|
if not input or not vim.startswith(string.lower(input), "y") then return end
|
2024-09-15 12:43:59 +02:00
|
|
|
local window = utils.get_appropriate_window(state)
|
|
|
|
local buf = vim.api.nvim_win_get_buf(window)
|
|
|
|
local content = codemp_utils.buffer.get_content(buf)
|
2024-09-27 23:46:35 +02:00
|
|
|
buf_manager.attach(selected.name, { buffer = buf, content = content, skip_exists_check = true })
|
2024-09-15 12:43:59 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
error("current file can only be copied into buffers")
|
|
|
|
end
|
|
|
|
|
2024-09-07 01:31:04 +02:00
|
|
|
M.delete = function(state, path, extra)
|
|
|
|
local selected = state.tree:get_node()
|
2024-09-26 16:38:52 +02:00
|
|
|
if selected.type == "title" then
|
|
|
|
vim.ui.input({ prompt = "disconnect from server?" }, function(choice)
|
|
|
|
if not choice or not vim.startswith(string.lower(choice), "y") then return end
|
2024-10-12 23:24:33 +02:00
|
|
|
if CODEMP.workspace ~= nil then ws_manager.leave() end
|
2024-09-26 16:38:52 +02:00
|
|
|
CODEMP.client = nil
|
2024-10-12 23:27:08 +02:00
|
|
|
manager.refresh("codemp")
|
2024-09-26 16:38:52 +02:00
|
|
|
collectgarbage("collect") -- to make sure we drop the reference and disconnect
|
|
|
|
end)
|
|
|
|
elseif selected.type == "root" and vim.startswith(selected.name, "#") then
|
2024-09-07 05:03:34 +02:00
|
|
|
vim.ui.input({ prompt = "disconnect from workspace?" }, function (input)
|
2024-09-26 16:38:52 +02:00
|
|
|
if not input or not vim.startswith(string.lower(input), "y") then return end
|
2024-09-07 05:03:34 +02:00
|
|
|
ws_manager.leave()
|
|
|
|
end)
|
|
|
|
elseif selected.type == "buffer" then
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.workspace == nil then error("join a workspace first") end
|
2024-09-15 12:10:19 +02:00
|
|
|
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
|
2024-09-26 16:38:52 +02:00
|
|
|
if not input or not vim.startswith(string.lower(input), "y") then return end
|
|
|
|
if buf_manager.map_rev[selected.name] ~= nil then
|
|
|
|
buf_manager.detach(selected.name)
|
|
|
|
end
|
2024-10-26 18:15:30 +02:00
|
|
|
CODEMP.workspace:delete_buffer(selected.name):and_then(function ()
|
2024-09-17 16:33:22 +02:00
|
|
|
print("deleted buffer " .. selected.name)
|
|
|
|
manager.refresh("codemp")
|
|
|
|
end)
|
2024-09-15 12:10:19 +02:00
|
|
|
end)
|
2024-09-07 01:31:04 +02:00
|
|
|
elseif selected.type == "workspace" then
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.client == nil then error("connect to server first") end
|
2024-09-15 12:10:19 +02:00
|
|
|
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
|
2024-09-26 16:38:52 +02:00
|
|
|
if not input or not vim.startswith(string.lower(input), "y") then return end
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.client:delete_workspace(selected.name):and_then(function ()
|
2024-09-17 16:33:22 +02:00
|
|
|
print("deleted workspace " .. selected.name)
|
|
|
|
manager.refresh("codemp")
|
|
|
|
end)
|
2024-09-15 12:10:19 +02:00
|
|
|
end)
|
2024-09-07 01:31:04 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-07 05:03:34 +02:00
|
|
|
M.add = function(state, path, extra)
|
|
|
|
local selected = state.tree:get_node()
|
|
|
|
if selected.type == "root" then
|
|
|
|
if vim.startswith(selected.name, "#") then
|
2024-09-15 12:46:43 +02:00
|
|
|
vim.ui.input({ prompt = "new buffer path" }, function(input)
|
2024-09-07 05:03:34 +02:00
|
|
|
if input == nil or input == "" then return end
|
2024-10-26 18:15:30 +02:00
|
|
|
CODEMP.workspace:create_buffer(input):and_then(function ()
|
2024-09-17 16:33:22 +02:00
|
|
|
manager.refresh("codemp")
|
|
|
|
end)
|
2024-09-07 05:03:34 +02:00
|
|
|
end)
|
|
|
|
elseif selected.name == "workspaces" then
|
2024-09-15 12:46:43 +02:00
|
|
|
vim.ui.input({ prompt = "new workspace name" }, function(input)
|
2024-09-07 05:03:34 +02:00
|
|
|
if input == nil or input == "" then return end
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.client:create_workspace(input):and_then(function ()
|
2024-09-17 18:43:45 +02:00
|
|
|
manager.refresh("codemp")
|
2024-09-17 16:33:22 +02:00
|
|
|
require('codemp.workspace').list()
|
|
|
|
end)
|
2024-09-07 05:03:34 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
elseif selected.type == "workspace" then
|
2024-09-15 12:46:43 +02:00
|
|
|
vim.ui.input({ prompt = "user name to invite" }, function(input)
|
2024-09-06 19:11:18 +02:00
|
|
|
if input == nil or input == "" then return end
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.client:invite_to_workspace(selected.name, input):and_then(function ()
|
2024-09-17 16:33:22 +02:00
|
|
|
print("invited user " .. input .. " to workspace " .. selected.name)
|
|
|
|
end)
|
2024-09-06 19:11:18 +02:00
|
|
|
end)
|
2024-09-26 03:42:06 +02:00
|
|
|
elseif selected.type == "buffer" then
|
|
|
|
if buf_manager.map_rev[selected.name] ~= nil then
|
|
|
|
vim.ui.input({ prompt = "detach from '" .. selected.name .. "'?" }, function (choice)
|
2024-09-26 16:38:52 +02:00
|
|
|
if not choice or not vim.startswith(string.lower(choice), "y") then return end
|
2024-09-26 03:44:42 +02:00
|
|
|
buf_manager.detach(selected.name)
|
2024-09-26 03:42:06 +02:00
|
|
|
end)
|
|
|
|
end
|
2024-09-06 19:11:18 +02:00
|
|
|
end
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
cc._add_common_commands(M)
|
|
|
|
return M
|