Skip to content

Commit 7822481

Browse files
committed
refactor(node.director): move gen_iterator to expand and safely call descend_until_empty
1 parent 9e19b5e commit 7822481

1 file changed

Lines changed: 38 additions & 43 deletions

File tree

lua/nvim-tree/node/directory.lua

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,10 @@ local function to_lookup_table(list)
320320
end
321321

322322
---@private
323-
---@param _ integer expansion_count
324323
---@return boolean
325-
function DirectoryNode:descend_until_empty(node)
326-
local EXCLUDE = to_lookup_table(node.explorer.opts.actions.expand_all.exclude)
327-
local should_exclude = EXCLUDE[node.name]
324+
function DirectoryNode:descend_until_empty()
325+
local EXCLUDE = to_lookup_table(self.explorer.opts.actions.expand_all.exclude)
326+
local should_exclude = EXCLUDE[self.name]
328327
return not should_exclude
329328
end
330329

@@ -357,53 +356,49 @@ function DirectoryNode:should_expand(expansion_count, node, should_descend)
357356
return false
358357
end
359358

360-
---@private
361-
---@param should_descend fun(expansion_count: integer, node: Node): boolean
362-
---@return fun(node): any
363-
function DirectoryNode:gen_iterator(should_descend)
359+
---@param expand_opts ApiTreeExpandOpts?
360+
function DirectoryNode:expand(expand_opts)
364361
local expansion_count = 0
365362

366-
return function(parent)
367-
if parent.parent and parent.nodes and not parent.open then
368-
expansion_count = expansion_count + 1
369-
parent:expand_dir_node()
370-
end
363+
local function safe_descend_until_empty(_, node)
364+
return self.descend_until_empty(node) -- calling with dot so that node is attached as self
365+
end
371366

372-
Iterator.builder(parent.nodes)
373-
:hidden()
374-
:applier(function(node)
375-
if DirectoryNode:should_expand(expansion_count, node, should_descend) then
376-
expansion_count = expansion_count + 1
377-
node = node:as(DirectoryNode)
378-
if node then
379-
node:expand_dir_node()
380-
end
381-
end
382-
end)
383-
:recursor(function(node)
384-
if not should_descend(expansion_count, node) then
385-
return nil
386-
end
367+
local should_descend = self:limit_folder_discovery((expand_opts and expand_opts.expand_until) or safe_descend_until_empty)
387368

388-
if node.group_next then
389-
return { node.group_next }
390-
end
391369

392-
if node.open and node.nodes then
393-
return node.nodes
394-
end
370+
if self.parent and self.nodes and not self.open then
371+
expansion_count = expansion_count + 1
372+
self:expand_dir_node()
373+
end
395374

375+
Iterator.builder(self.nodes)
376+
:hidden()
377+
:applier(function(node)
378+
if DirectoryNode:should_expand(expansion_count, node, should_descend) then
379+
expansion_count = expansion_count + 1
380+
node = node:as(DirectoryNode)
381+
if node then
382+
node:expand_dir_node()
383+
end
384+
end
385+
end)
386+
:recursor(function(node)
387+
if not should_descend(expansion_count, node) then
396388
return nil
397-
end)
398-
:iterate()
399-
end
400-
end
389+
end
401390

402-
---@param expand_opts ApiTreeExpandOpts?
403-
function DirectoryNode:expand(expand_opts)
404-
local descend_until_empty_fn = self.descend_until_empty
405-
local descend_until = self:limit_folder_discovery((expand_opts and expand_opts.expand_until) or descend_until_empty_fn)
406-
self:gen_iterator(descend_until)(self)
391+
if node.group_next then
392+
return { node.group_next }
393+
end
394+
395+
if node.open and node.nodes then
396+
return node.nodes
397+
end
398+
399+
return nil
400+
end)
401+
:iterate()
407402

408403
self.explorer.renderer:draw()
409404
end

0 commit comments

Comments
 (0)