2024-08-06 18:46:22 +02:00
|
|
|
local buffers = require('codemp.buffers')
|
2024-08-06 02:21:30 +02:00
|
|
|
local workspace = require('codemp.workspace')
|
2024-08-15 01:22:48 +02:00
|
|
|
local utils = require('codemp.utils')
|
2024-08-24 01:57:00 +02:00
|
|
|
local client = require("codemp.client")
|
2024-08-06 02:21:30 +02:00
|
|
|
|
2024-09-16 20:49:02 +02:00
|
|
|
local function filter(needle, haystack, getter)
|
2024-08-06 02:21:30 +02:00
|
|
|
local hints = {}
|
|
|
|
for _, opt in pairs(haystack) do
|
2024-09-16 20:49:02 +02:00
|
|
|
local hay = opt
|
|
|
|
if getter ~= nil then
|
|
|
|
hay = getter(opt)
|
|
|
|
end
|
|
|
|
if vim.startswith(hay, needle) then
|
2024-09-16 20:50:28 +02:00
|
|
|
table.insert(hints, hay)
|
2024-08-06 02:21:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return hints
|
|
|
|
end
|
|
|
|
|
2024-08-08 04:34:23 +02:00
|
|
|
-- always available
|
|
|
|
local base_actions = {
|
2024-09-05 05:28:48 +02:00
|
|
|
toggle = function()
|
2024-09-14 14:24:48 +02:00
|
|
|
require('codemp.window').toggle()
|
2024-09-05 05:28:48 +02:00
|
|
|
end,
|
|
|
|
|
2024-09-16 20:46:10 +02:00
|
|
|
connect = function()
|
|
|
|
client.connect()
|
2024-08-08 04:34:23 +02:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- only available if state.client is not nil
|
|
|
|
local connected_actions = {
|
|
|
|
id = function()
|
2024-10-26 18:15:30 +02:00
|
|
|
print("> codemp::" .. CODEMP.client:current_user().id)
|
2024-08-15 04:07:22 +02:00
|
|
|
end,
|
|
|
|
|
2024-08-08 04:34:23 +02:00
|
|
|
join = function(ws)
|
2024-09-15 12:39:12 +02:00
|
|
|
if ws == nil then
|
|
|
|
local opts = { prompt = "Select workspace to join:", format_item = function (x) return x.name end }
|
2024-09-17 17:26:23 +02:00
|
|
|
return vim.ui.select(CODEMP.available, opts, function (choice)
|
2024-09-15 12:39:12 +02:00
|
|
|
if choice == nil then return end -- action canceled by user
|
2024-09-17 17:26:23 +02:00
|
|
|
workspace.join(CODEMP.available[choice].name)
|
2024-09-15 12:39:12 +02:00
|
|
|
end)
|
|
|
|
else
|
|
|
|
workspace.join(ws)
|
|
|
|
end
|
2024-08-08 04:34:23 +02:00
|
|
|
end,
|
2024-08-08 04:48:26 +02:00
|
|
|
|
2024-08-22 03:41:46 +02:00
|
|
|
start = function(ws)
|
|
|
|
if ws == nil then error("missing workspace name") end
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.client:create_workspace(ws):and_then(function ()
|
2024-09-17 16:33:22 +02:00
|
|
|
print(" <> created workspace " .. ws)
|
|
|
|
workspace.list()
|
|
|
|
end)
|
2024-08-22 03:41:46 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
available = function()
|
2024-09-26 16:21:26 +02:00
|
|
|
CODEMP.available = {}
|
2024-10-26 18:15:30 +02:00
|
|
|
for _, ws in ipairs(CODEMP.client:fetch_owned_workspaces():await()) do
|
2024-08-22 03:41:46 +02:00
|
|
|
print(" ++ " .. ws)
|
2024-09-26 16:21:26 +02:00
|
|
|
table.insert(CODEMP.available, ws)
|
2024-08-22 03:41:46 +02:00
|
|
|
end
|
2024-10-26 18:15:30 +02:00
|
|
|
for _, ws in ipairs(CODEMP.client:fetch_joined_workspaces():await()) do
|
2024-08-22 03:41:46 +02:00
|
|
|
print(" -- " .. ws)
|
2024-09-26 16:21:26 +02:00
|
|
|
table.insert(CODEMP.available, ws)
|
2024-08-22 03:41:46 +02:00
|
|
|
end
|
2024-09-26 16:21:26 +02:00
|
|
|
require('codemp.window').update()
|
2024-08-22 03:41:46 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
invite = function(user)
|
|
|
|
local ws
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.workspace ~= nil then
|
2024-10-26 18:15:30 +02:00
|
|
|
ws = CODEMP.workspace:id()
|
2024-08-22 03:41:46 +02:00
|
|
|
else
|
|
|
|
ws = vim.fn.input("workspace > ", "")
|
|
|
|
end
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.client:invite_to_workspace(ws, user):and_then(function ()
|
2024-09-17 16:33:22 +02:00
|
|
|
print(" :: invited " .. user .. " to workspace " .. ws)
|
|
|
|
end)
|
2024-08-22 03:41:46 +02:00
|
|
|
end,
|
|
|
|
|
2024-08-08 04:48:26 +02:00
|
|
|
disconnect = function()
|
2024-10-12 23:24:33 +02:00
|
|
|
if CODEMP.workspace ~= nil then
|
2024-10-26 18:15:30 +02:00
|
|
|
print(" xx leaving workspace " .. CODEMP.workspace:id())
|
2024-10-12 23:24:33 +02:00
|
|
|
workspace.leave()
|
|
|
|
end
|
2024-10-26 18:15:30 +02:00
|
|
|
print(" xx disconnecting client " .. CODEMP.client:current_user().id)
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.client = nil -- should drop and thus close everything
|
2024-09-27 20:52:44 +02:00
|
|
|
collectgarbage("collect") -- make sure we drop
|
2024-10-12 23:27:08 +02:00
|
|
|
require('codemp.window').update()
|
2024-08-08 04:48:26 +02:00
|
|
|
end,
|
2024-08-08 04:34:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
-- only available if state.workspace is not nil
|
|
|
|
local joined_actions = {
|
|
|
|
create = function(path)
|
|
|
|
if path == nil then error("missing buffer name") end
|
|
|
|
buffers.create(path)
|
|
|
|
end,
|
|
|
|
|
2024-09-06 17:06:12 +02:00
|
|
|
share = function(path, bang)
|
2024-08-15 01:22:48 +02:00
|
|
|
if path == nil then
|
|
|
|
local cwd = vim.fn.getcwd()
|
|
|
|
local full_path = vim.fn.expand("%:p")
|
2024-09-17 01:07:23 +02:00
|
|
|
path = string.gsub(full_path, cwd .. utils.sep(), "")
|
|
|
|
path = string.gsub(path, '\\', '/')
|
2024-08-15 01:22:48 +02:00
|
|
|
end
|
|
|
|
if #path > 0 then
|
|
|
|
local buf = vim.api.nvim_get_current_buf()
|
2024-09-06 17:06:12 +02:00
|
|
|
if not bang then buffers.create(path) end
|
2024-08-15 01:22:48 +02:00
|
|
|
local content = utils.buffer.get_content(buf)
|
2024-09-27 23:46:35 +02:00
|
|
|
buffers.attach(path, { buffer = buf, content = content, skip_exists_check = true })
|
2024-09-14 14:24:48 +02:00
|
|
|
require('codemp.window').update() -- TODO would be nice to do automatically inside
|
2024-08-15 01:22:48 +02:00
|
|
|
else
|
|
|
|
print(" !! empty path or open a file")
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2024-08-08 04:48:26 +02:00
|
|
|
delete = function(path)
|
|
|
|
if path == nil then error("missing buffer name") end
|
2024-10-26 18:15:30 +02:00
|
|
|
CODEMP.workspace:delete_buffer(path):and_then(function()
|
2024-09-17 16:33:22 +02:00
|
|
|
print(" xx deleted buffer " .. path)
|
|
|
|
end)
|
2024-08-08 04:48:26 +02:00
|
|
|
end,
|
|
|
|
|
2024-08-08 04:34:23 +02:00
|
|
|
buffers = function()
|
2024-10-26 18:15:30 +02:00
|
|
|
for _, buf in ipairs(CODEMP.workspace:search_buffers()) do
|
2024-09-16 20:46:10 +02:00
|
|
|
if buffers.map_rev[buf] ~= nil then
|
|
|
|
print(" +- " .. buf)
|
|
|
|
else
|
|
|
|
print(" -- " .. buf)
|
|
|
|
end
|
|
|
|
end
|
2024-08-08 04:34:23 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
sync = function()
|
|
|
|
buffers.sync()
|
|
|
|
end,
|
|
|
|
|
|
|
|
attach = function(path, bang)
|
2024-09-15 12:39:12 +02:00
|
|
|
local function doit(p)
|
|
|
|
local buffer = nil
|
|
|
|
if bang then
|
|
|
|
buffer = vim.api.nvim_get_current_buf()
|
|
|
|
else
|
|
|
|
buffer = vim.api.nvim_create_buf(true, false)
|
|
|
|
vim.api.nvim_set_current_buf(buffer)
|
|
|
|
end
|
2024-09-26 03:29:13 +02:00
|
|
|
buffers.attach(p, { buffer = buffer })
|
2024-09-15 12:39:12 +02:00
|
|
|
end
|
|
|
|
if path == nil then
|
2024-10-26 18:15:30 +02:00
|
|
|
local filetree = CODEMP.workspace:search_buffers()
|
2024-09-15 12:39:12 +02:00
|
|
|
return vim.ui.select(filetree, { prompt = "Select buffer to attach to:" }, function (choice)
|
|
|
|
if choice == nil then return end -- action canceled by user
|
|
|
|
doit(filetree[choice])
|
|
|
|
end)
|
2024-09-06 19:49:52 +02:00
|
|
|
else
|
2024-09-15 12:39:12 +02:00
|
|
|
doit(path)
|
2024-09-06 19:49:52 +02:00
|
|
|
end
|
2024-08-08 04:34:23 +02:00
|
|
|
end,
|
|
|
|
|
2024-08-08 04:48:26 +02:00
|
|
|
detach = function(path)
|
2024-09-26 03:00:04 +02:00
|
|
|
if path == nil then
|
|
|
|
local bufid = vim.api.nvim_get_current_buf()
|
|
|
|
path = buffers.map[bufid]
|
|
|
|
if path == nil then error("missing buffer name") end
|
|
|
|
end
|
2024-08-08 04:48:26 +02:00
|
|
|
buffers.detach(path)
|
2024-09-14 14:24:48 +02:00
|
|
|
require('codemp.window').update() -- TODO would be nice to do automatically inside
|
2024-08-08 04:48:26 +02:00
|
|
|
end,
|
|
|
|
|
2024-09-26 03:00:04 +02:00
|
|
|
leave = function()
|
2024-08-24 01:56:27 +02:00
|
|
|
workspace.leave()
|
2024-08-08 04:48:26 +02:00
|
|
|
end,
|
2024-08-08 04:34:23 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 02:21:30 +02:00
|
|
|
vim.api.nvim_create_user_command(
|
|
|
|
"MP",
|
|
|
|
function (args)
|
2024-08-08 04:34:23 +02:00
|
|
|
local action = args.fargs[1]
|
|
|
|
local fn = nil
|
|
|
|
|
|
|
|
if base_actions[action] ~= nil then
|
|
|
|
fn = base_actions[action]
|
2024-08-06 02:21:30 +02:00
|
|
|
end
|
2024-08-08 04:34:23 +02:00
|
|
|
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.client ~= nil and connected_actions[action] ~= nil then
|
2024-08-08 04:34:23 +02:00
|
|
|
fn = connected_actions[action]
|
|
|
|
end
|
|
|
|
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.workspace ~= nil and joined_actions[action] ~= nil then
|
2024-08-08 04:34:23 +02:00
|
|
|
fn = joined_actions[action]
|
|
|
|
end
|
|
|
|
|
|
|
|
if fn ~= nil then
|
|
|
|
fn(args.fargs[2], args.bang)
|
|
|
|
else
|
|
|
|
print(" ?? invalid command")
|
2024-08-06 02:21:30 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
{
|
|
|
|
bang = true,
|
|
|
|
desc = "codeMP main command",
|
|
|
|
nargs = "+",
|
|
|
|
complete = function (lead, cmd, _pos)
|
|
|
|
local args = vim.split(cmd, " ", { plain = true, trimempty = false })
|
|
|
|
local stage = #args
|
|
|
|
if stage == 1 then
|
|
|
|
return { "MP" }
|
|
|
|
elseif stage == 2 then
|
2024-08-08 04:34:23 +02:00
|
|
|
local suggestions = {}
|
|
|
|
local n = 0
|
|
|
|
for sugg, _ in pairs(base_actions) do
|
|
|
|
n = n + 1
|
|
|
|
suggestions[n] = sugg
|
|
|
|
end
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.client ~= nil then
|
2024-08-08 04:34:23 +02:00
|
|
|
for sugg, _ in pairs(connected_actions) do
|
|
|
|
n = n + 1
|
|
|
|
suggestions[n] = sugg
|
|
|
|
end
|
|
|
|
end
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.workspace ~= nil then
|
2024-08-08 04:34:23 +02:00
|
|
|
for sugg, _ in pairs(joined_actions) do
|
|
|
|
n = n + 1
|
|
|
|
suggestions[n] = sugg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return filter(lead, suggestions)
|
2024-08-06 02:21:30 +02:00
|
|
|
elseif stage == 3 then
|
2024-09-26 03:00:04 +02:00
|
|
|
local last_arg = args[#args-1]
|
|
|
|
if last_arg == 'attach' or last_arg == 'detach' then
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.client ~= nil and CODEMP.workspace ~= nil then
|
2024-09-26 03:00:04 +02:00
|
|
|
local choices
|
|
|
|
if last_arg == "attach" then
|
2024-10-26 18:15:30 +02:00
|
|
|
choices = CODEMP.workspace:search_buffers()
|
2024-09-26 03:00:04 +02:00
|
|
|
elseif last_arg == "detach" then
|
|
|
|
choices = CODEMP.workspace.active_buffers
|
|
|
|
end
|
|
|
|
return filter(lead, choices)
|
2024-08-06 02:21:30 +02:00
|
|
|
end
|
2024-09-16 20:46:10 +02:00
|
|
|
elseif args[#args-1] == 'join' then
|
2024-09-17 17:26:23 +02:00
|
|
|
return filter(lead, CODEMP.available, function(ws) return ws.name end)
|
2024-08-06 02:21:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
)
|