codemp-nvim/lua/codemp/command.lua

201 lines
4.7 KiB
Lua
Raw Normal View History

2024-08-24 01:56:27 +02:00
local session = require('codemp.session')
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
local function filter(needle, haystack)
local hints = {}
for _, opt in pairs(haystack) do
if vim.startswith(opt, needle) then
table.insert(hints, opt)
end
end
return hints
end
2024-08-08 04:34:23 +02:00
-- always available
local base_actions = {
toggle = function()
require('codemp.window').toggle()
end,
2024-09-01 03:07:05 +02:00
connect = function(host)
client.connect(host)
2024-08-08 04:34:23 +02:00
end,
}
-- only available if state.client is not nil
local connected_actions = {
id = function()
2024-08-24 01:56:27 +02:00
print("> codemp::" .. session.client.id)
2024-08-15 04:07:22 +02:00
end,
2024-08-08 04:34:23 +02:00
join = function(ws)
if ws == nil then error("missing workspace name") end
2024-08-24 01:56:27 +02:00
workspace.join(ws)
2024-08-08 04:34:23 +02:00
end,
start = function(ws)
if ws == nil then error("missing workspace name") end
2024-08-24 01:56:27 +02:00
session.client:create_workspace(ws):await()
2024-09-01 03:07:05 +02:00
vim.schedule(function () workspace.list() end)
print(" <> created workspace " .. ws)
end,
available = function()
2024-08-24 01:56:27 +02:00
for _, ws in ipairs(session.client:list_workspaces(true, false):await()) do
print(" ++ " .. ws)
end
2024-08-24 01:56:27 +02:00
for _, ws in ipairs(session.client:list_workspaces(false, true):await()) do
print(" -- " .. ws)
end
end,
invite = function(user)
local ws
2024-08-24 01:56:27 +02:00
if session.workspace ~= nil then
ws = session.workspace
else
ws = vim.fn.input("workspace > ", "")
end
2024-08-24 01:56:27 +02:00
session.client:invite_to_workspace(ws, user):await()
print(" ][ invited " .. user .. " to workspace " .. ws)
end,
disconnect = function()
2024-08-24 01:56:27 +02:00
print(" xx disconnecting client " .. session.client.id)
session.client = nil -- should drop and thus close everything
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,
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")
path = string.gsub(full_path, cwd .. "/", "")
2024-08-15 01:22:48 +02:00
end
if #path > 0 then
local buf = vim.api.nvim_get_current_buf()
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-01 03:07:05 +02:00
buffers.attach(path, buf, content)
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,
delete = function(path)
if path == nil then error("missing buffer name") end
2024-09-07 01:35:45 +02:00
session.workspace:delete_buffer(path):await()
print(" xx deleted buffer " .. path)
end,
2024-08-08 04:34:23 +02:00
buffers = function()
workspace.open_buffer_tree()
end,
sync = function()
buffers.sync()
end,
attach = function(path, bang)
if path == nil then error("missing buffer name") end
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
buffers.attach(path, buffer)
2024-08-08 04:34:23 +02:00
end,
detach = function(path)
if path == nil then error("missing buffer name") end
buffers.detach(path)
require('codemp.window').update() -- TODO would be nice to do automatically inside
end,
leave = function(ws)
if ws == nil then error("missing workspace to leave") end
2024-08-24 01:56:27 +02:00
workspace.leave()
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-08-24 01:56:27 +02:00
if session.client ~= nil and connected_actions[action] ~= nil then
2024-08-08 04:34:23 +02:00
fn = connected_actions[action]
end
2024-08-24 01:56:27 +02:00
if session.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-08-24 01:56:27 +02:00
if session.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-08-24 01:56:27 +02:00
if session.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
if args[#args-1] == 'attach' or args[#args-1] == 'detach' then
2024-08-24 01:56:27 +02:00
if session.client ~= nil and session.workspace ~= nil then
if session.workspace ~= nil then
return filter(lead, session.workspace:filetree())
2024-08-06 02:21:30 +02:00
end
end
end
return {}
end
end,
}
)