Skip to content

Commit

Permalink
Merge pull request #30 from lucobellic/feature/open-groups-by-key-wit…
Browse files Browse the repository at this point in the history
…h-position

feat!: add option to open groups by key with position
  • Loading branch information
lucobellic authored Sep 22, 2024
2 parents cfb3a83 + 8a47a6d commit dc04228
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ Groups without pick_key will be assigned to the first available key in alphabeti
open group with offset relative to the current group.
- **require('edgy-group').open_group_index(position, index)**
open group with index relative to the current position.
- **require('edgy-group').open_groups_by_key(key, toggle?)**
open one or multiple groups with this key, with an optional toggle option.
- **require('edgy-group').open_groups_by_key(key, opts?)**
open one or multiple groups with this key, with optional table {position, toggle}.
- **require('edgy-group.stl').get_statusline(position)**
get a list of string in statusline format for each group icons with optional
highlight and click support.
Expand Down
24 changes: 16 additions & 8 deletions lua/edgy-group/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ end

-- Get list of EdgyGroup with position by key
---@param key string key to pick a group
---@param position? Edgy.Pos position of the group
---@return EdgyGroup.Indexed[]
function M.get_groups_by_key(key)
function M.get_groups_by_key(key, position)
local groups_with_pos = {}
for pos, indexed_groups in pairs(M.groups_by_pos) do
for i, group in ipairs(indexed_groups.groups) do
if group.pick_key == key then table.insert(groups_with_pos, { position = pos, group = group, index = i }) end
if position == nil or pos == position then
for i, group in ipairs(indexed_groups.groups) do
if group.pick_key == key then table.insert(groups_with_pos, { position = pos, group = group, index = i }) end
end
end
end
return groups_with_pos
Expand Down Expand Up @@ -133,7 +136,7 @@ function M.open_group_index(pos, index, toggle)
end, other_groups)

M.open_edgebar_views_by_titles(pos, indexed_group.titles)
M.close_edgebar_views_by_titles(pos, vim.tbl_flatten(other_groups_titles))
M.close_edgebar_views_by_titles(pos, vim.iter(other_groups_titles):flatten():totable())
g.selected_index = index
end
end
Expand All @@ -147,12 +150,17 @@ function M.open_group_offset(pos, offset)
if g then M.open_group_index(pos, g:get_offset_index(offset)) end
end

---@class EdgyGroup.OpenOpts
---@field position? Edgy.Pos position of the group
---@field toggle? boolean toggle a group if at least one window in the group is open

-- Open groups by key, this might open on or multiple groups sharing the same key
---@param key string
---@param toggle? boolean whether to toggle an already opened group or not
function M.open_groups_by_key(key, toggle)
local toggle_group = toggle == nil and M.toggle or toggle
vim.iter(M.get_groups_by_key(key)):each(function(group)
---@param opts? EdgyGroup.OpenOpts option to open a group
function M.open_groups_by_key(key, opts)
local opts = opts or {}
local toggle_group = opts.toggle == nil and M.toggle or opts.toggle
vim.iter(M.get_groups_by_key(key, opts.position)):each(function(group)
pcall(M.open_group_index, group.position, group.index, toggle_group)
end)
end
Expand Down

0 comments on commit dc04228

Please sign in to comment.