From faa68b235c1431fa25d9b873c3514dc75a22a905 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 26 Sep 2024 03:00:04 +0200 Subject: [PATCH] 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 --- lua/codemp/command.lua | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lua/codemp/command.lua b/lua/codemp/command.lua index cc38af8..e60a0e2 100644 --- a/lua/codemp/command.lua +++ b/lua/codemp/command.lua @@ -150,13 +150,16 @@ local joined_actions = { end, detach = function(path) - if path == nil then error("missing buffer name") end + 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 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 + leave = function() workspace.leave() end, } @@ -215,9 +218,16 @@ vim.api.nvim_create_user_command( end return filter(lead, suggestions) 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 - 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 elseif args[#args-1] == 'join' then return filter(lead, CODEMP.available, function(ws) return ws.name end)