Skip to content

Commit

Permalink
feat(tree): added flatten() to get all items from the tree
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent cb59440 commit 35c0236
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lua/trouble/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ function M:count()
return self._count
end

---@param ret trouble.Item[]?
function M:flatten(ret)
ret = ret or {}
for _, child in ipairs(self.children or {}) do
child:flatten(ret)
end
if not self.group and self.item then
ret[#ret + 1] = self.item
end
return ret
end

---@param idx number|string
---@return trouble.Node?
function M:get(idx)
Expand Down
8 changes: 8 additions & 0 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ function M:count()
return count
end

function M:flatten()
local ret = {}
for _, section in ipairs(self.sections) do
section.node:flatten(ret)
end
return ret
end

function M:update()
if self.opts.auto_close and self:count() == 0 then
return self:close()
Expand Down

0 comments on commit 35c0236

Please sign in to comment.