* chore: add type annotations to (almost) all functions
* stylua
* Add classes for symlink nodes
* Replace deprecated `@vararg`
* Move node classes to `node` module
* Fix `Symlink*` classes
* add vim and libuv runtime for luals, qualify libuv types
* add scripts/luals-check, not quite ready for CI
* additional nil checks for git/init.lua and git/runner.lua
* additional nil checks for nvim-tree.lua
* wrap vim.cmd-as-a-function calls inside functions
* vim.tbl_filter predicate returns booleans
* Revert "add scripts/luals-check, not quite ready for CI"
This reverts commit c70229cad9.
* Add `MinimalNode` class in `marks` module
* Fix various LSP warnings
* stylua
* Fix `Explorer` class, update related annotations and add necessary checks
* Add missing annotations to `live-filter`
* Add temporary aliases for `uv.*` types
* Resolve remaining LSP warnings
* Revert changes not related to internal types
* Minor adjustments
* Update doc comments style
* Minor adjustments (pt. 2)
---------
Co-authored-by: Alexander Courtis <alex@courtis.org>
80 lines
1.7 KiB
Lua
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|nil
|
|
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
|