Skip to content

Commit 60c8f77

Browse files
authored
fix(commands): generalize expand_all_nodes (#1661)
1 parent 59070e0 commit 60c8f77

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

Diff for: lua/neo-tree/sources/common/commands.lua

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
--This file should contain all commands meant to be used by mappings.
2-
32
local vim = vim
43
local fs_actions = require("neo-tree.sources.filesystem.lib.fs_actions")
54
local utils = require("neo-tree.utils")
@@ -113,17 +112,17 @@ end
113112
---Expand all nodes
114113
---@param state table The state of the source
115114
---@param node table A node to expand
116-
---@param prefetcher table an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean`
115+
---@param prefetcher table? an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean`
117116
M.expand_all_nodes = function(state, node, prefetcher)
118-
log.debug("Expanding all nodes under " .. node:get_id())
119-
if prefetcher == nil then
120-
prefetcher = node_expander.default_prefetcher
121-
end
117+
local root_nodes = node and { node } or state.tree:get_nodes()
122118

123119
renderer.position.set(state, nil)
124120

125121
local task = function()
126-
node_expander.expand_directory_recursively(state, node, prefetcher)
122+
for _, root in pairs(root_nodes) do
123+
log.debug("Expanding all nodes under " .. root:get_id())
124+
node_expander.expand_directory_recursively(state, root, prefetcher)
125+
end
127126
end
128127
async.run(task, function()
129128
log.debug("All nodes expanded - redrawing")
@@ -150,11 +149,8 @@ M.close_node = function(state, callback)
150149
target_node:collapse()
151150
renderer.redraw(state)
152151
renderer.focus_node(state, target_node:get_id())
153-
if
154-
state.explicitly_opened_directories
155-
and state.explicitly_opened_directories[target_node:get_id()]
156-
then
157-
state.explicitly_opened_directories[target_node:get_id()] = false
152+
if state.explicitly_opened_nodes and state.explicitly_opened_nodes[target_node:get_id()] then
153+
state.explicitly_opened_nodes[target_node:get_id()] = false
158154
end
159155
end
160156
end
@@ -174,16 +170,13 @@ M.close_all_subnodes = function(state)
174170
renderer.collapse_all_nodes(tree, target_node:get_id())
175171
renderer.redraw(state)
176172
renderer.focus_node(state, target_node:get_id())
177-
if
178-
state.explicitly_opened_directories
179-
and state.explicitly_opened_directories[target_node:get_id()]
180-
then
181-
state.explicitly_opened_directories[target_node:get_id()] = false
173+
if state.explicitly_opened_nodes and state.explicitly_opened_nodes[target_node:get_id()] then
174+
state.explicitly_opened_nodes[target_node:get_id()] = false
182175
end
183176
end
184177

185178
M.close_all_nodes = function(state)
186-
state.explicitly_opened_directories = {}
179+
state.explicitly_opened_nodes = {}
187180
renderer.collapse_all_nodes(state.tree)
188181
renderer.redraw(state)
189182
end

Diff for: lua/neo-tree/sources/common/node_expander.lua

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local log = require("neo-tree.log")
2+
local utils = require("neo-tree.utils")
23

34
local M = {}
45

@@ -15,15 +16,15 @@ local function expand_loaded(node, state, prefetcher)
1516
else
1617
if not current_node:is_expanded() then
1718
current_node:expand()
18-
state.explicitly_opened_directories[current_node:get_id()] = true
19+
state.explicitly_opened_nodes[current_node:get_id()] = true
1920
end
2021
local children = state.tree:get_nodes(current_node:get_id())
2122
log.debug("Expanding childrens of " .. current_node:get_id())
2223
for _, child in ipairs(children) do
23-
if child.type == "directory" then
24+
if utils.is_expandable(child) then
2425
rec(child, to_load)
2526
else
26-
log.trace("Child: " .. child.name .. " is not a directory, skipping")
27+
log.trace("Child: " .. (child.name or "") .. " is not expandable, skipping")
2728
end
2829
end
2930
end
@@ -53,16 +54,18 @@ end
5354
--- async method
5455
---@param state table current state of the source
5556
---@param node table a node to expand
56-
---@param prefetcher table an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean`
57+
---@param prefetcher table? an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean`
5758
M.expand_directory_recursively = function(state, node, prefetcher)
5859
log.debug("Expanding directory " .. node:get_id())
59-
if node.type ~= "directory" then
60+
prefetcher = prefetcher or M.default_prefetcher
61+
if not utils.is_expandable(node) then
6062
return
6163
end
62-
state.explicitly_opened_directories = state.explicitly_opened_directories or {}
64+
65+
state.explicitly_opened_nodes = state.explicitly_opened_nodes or {}
6366
if prefetcher.should_prefetch(node) then
6467
local id = node:get_id()
65-
state.explicitly_opened_directories[id] = true
68+
state.explicitly_opened_nodes[id] = true
6669
prefetcher.prefetch(state, node)
6770
expand_loaded(node, state, prefetcher)
6871
else

Diff for: lua/neo-tree/sources/filesystem/commands.lua

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

7070
M.expand_all_nodes = function(state, node)
71-
if node == nil then
72-
node = state.tree:get_node(state.path)
73-
end
7471
cc.expand_all_nodes(state, node, fs.prefetcher)
7572
end
7673

Diff for: lua/neo-tree/sources/filesystem/init.lua

+7-7
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ local follow_internal = function(callback, force_show, async)
6464

6565
log.debug("follow file: ", path_to_reveal)
6666
local show_only_explicitly_opened = function()
67-
state.explicitly_opened_directories = state.explicitly_opened_directories or {}
67+
state.explicitly_opened_nodes = state.explicitly_opened_nodes or {}
6868
local expanded_nodes = renderer.get_expanded_nodes(state.tree)
6969
local state_changed = false
7070
for _, id in ipairs(expanded_nodes) do
71-
if not state.explicitly_opened_directories[id] then
71+
if not state.explicitly_opened_nodes[id] then
7272
if path_to_reveal:sub(1, #id) == id then
73-
state.explicitly_opened_directories[id] = state.follow_current_file.leave_dirs_open
73+
state.explicitly_opened_nodes[id] = state.follow_current_file.leave_dirs_open
7474
else
7575
local node = state.tree:get_node(id)
7676
if node then
@@ -394,20 +394,20 @@ M.toggle_directory = function(state, node, path_to_reveal, skip_redraw, recursiv
394394
if node.type ~= "directory" then
395395
return
396396
end
397-
state.explicitly_opened_directories = state.explicitly_opened_directories or {}
397+
state.explicitly_opened_nodes = state.explicitly_opened_nodes or {}
398398
if node.loaded == false then
399399
local id = node:get_id()
400-
state.explicitly_opened_directories[id] = true
400+
state.explicitly_opened_nodes[id] = true
401401
renderer.position.set(state, nil)
402402
fs_scan.get_items(state, id, path_to_reveal, callback, false, recursive)
403403
elseif node:has_children() then
404404
local updated = false
405405
if node:is_expanded() then
406406
updated = node:collapse()
407-
state.explicitly_opened_directories[node:get_id()] = false
407+
state.explicitly_opened_nodes[node:get_id()] = false
408408
else
409409
updated = node:expand()
410-
state.explicitly_opened_directories[node:get_id()] = true
410+
state.explicitly_opened_nodes[node:get_id()] = true
411411
end
412412
if updated and not skip_redraw then
413413
renderer.redraw(state)

0 commit comments

Comments
 (0)