feat: more friendly detach and leave commands

leave requires no args, detach shows completions only for attached
buffers and if called with no args works on current buffer when managed
This commit is contained in:
əlemi 2024-09-26 03:00:04 +02:00
parent db1f537dcf
commit faa68b235c
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -150,13 +150,16 @@ local joined_actions = {
end, end,
detach = function(path) detach = function(path)
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 if path == nil then error("missing buffer name") end
end
buffers.detach(path) buffers.detach(path)
require('codemp.window').update() -- TODO would be nice to do automatically inside require('codemp.window').update() -- TODO would be nice to do automatically inside
end, end,
leave = function(ws) leave = function()
if ws == nil then error("missing workspace to leave") end
workspace.leave() workspace.leave()
end, end,
} }
@ -215,9 +218,16 @@ vim.api.nvim_create_user_command(
end end
return filter(lead, suggestions) return filter(lead, suggestions)
elseif stage == 3 then elseif stage == 3 then
if args[#args-1] == 'attach' or args[#args-1] == 'detach' then local last_arg = args[#args-1]
if last_arg == 'attach' or last_arg == 'detach' then
if CODEMP.client ~= nil and CODEMP.workspace ~= nil then if CODEMP.client ~= nil and CODEMP.workspace ~= nil then
return filter(lead, CODEMP.workspace:filetree()) local choices
if last_arg == "attach" then
choices = CODEMP.workspace:filetree()
elseif last_arg == "detach" then
choices = CODEMP.workspace.active_buffers
end
return filter(lead, choices)
end end
elseif args[#args-1] == 'join' then elseif args[#args-1] == 'join' then
return filter(lead, CODEMP.available, function(ws) return ws.name end) return filter(lead, CODEMP.available, function(ws) return ws.name end)