fix: find_node offset when hide_root_folder is true

fixes #843
This commit is contained in:
kiyan
2022-01-21 12:55:07 +01:00
parent da302ea3b1
commit 00807d7bd5

View File

@@ -85,19 +85,24 @@ end
-- get the node from the tree that matches the predicate -- get the node from the tree that matches the predicate
-- @param nodes list of node -- @param nodes list of node
-- @param fn function(node): boolean -- @param fn function(node): boolean
function M.find_node(nodes, fn) function M.find_node(_nodes, _fn)
local i = 1 local function iter(nodes, fn)
for _, node in ipairs(nodes) do local i = 1
if fn(node) then return node, i end for _, node in ipairs(nodes) do
if node.open and #node.entries > 0 then if fn(node) then return node, i end
local n, idx = M.find_node(node.entries, fn) if node.open and #node.entries > 0 then
i = i + idx local n, idx = iter(node.entries, fn)
if n then return n, i end i = i + idx
else if n then return n, i end
i = i + 1 else
i = i + 1
end
end end
return nil, i
end end
return nil, i local node, i = iter(_nodes, _fn)
i = require'nvim-tree.view'.View.hide_root_folder and i - 1 or i
return node, i
end end
---Create a shallow copy of a portion of a list. ---Create a shallow copy of a portion of a list.