nvim-tree.lua/lua/nvim-tree/iterators/node-iterator.lua
Alexander Courtis 0a06f65bf0
refactor(#2826): move view to instanced window class (#3153)
* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class

* refactor(#2826): View is an Explorer member

* refactor(#2826): move autocmds to Explorer

* refactor(#2826): API uses Explorer's View

* refactor(#2826): move View into Explorer package

* refactor(#2826): retain necessary view globals

* refactor(#2826): move all winhl to appearance constants

* refactor(#2826): add lifecycle logging to all Explorer members

* refactor(#2826): fix bad cherry-pick

* refactor(#2826): better enumerate_options function

* refactor(#2826): add View.tab_line for debugging

* refactor(#2826): default lifecycle log off

* refactor(#2826): add experimental.multi_instance_debug, split globals out of view, move diagnostics to its own module

* refactor(#2826): instrument View:get_winnr

* refactor(#2826): instrument View:setup_tabpage

* refactor(#2826): instrument View:set_current_win, View:prevent_buffer_override

* refactor(#2826): instrument View:get_bufnr

* refactor(#2826): track member bufnr -> winid with global

* refactor(#2826): tidy experiment names and logs

* vim: nvim-tree: track bufnr via buffer-update channel

* vim: nvim-tree: more logging

* vim: nvim-tree: revert: track bufnr via buffer-update channel

* refactor(#2826): notify error on view winid and bufnr mismatches

* refactor(#2826): notify error on view winid and bufnr mismatches

* refactor(#2826): explorer init logging
2025-06-19 15:45:55 +10:00

80 lines
1.7 KiB
Lua

---@class NodeIterator
local NodeIterator = {}
NodeIterator.__index = NodeIterator
---@param nodes Node[]
---@return NodeIterator
function NodeIterator.builder(nodes)
return setmetatable({
nodes = nodes,
_filter_hidden = function(node)
return not node.hidden
end,
_apply_fn_on_node = function(_) end,
_match = function(_) end,
_recurse_with = function(node)
return node.nodes
end,
}, NodeIterator)
end
---@return NodeIterator
function NodeIterator:hidden()
self._filter_hidden = function(_)
return true
end
return self
end
---@param f fun(node: Node): boolean
---@return NodeIterator
function NodeIterator:matcher(f)
self._match = f
return self
end
---@param f fun(node: Node, i: number)
---@return NodeIterator
function NodeIterator:applier(f)
self._apply_fn_on_node = f
return self
end
---@param f fun(node: Node): any
---@return NodeIterator
function NodeIterator:recursor(f)
self._recurse_with = f
return self
end
---@return Node|nil
---@return number
function NodeIterator:iterate()
local iteration_count = 0
local function iter(nodes)
for _, node in ipairs(nodes) do
if self._filter_hidden(node) then
if not node.group_next then
iteration_count = iteration_count + 1
end
if self._match(node) then
return node, iteration_count
end
self._apply_fn_on_node(node, iteration_count)
local children = self._recurse_with(node)
if children then
local n = iter(children)
if n then
return n, iteration_count
end
end
end
end
return nil, 0
end
return iter(self.nodes)
end
return NodeIterator