2024-08-08 04:34:23 +02:00
|
|
|
local state = require('codemp.state')
|
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')
|
|
|
|
|
|
|
|
local native = require('codemp.loader').load()
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
connect = function(host)
|
|
|
|
if host == nil then host = 'http://codemp.alemi.dev:50053' end
|
|
|
|
local user = vim.fn.input("username > ", "user-" .. vim.fn.rand() % 1024)
|
|
|
|
local password = vim.fn.input("password > ", "lmaodefaultpassword")
|
|
|
|
state.client = native.connect(host, user, password)
|
|
|
|
print(" ++ connected to " .. host .. " as " .. user)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- only available if state.client is not nil
|
|
|
|
local connected_actions = {
|
|
|
|
id = function()
|
|
|
|
print(" ::codemp#" .. state.client.id)
|
|
|
|
end,
|
|
|
|
|
|
|
|
join = function(ws)
|
|
|
|
if ws == nil then error("missing workspace name") end
|
|
|
|
state.workspace = ws
|
|
|
|
workspace.join(ws)
|
|
|
|
print(" >< joined workspace " .. ws)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- 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,
|
|
|
|
|
|
|
|
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
|
|
|
|
buffers.attach(path, bang)
|
|
|
|
end,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if state.client ~= nil and connected_actions[action] ~= nil then
|
|
|
|
fn = connected_actions[action]
|
|
|
|
end
|
|
|
|
|
|
|
|
if state.workspace ~= nil and joined_actions[action] ~= nil then
|
|
|
|
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
|
|
|
|
if state.client ~= nil then
|
|
|
|
for sugg, _ in pairs(connected_actions) do
|
|
|
|
n = n + 1
|
|
|
|
suggestions[n] = sugg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if state.workspace ~= nil then
|
|
|
|
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-08 04:34:23 +02:00
|
|
|
if state.client ~= nil and state.workspace ~= nil then
|
|
|
|
local ws = state.client:get_workspace(state.workspace)
|
2024-08-06 02:21:30 +02:00
|
|
|
if ws ~= nil then
|
|
|
|
return filter(lead, ws.filetree)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
)
|