From d825a0d6fdbc318a52f8b2eb19f1bbc60ca3d3d1 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 6 Aug 2024 02:21:30 +0200 Subject: [PATCH] chore: separated command into file --- src/command.lua | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ src/init.lua | 82 +++---------------------------------------------- 2 files changed, 85 insertions(+), 78 deletions(-) create mode 100644 src/command.lua diff --git a/src/command.lua b/src/command.lua new file mode 100644 index 0000000..b57116b --- /dev/null +++ b/src/command.lua @@ -0,0 +1,81 @@ +local client = require('codemp.client') +local buffers = require('codemp.buffer') +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 + +vim.api.nvim_create_user_command( + "MP", + function (args) + if args.fargs[1] == "login" then + client.login(args.fargs[2], args.fargs[3], args.fargs[4]) + elseif args.fargs[1] == "create" then + if #args.fargs < 2 then error("missing buffer name") end + if client.workspace == nil then error("connect to a workspace first") end + buffers.create(client.workspace, args.fargs[2]) + elseif args.fargs[1] == "join" then + if #args.fargs < 2 then error("missing workspace name") end + client.workspace = args.fargs[2] + workspace.join(client.workspace) + elseif args.fargs[1] == "attach" then + if #args.fargs < 2 then error("missing buffer name") end + if client.workspace == nil then error("connect to a workspace first") end + buffers.attach(client.workspace, args.fargs[2], args.bang) + elseif args.fargs[1] == "sync" then + if client.workspace == nil then error("connect to a workspace first") end + buffers.sync(client.workspace) + elseif args.fargs[1] == "buffers" then + if client.workspace == nil then error("connect to a workspace first") end + workspace.buffers(client.workspace) + -- elseif args.fargs[1] == "users" then + -- if client.workspace == nil then error("connect to a workspace first") end + -- workspace.users(client.workspace) + -- elseif args.fargs[1] == "detach" then + -- if #args.fargs < 2 then error("missing buffer name") end + -- if client.workspace == nil then error("connect to a workspace first") end + -- buffers.detach(client.workspace, args.fargs[2]) + -- elseif args.fargs[1] == "leave" then + -- if client.workspace == nil then error("connect to a workspace first") end + -- workspace.leave() + -- client.workspace = nil + end + if args.bang then + print("pls stop shouting :'c") + 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 + return filter(lead, {'login', 'create', 'join', 'attach', 'sync', 'buffers', 'users', 'detach', 'leave'}) + elseif stage == 3 then + if args[#args-1] == 'attach' or args[#args-1] == 'detach' then + if client.workspace ~= nil then + local ws = native.get_workspace(client.workspace) + if ws ~= nil then + return filter(lead, ws.filetree) + end + end + end + + return {} + end + end, + } +) diff --git a/src/init.lua b/src/init.lua index 3323580..3280066 100644 --- a/src/init.lua +++ b/src/init.lua @@ -1,8 +1,5 @@ local native = require('codemp.loader')() -- make sure we can load the native library correctly, otherwise no point going forward -local client = require('codemp.client') -local buffers = require('codemp.buffer') -local workspace = require('codemp.workspace') -- TODO nvim docs say that we should stop all threads before exiting nvim -- but we like to live dangerously (: @@ -15,84 +12,13 @@ vim.loop.new_thread({}, function() end end) -local active_workspace = nil -- TODO dont use a single global one!!! - -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 - -vim.api.nvim_create_user_command( - "MP", - function (args) - if args.fargs[1] == "login" then - client.login(args.fargs[2], args.fargs[3], args.fargs[4]) - elseif args.fargs[1] == "create" then - if #args.fargs < 2 then error("missing buffer name") end - if active_workspace == nil then error("connect to a workspace first") end - buffers.create(active_workspace, args.fargs[2]) - elseif args.fargs[1] == "join" then - if #args.fargs < 2 then error("missing workspace name") end - active_workspace = args.fargs[2] - workspace.join(active_workspace) - elseif args.fargs[1] == "attach" then - if #args.fargs < 2 then error("missing buffer name") end - if active_workspace == nil then error("connect to a workspace first") end - buffers.attach(active_workspace, args.fargs[2], args.bang) - elseif args.fargs[1] == "sync" then - if active_workspace == nil then error("connect to a workspace first") end - buffers.sync(active_workspace) - elseif args.fargs[1] == "buffers" then - if active_workspace == nil then error("connect to a workspace first") end - workspace.buffers(active_workspace) - elseif args.fargs[1] == "users" then - if active_workspace == nil then error("connect to a workspace first") end - workspace.users(active_workspace) - elseif args.fargs[1] == "detach" then - if #args.fargs < 2 then error("missing buffer name") end - if active_workspace == nil then error("connect to a workspace first") end - buffers.detach(active_workspace, args.fargs[2]) - elseif args.fargs[1] == "leave" then - if active_workspace == nil then error("connect to a workspace first") end - workspace.leave() - active_workspace = nil - end - end, - { - 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 - return filter(lead, {'login', 'create', 'join', 'attach', 'sync', 'buffers', 'users', 'detach', 'leave'}) - elseif stage == 3 then - if args[#args-1] == 'attach' or args[#args-1] == 'detach' then - if active_workspace ~= nil then - local ws = native.get_workspace(active_workspace) - if ws ~= nil then - return filter(lead, ws.filetree) - end - end - end - - return {} - end - end, - } -) +local command = require('codemp.command') return { native = native, - client = client, - buffers = buffers, - workspace = workspace, + client = require('codemp.client'), + buffers = require('codemp.buffer'), + workspace = require('codemp.workspace'), utils = require('codemp.utils'), async = require('codemp.async'), }