Skip to content

Commit ebfc1ed

Browse files
committed
feat(groups): add except argument
1 parent 4166018 commit ebfc1ed

3 files changed

Lines changed: 58 additions & 24 deletions

File tree

lua/bufferline/commands.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ end
6262

6363
---@param position number
6464
local function handle_group_click(position)
65-
groups.toggle_hidden(position)
65+
groups.toggle_hidden({ priority = position })
6666
ui.refresh()
6767
end
6868

lua/bufferline/groups.lua

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,10 @@ end
286286
---Execute a command on each buffer of a group
287287
---@param group_name string
288288
---@param callback fun(b: bufferline.Buffer)
289-
local function command(group_name, callback)
289+
local function buf_exec(group_name, callback)
290290
local group = utils.find(function(list) return list.name == group_name end, group_state.components_by_group)
291-
292291
if not group then return end
293-
294-
utils.for_each(callback, group)
292+
utils.for_each(callback, group, function(item) return type(item) == "table" end)
295293
end
296294

297295
---@generic T
@@ -344,10 +342,9 @@ function M.set_hidden(id, value)
344342
if group then group.hidden = value end
345343
end
346344

347-
---@param priority number?
348-
---@param name string?
349-
function M.toggle_hidden(priority, name)
350-
local group = priority and group_by_priority(priority) or group_by_name(name)
345+
---@param opts {priority: number?, name: string?}
346+
function M.toggle_hidden(opts)
347+
local group = opts.priority and group_by_priority(opts.priority) or group_by_name(opts.name)
351348
if group then group.hidden = not group.hidden end
352349
end
353350

@@ -453,26 +450,50 @@ end
453450

454451
function M.get_all() return group_state.user_groups end
455452

456-
---@alias group_actions "close" | "toggle"
453+
---@param group_name string
454+
local function close_group(group_name)
455+
buf_exec(group_name, function(b) api.nvim_buf_delete(b.id, { force = true }) end)
456+
if group_name == PINNED_NAME then vim.g[PINNED_KEY] = "" end
457+
for buf, group_id in pairs(group_state.manual_groupings) do
458+
if group_id == group_name then group_state.manual_groupings[buf] = nil end
459+
end
460+
end
461+
462+
---@param args string?
463+
---@param except string[]?
464+
---@return nil
465+
local function handle_close(args, except)
466+
if except then
467+
return utils.for_each(function(group)
468+
if not vim.tbl_contains(except, group.id) then close_group(group.id) end
469+
end, group_state.user_groups)
470+
end
471+
472+
if args then close_group(args) end
473+
end
474+
475+
---@alias bufferline.GroupCLIArgs {except: string[]?}
476+
477+
local arguments = {
478+
except = function(value) return vim.split(value, ",") end,
479+
}
480+
457481
---Execute an action on a group of buffers
458-
---@param name string
482+
---@alias group_actions "close" | "toggle"
483+
---@param args string
459484
---@param action group_actions | fun(b: bufferline.Buffer)
460-
function M.action(name, action)
461-
assert(name, "A name must be passed to execute a group action")
462-
if action == "close" then
463-
command(name, function(b) api.nvim_buf_delete(b.id, { force = true }) end)
464-
ui.refresh()
485+
function M.action(args, action)
486+
assert(args, "arguments must be passed to execute a group action")
487+
local opts = utils.parse_args(args, arguments) ---@type bufferline.GroupCLIArgs
465488

466-
if name == PINNED_NAME then vim.g[PINNED_KEY] = {} end
467-
for buf, group_id in pairs(group_state.manual_groupings) do
468-
if group_id == name then group_state.manual_groupings[buf] = nil end
469-
end
489+
if action == "close" then
490+
handle_close(args, opts.except)
470491
elseif action == "toggle" then
471-
M.toggle_hidden(nil, name)
472-
ui.refresh()
492+
M.toggle_hidden({ name = args })
473493
elseif type(action) == "function" then
474-
command(name, action)
494+
buf_exec(args, action)
475495
end
496+
ui.refresh()
476497
end
477498

478499
function M.toggle_pin()

lua/bufferline/utils/init.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ end
102102
---@param callback fun(item: T)
103103
---@param matcher (fun(item: T):boolean)?
104104
function M.for_each(callback, list, matcher)
105-
for _, item in ipairs(list) do
105+
for _, item in pairs(list) do
106106
if not matcher or matcher(item) then callback(item) end
107107
end
108108
end
@@ -230,4 +230,17 @@ end
230230
--- Determine which list-check function to use
231231
M.is_list = vim.tbl_isarray or vim.tbl_islist
232232

233+
---parse command line arguments into a table
234+
---@generic T
235+
---@param args string
236+
---@param expected {[string]: fun(arg: string): any}
237+
---@return T:table<string, any>
238+
function M.parse_args(args, expected)
239+
local opts = {}
240+
for arg_name, arg_value in args:gmatch("(%S+)=(%S+)") do
241+
if expected[arg_name] then opts[arg_name] = expected[arg_name](arg_value) end
242+
end
243+
return opts
244+
end
245+
233246
return M

0 commit comments

Comments
 (0)