Skip to content

Commit 40f30a7

Browse files
committed
Rewrite recursive expand to use plenary.async
1 parent c96f154 commit 40f30a7

File tree

5 files changed

+126
-170
lines changed

5 files changed

+126
-170
lines changed

lua/neo-tree/git/ignored.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ M.mark_ignored = function(state, items, callback)
113113
enabled_recording = true,
114114
writer = folder_items,
115115
on_start = function()
116-
log.debug("IGNORED: Running async git with args: ", args)
117-
log.debug("IGNORED: folder items: ", folder_items)
116+
log.trace("IGNORED: Running async git with args: ", args)
118117
end,
119118
on_exit = function(self, code, _)
120119
local result
@@ -128,7 +127,6 @@ M.mark_ignored = function(state, items, callback)
128127
progress = progress + 1
129128
if progress == #jobs then
130129
finalize(all_results)
131-
log.debug("calling master callback all git_ignored finished")
132130
callback(all_results)
133131
end
134132
end,

lua/neo-tree/sources/common/commands.lua

+12-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local popups = require("neo-tree.ui.popups")
1111
local log = require("neo-tree.log")
1212
local help = require("neo-tree.sources.common.help")
1313
local Preview = require("neo-tree.sources.common.preview")
14+
local async = require("plenary.async")
1415

1516
---Gets the node parent folder
1617
---@param state table to look for nodes
@@ -109,27 +110,18 @@ M.add_directory = function(state, callback)
109110
fs_actions.create_directory(in_directory, callback, using_root_directory)
110111
end
111112

112-
M.expand_all_nodes = function(state)
113-
local async = require("plenary.async")
114-
115-
local expand_node = function(node, _callback)
116-
log.debug("load directory ".. node:get_id())
117-
fs.load_directory_async(state,node,_callback)
118-
end
119-
120-
local tasks = {}
121-
for _, node in ipairs(state.tree:get_nodes()) do
122-
local task = async.wrap(function (callback)
123-
log.debug("async expand node" .. node.name)
124-
expand_node(node, callback)
125-
end, 1)
126-
table.insert(tasks, task)
127-
end
128-
log.debug("run all on top " .. #tasks)
129-
async.util.run_all(
130-
tasks,
113+
---Expand all nodes
114+
---@param state table The state of the source
115+
---@param node table A node to expand
116+
M.expand_all_nodes = function(state, node)
117+
log.debug("Expanding all nodes under " .. node:get_id())
118+
local task = function ()
119+
fs.expand_directory(state, node)
120+
end
121+
async.run(
122+
task,
131123
function ()
132-
log.debug("run all on top - finish")
124+
log.debug("All nodes expanded - redrawing")
133125
vim.schedule_wrap(function()
134126
renderer.redraw(state)
135127
end)

lua/neo-tree/sources/filesystem/commands.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ M.delete_visual = function(state, selected_nodes)
6868
end
6969

7070
M.expand_all_nodes = function(state)
71-
cc.expand_all_nodes(state)
71+
cc.expand_all_nodes(state, state.tree:get_node(state.path))
7272
end
7373

7474
---Shows the filter input, which will filter the tree.

lua/neo-tree/sources/filesystem/init.lua

+54-46
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local log = require("neo-tree.log")
1010
local manager = require("neo-tree.sources.manager")
1111
local git = require("neo-tree.git")
1212
local glob = require("neo-tree.sources.filesystem.lib.globtopattern")
13+
local async = require("plenary.async")
1314

1415
local M = {
1516
name = "filesystem",
@@ -406,7 +407,7 @@ M.toggle_directory = function(state, node, path_to_reveal, skip_redraw, recursiv
406407
local id = node:get_id()
407408
state.explicitly_opened_directories[id] = true
408409
renderer.position.set(state, nil)
409-
fs_scan.get_items(state, id, path_to_reveal, callback, true, recursive)
410+
fs_scan.get_items(state, id, path_to_reveal, callback, false, recursive)
410411
elseif node:has_children() then
411412
local updated = false
412413
if node:is_expanded() then
@@ -428,69 +429,76 @@ M.toggle_directory = function(state, node, path_to_reveal, skip_redraw, recursiv
428429
end
429430
end
430431

431-
local function expand_loaded_rec(node, state, to_load)
432-
if node.loaded==false then
433-
table.insert(to_load, node)
434-
else
435-
if not node:is_expanded() then
436-
node:expand()
437-
state.explicitly_opened_directories[node:get_id()] = true
432+
--- Recursively expand all loaded nodes under the given node
433+
--- returns table with all discovered nodes that need to be loaded
434+
---@param node table a node to expand
435+
---@param state table current state of the source
436+
---@return table discovered nodes that need to be loaded
437+
local function expand_loaded(node, state)
438+
local function rec(_node, to_load)
439+
if _node.loaded == false then
440+
table.insert(to_load, _node)
441+
else
442+
if not _node:is_expanded() then
443+
_node:expand()
444+
state.explicitly_opened_directories[_node:get_id()] = true
438445
end
439-
local children = state.tree:get_nodes(node:get_id())
440-
log.debug("children of " .. node.name .. " - " .. #children)
441-
if children then
442-
log.debug("children of " .. node.name .. " - " .. #children)
443-
for _, child in ipairs(children) do
444-
if child.type == "directory" then
445-
expand_loaded_rec(child, state, to_load)
446-
else
447-
log.debug("skipping child " .. vim.inspect(child.name))
448-
end
446+
local children = state.tree:get_nodes(_node:get_id())
447+
log.debug("Expanding childrens of " .. _node:get_id())
448+
for _, child in ipairs(children) do
449+
if child.type == "directory" then
450+
rec(child, to_load)
451+
else
452+
log.trace("Child: " .. child.name .. " is not a directory, skipping")
449453
end
450-
else
451-
log.debug("no kids")
452454
end
455+
end
453456
end
457+
458+
local to_load = {}
459+
rec(node, to_load)
460+
return to_load
454461
end
455462

456-
local function call_rec(node, state, to_load, progress, root_callback)
457-
log.debug("call_rec ".. vim.inspect(node) .. "toload: " .. vim.inspect(to_load) .. " progress:" .. progress)
458-
expand_loaded_rec(node, state, to_load)
459-
progress=progress+1
460-
if progress <= #to_load then
461-
M.load_directory_async(state, to_load[progress], function ()
462-
call_rec(to_load[progress], state, to_load, progress, root_callback)
463-
end)
464-
else
465-
root_callback()
463+
--- Recursively expands all nodes under the given node
464+
--- loading nodes if necessary.
465+
--- asyn method
466+
---@param node table a node to expand
467+
---@param state table current state of the source
468+
local function expand_and_load(node, state)
469+
local function rec(to_load, progress)
470+
local to_load_current = expand_loaded(node, state)
471+
for _,v in ipairs(to_load_current) do
472+
table.insert(to_load, v)
473+
end
474+
if progress <= #to_load then
475+
M.expand_directory(state, to_load[progress])
476+
rec(to_load, progress + 1)
477+
end
466478
end
479+
rec({}, 1)
467480
end
468481

469-
---Expands or collapses the current node.
470-
M.load_directory_async = function(state, node, callback)
471-
log.debug("load_directory_async " .. vim.inspect(node))
472-
local async = require("plenary.async")
482+
---Expands given node recursively loading all descendant nodes if needed
483+
---async method
484+
---@param state table current state of the source
485+
---@param node table a node to expand
486+
M.expand_directory = function(state, node)
487+
log.debug("Expanding directory " .. node:get_id())
473488
if node.type ~= "directory" then
474-
callback()
489+
return
475490
end
476491
state.explicitly_opened_directories = state.explicitly_opened_directories or {}
477492
if node.loaded == false then
478493
local id = node:get_id()
479494
state.explicitly_opened_directories[id] = true
480495
renderer.position.set(state, nil)
481-
async.run(function ()
482-
fs_scan.get_items_2(state, id, true)
483-
end, function ()
484-
expand_loaded_rec(node ,state, {})
485-
callback()
486-
end)
496+
fs_scan.get_dir_items_async(state, id, true)
497+
-- ignore results as we know here that all descendant nodes have been already loaded
498+
expand_loaded(node ,state)
487499
else
488-
local to_load = {}
489-
local progress = 1
490-
call_rec(node, state, to_load, progress, callback)
500+
expand_and_load(node, state)
491501
end
492502
end
493503

494-
495-
496504
return M

0 commit comments

Comments
 (0)