@@ -10,6 +10,7 @@ local log = require("neo-tree.log")
10
10
local manager = require (" neo-tree.sources.manager" )
11
11
local git = require (" neo-tree.git" )
12
12
local glob = require (" neo-tree.sources.filesystem.lib.globtopattern" )
13
+ local async = require (" plenary.async" )
13
14
14
15
local M = {
15
16
name = " filesystem" ,
@@ -406,7 +407,7 @@ M.toggle_directory = function(state, node, path_to_reveal, skip_redraw, recursiv
406
407
local id = node :get_id ()
407
408
state .explicitly_opened_directories [id ] = true
408
409
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 )
410
411
elseif node :has_children () then
411
412
local updated = false
412
413
if node :is_expanded () then
@@ -428,69 +429,76 @@ M.toggle_directory = function(state, node, path_to_reveal, skip_redraw, recursiv
428
429
end
429
430
end
430
431
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
438
445
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" )
449
453
end
450
- else
451
- log .debug (" no kids" )
452
454
end
455
+ end
453
456
end
457
+
458
+ local to_load = {}
459
+ rec (node , to_load )
460
+ return to_load
454
461
end
455
462
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
466
478
end
479
+ rec ({}, 1 )
467
480
end
468
481
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 ())
473
488
if node .type ~= " directory" then
474
- callback ()
489
+ return
475
490
end
476
491
state .explicitly_opened_directories = state .explicitly_opened_directories or {}
477
492
if node .loaded == false then
478
493
local id = node :get_id ()
479
494
state .explicitly_opened_directories [id ] = true
480
495
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 )
487
499
else
488
- local to_load = {}
489
- local progress = 1
490
- call_rec (node , state , to_load , progress , callback )
500
+ expand_and_load (node , state )
491
501
end
492
502
end
493
503
494
-
495
-
496
504
return M
0 commit comments