From d710b1a088be6b12fd1c099b0a05b75d87da1c0e Mon Sep 17 00:00:00 2001 From: pynappo Date: Thu, 23 Jan 2025 04:25:04 -0800 Subject: [PATCH 1/4] generalize expand_all_nodes --- lua/neo-tree/sources/common/commands.lua | 7 +- lua/neo-tree/sources/common/node_expander.lua | 69 ++++++++++--------- lua/neo-tree/sources/filesystem/commands.lua | 3 - 3 files changed, 38 insertions(+), 41 deletions(-) diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index 1a7e528a..518e7589 100644 --- a/lua/neo-tree/sources/common/commands.lua +++ b/lua/neo-tree/sources/common/commands.lua @@ -1,5 +1,4 @@ --This file should contain all commands meant to be used by mappings. - local vim = vim local fs_actions = require("neo-tree.sources.filesystem.lib.fs_actions") local utils = require("neo-tree.utils") @@ -113,12 +112,10 @@ end ---Expand all nodes ---@param state table The state of the source ---@param node table A node to expand ----@param prefetcher table an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean` +---@param prefetcher table? an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean` M.expand_all_nodes = function(state, node, prefetcher) + node = node or state.tree:get_nodes()[1] log.debug("Expanding all nodes under " .. node:get_id()) - if prefetcher == nil then - prefetcher = node_expander.default_prefetcher - end renderer.position.set(state, nil) diff --git a/lua/neo-tree/sources/common/node_expander.lua b/lua/neo-tree/sources/common/node_expander.lua index c04f736c..d6e6dca4 100644 --- a/lua/neo-tree/sources/common/node_expander.lua +++ b/lua/neo-tree/sources/common/node_expander.lua @@ -1,51 +1,52 @@ local log = require("neo-tree.log") +local utils = require("neo-tree.utils") local M = {} --- Recursively expand all loaded nodes under the given node ---- returns table with all discovered nodes that need to be loaded +--- returns table with all discovered nodes that need to be loaded ---@param node table a node to expand ---@param state table current state of the source ---@return table discovered nodes that need to be loaded local function expand_loaded(node, state, prefetcher) - local function rec(current_node, to_load) - if prefetcher.should_prefetch(current_node) then - log.trace("Node " .. current_node:get_id() .. "not loaded, saving for later") - table.insert(to_load, current_node) - else - if not current_node:is_expanded() then - current_node:expand() - state.explicitly_opened_directories[current_node:get_id()] = true - end - local children = state.tree:get_nodes(current_node:get_id()) - log.debug("Expanding childrens of " .. current_node:get_id()) - for _, child in ipairs(children) do - if child.type == "directory" then - rec(child, to_load) - else - log.trace("Child: " .. child.name .. " is not a directory, skipping") - end + local function rec(current_node, to_load) + if prefetcher.should_prefetch(current_node) then + log.trace("Node " .. current_node:get_id() .. "not loaded, saving for later") + table.insert(to_load, current_node) + else + if not current_node:is_expanded() then + current_node:expand() + state.explicitly_opened_directories[current_node:get_id()] = true + end + local children = state.tree:get_nodes(current_node:get_id()) + log.debug("Expanding childrens of " .. current_node:get_id()) + for _, child in ipairs(children) do + if utils.is_expandable(child) then + rec(child, to_load) + else + log.trace("Child: " .. (child.name or "") .. " is not expandable, skipping") end end end + end - local to_load = {} - rec(node, to_load) - return to_load + local to_load = {} + rec(node, to_load) + return to_load end --- Recursively expands all nodes under the given node collecting all unloaded nodes ---- Then run prefetcher on all unloaded nodes. Finally, expand loded nodes. +--- Then run prefetcher on all unloaded nodes. Finally, expand loded nodes. --- async method ---@param node table a node to expand ---@param state table current state of the source local function expand_and_load(node, state, prefetcher) - local to_load = expand_loaded(node, state, prefetcher) - for _, _node in ipairs(to_load) do - prefetcher.prefetch(state, _node) - -- no need to handle results as prefetch is recursive - expand_loaded(_node, state, prefetcher) - end + local to_load = expand_loaded(node, state, prefetcher) + for _, _node in ipairs(to_load) do + prefetcher.prefetch(state, _node) + -- no need to handle results as prefetch is recursive + expand_loaded(_node, state, prefetcher) + end end --- Expands given node recursively loading all descendant nodes if needed @@ -53,12 +54,14 @@ end --- async method ---@param state table current state of the source ---@param node table a node to expand ----@param prefetcher table an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean` +---@param prefetcher table? an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean` M.expand_directory_recursively = function(state, node, prefetcher) log.debug("Expanding directory " .. node:get_id()) - if node.type ~= "directory" then + prefetcher = prefetcher or M.default_prefetcher + if not utils.is_expandable(node) then return end + state.explicitly_opened_directories = state.explicitly_opened_directories or {} if prefetcher.should_prefetch(node) then local id = node:get_id() @@ -71,12 +74,12 @@ M.expand_directory_recursively = function(state, node, prefetcher) end M.default_prefetcher = { - prefetch = function (state, node) + prefetch = function(state, node) log.debug("Default expander prefetch does nothing") end, - should_prefetch = function (node) + should_prefetch = function(node) return false - end + end, } return M diff --git a/lua/neo-tree/sources/filesystem/commands.lua b/lua/neo-tree/sources/filesystem/commands.lua index 3198d9f1..958f1fa5 100644 --- a/lua/neo-tree/sources/filesystem/commands.lua +++ b/lua/neo-tree/sources/filesystem/commands.lua @@ -68,9 +68,6 @@ M.delete_visual = function(state, selected_nodes) end M.expand_all_nodes = function(state, node) - if node == nil then - node = state.tree:get_node(state.path) - end cc.expand_all_nodes(state, node, fs.prefetcher) end From 075809d280d9a92200bbdf1d36cea74bdee21eed Mon Sep 17 00:00:00 2001 From: pynappo Date: Sun, 26 Jan 2025 03:21:10 -0800 Subject: [PATCH 2/4] support 0/multiple root nodes --- lua/neo-tree/sources/common/commands.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index 518e7589..f963d344 100644 --- a/lua/neo-tree/sources/common/commands.lua +++ b/lua/neo-tree/sources/common/commands.lua @@ -114,13 +114,15 @@ end ---@param node table A node to expand ---@param prefetcher table? an object with two methods `prefetch(state, node)` and `should_prefetch(node) => boolean` M.expand_all_nodes = function(state, node, prefetcher) - node = node or state.tree:get_nodes()[1] - log.debug("Expanding all nodes under " .. node:get_id()) + local root_nodes = node and { node } or state.tree:get_nodes() renderer.position.set(state, nil) local task = function() - node_expander.expand_directory_recursively(state, node, prefetcher) + for _, root in pairs(root_nodes) do + log.debug("Expanding all nodes under " .. root:get_id()) + node_expander.expand_directory_recursively(state, root, prefetcher) + end end async.run(task, function() log.debug("All nodes expanded - redrawing") From 5b3f525e6641003532edd85e3575faefcb100eba Mon Sep 17 00:00:00 2001 From: pynappo Date: Sun, 26 Jan 2025 03:23:11 -0800 Subject: [PATCH 3/4] rename explicitly_opened_directories --- lua/neo-tree/sources/common/commands.lua | 14 +++++++------- lua/neo-tree/sources/common/node_expander.lua | 6 +++--- lua/neo-tree/sources/filesystem/init.lua | 14 +++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index f963d344..5c2701b6 100644 --- a/lua/neo-tree/sources/common/commands.lua +++ b/lua/neo-tree/sources/common/commands.lua @@ -150,10 +150,10 @@ M.close_node = function(state, callback) renderer.redraw(state) renderer.focus_node(state, target_node:get_id()) if - state.explicitly_opened_directories - and state.explicitly_opened_directories[target_node:get_id()] + state.explicitly_opened_nodes + and state.explicitly_opened_nodes[target_node:get_id()] then - state.explicitly_opened_directories[target_node:get_id()] = false + state.explicitly_opened_nodes[target_node:get_id()] = false end end end @@ -174,15 +174,15 @@ M.close_all_subnodes = function(state) renderer.redraw(state) renderer.focus_node(state, target_node:get_id()) if - state.explicitly_opened_directories - and state.explicitly_opened_directories[target_node:get_id()] + state.explicitly_opened_nodes + and state.explicitly_opened_nodes[target_node:get_id()] then - state.explicitly_opened_directories[target_node:get_id()] = false + state.explicitly_opened_nodes[target_node:get_id()] = false end end M.close_all_nodes = function(state) - state.explicitly_opened_directories = {} + state.explicitly_opened_nodes = {} renderer.collapse_all_nodes(state.tree) renderer.redraw(state) end diff --git a/lua/neo-tree/sources/common/node_expander.lua b/lua/neo-tree/sources/common/node_expander.lua index d6e6dca4..7d4991ab 100644 --- a/lua/neo-tree/sources/common/node_expander.lua +++ b/lua/neo-tree/sources/common/node_expander.lua @@ -16,7 +16,7 @@ local function expand_loaded(node, state, prefetcher) else if not current_node:is_expanded() then current_node:expand() - state.explicitly_opened_directories[current_node:get_id()] = true + state.explicitly_opened_nodes[current_node:get_id()] = true end local children = state.tree:get_nodes(current_node:get_id()) log.debug("Expanding childrens of " .. current_node:get_id()) @@ -62,10 +62,10 @@ M.expand_directory_recursively = function(state, node, prefetcher) return end - state.explicitly_opened_directories = state.explicitly_opened_directories or {} + state.explicitly_opened_nodes = state.explicitly_opened_nodes or {} if prefetcher.should_prefetch(node) then local id = node:get_id() - state.explicitly_opened_directories[id] = true + state.explicitly_opened_nodes[id] = true prefetcher.prefetch(state, node) expand_loaded(node, state, prefetcher) else diff --git a/lua/neo-tree/sources/filesystem/init.lua b/lua/neo-tree/sources/filesystem/init.lua index 7101864c..16ad42dc 100644 --- a/lua/neo-tree/sources/filesystem/init.lua +++ b/lua/neo-tree/sources/filesystem/init.lua @@ -64,13 +64,13 @@ local follow_internal = function(callback, force_show, async) log.debug("follow file: ", path_to_reveal) local show_only_explicitly_opened = function() - state.explicitly_opened_directories = state.explicitly_opened_directories or {} + state.explicitly_opened_nodes = state.explicitly_opened_nodes or {} local expanded_nodes = renderer.get_expanded_nodes(state.tree) local state_changed = false for _, id in ipairs(expanded_nodes) do - if not state.explicitly_opened_directories[id] then + if not state.explicitly_opened_nodes[id] then if path_to_reveal:sub(1, #id) == id then - state.explicitly_opened_directories[id] = state.follow_current_file.leave_dirs_open + state.explicitly_opened_nodes[id] = state.follow_current_file.leave_dirs_open else local node = state.tree:get_node(id) if node then @@ -394,20 +394,20 @@ M.toggle_directory = function(state, node, path_to_reveal, skip_redraw, recursiv if node.type ~= "directory" then return end - state.explicitly_opened_directories = state.explicitly_opened_directories or {} + state.explicitly_opened_nodes = state.explicitly_opened_nodes or {} if node.loaded == false then local id = node:get_id() - state.explicitly_opened_directories[id] = true + state.explicitly_opened_nodes[id] = true renderer.position.set(state, nil) fs_scan.get_items(state, id, path_to_reveal, callback, false, recursive) elseif node:has_children() then local updated = false if node:is_expanded() then updated = node:collapse() - state.explicitly_opened_directories[node:get_id()] = false + state.explicitly_opened_nodes[node:get_id()] = false else updated = node:expand() - state.explicitly_opened_directories[node:get_id()] = true + state.explicitly_opened_nodes[node:get_id()] = true end if updated and not skip_redraw then renderer.redraw(state) From e2db3d999843c5ec823fb33b21eaaa52b44b8484 Mon Sep 17 00:00:00 2001 From: pynappo Date: Sun, 26 Jan 2025 03:25:35 -0800 Subject: [PATCH 4/4] stylua --- lua/neo-tree/sources/common/commands.lua | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index 5c2701b6..cc3442dc 100644 --- a/lua/neo-tree/sources/common/commands.lua +++ b/lua/neo-tree/sources/common/commands.lua @@ -149,10 +149,7 @@ M.close_node = function(state, callback) target_node:collapse() renderer.redraw(state) renderer.focus_node(state, target_node:get_id()) - if - state.explicitly_opened_nodes - and state.explicitly_opened_nodes[target_node:get_id()] - then + if state.explicitly_opened_nodes and state.explicitly_opened_nodes[target_node:get_id()] then state.explicitly_opened_nodes[target_node:get_id()] = false end end @@ -173,10 +170,7 @@ M.close_all_subnodes = function(state) renderer.collapse_all_nodes(tree, target_node:get_id()) renderer.redraw(state) renderer.focus_node(state, target_node:get_id()) - if - state.explicitly_opened_nodes - and state.explicitly_opened_nodes[target_node:get_id()] - then + if state.explicitly_opened_nodes and state.explicitly_opened_nodes[target_node:get_id()] then state.explicitly_opened_nodes[target_node:get_id()] = false end end