From cd96cf9c1d83d19610da050d30c1363d875b4c36 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 1 Jun 2024 13:44:38 +0200 Subject: [PATCH 01/16] Add new parameter to expand function --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index 6af62e6ae49..b428806b7c6 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -29,13 +29,13 @@ end ---@param expansion_count integer ---@param node Node ---@return boolean -local function should_expand(expansion_count, node) +local function expand_until_max_or_empty(expansion_count, node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[node.name] return not should_halt and node.nodes and not node.open and not should_exclude end -local function gen_iterator() +local function gen_iterator(should_expand) local expansion_count = 0 return function(parent) @@ -64,9 +64,10 @@ local function gen_iterator() end ---@param base_node table -function M.fn(base_node) +function M.fn(base_node, expand_until) + expand_until = expand_until or expand_until_max_or_empty local node = base_node.nodes and base_node or core.get_explorer() - if gen_iterator()(node) then + if gen_iterator(expand_until)(node) then notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") end renderer.draw() From 9f0a22460d3b5fe939f58f19cbc765645c9038f8 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 1 Jun 2024 14:51:34 +0200 Subject: [PATCH 02/16] feat: Add more controll to expand-all function --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index b428806b7c6..20eedb71b20 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -28,14 +28,15 @@ end ---@param expansion_count integer ---@param node Node ----@return boolean +---@return boolean, function local function expand_until_max_or_empty(expansion_count, node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[node.name] - return not should_halt and node.nodes and not node.open and not should_exclude + local result = not should_halt and node.nodes and not node.open and not should_exclude + return result, expand_until_max_or_empty end -local function gen_iterator(should_expand) +local function gen_iterator(should_expand_fn) local expansion_count = 0 return function(parent) @@ -47,7 +48,9 @@ local function gen_iterator(should_expand) Iterator.builder(parent.nodes) :hidden() :applier(function(node) - if should_expand(expansion_count, node) then + local should_expand, should_expand_next_fn = should_expand_fn(expansion_count, node) + should_expand_fn = should_expand_next_fn + if should_expand then expansion_count = expansion_count + 1 expand(node) end From f691687a8aff3941b4143546a9c9bc84190bd277 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 1 Sep 2024 19:53:27 +0200 Subject: [PATCH 03/16] Working better --- .../actions/tree/modifiers/expand-all.lua | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index 20eedb71b20..e200f011ee9 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -40,12 +40,13 @@ local function gen_iterator(should_expand_fn) local expansion_count = 0 return function(parent) - if parent.parent and parent.nodes and not parent.open then - expansion_count = expansion_count + 1 - expand(parent) - end + -- if parent.parent and parent.nodes and not parent.open then + -- expansion_count = expansion_count + 1 + -- expand(parent) + -- end + expand(parent) - Iterator.builder(parent.nodes) + Iterator.builder({parent}) :hidden() :applier(function(node) local should_expand, should_expand_next_fn = should_expand_fn(expansion_count, node) @@ -54,8 +55,14 @@ local function gen_iterator(should_expand_fn) expansion_count = expansion_count + 1 expand(node) end + -- print("iterator " .. node.name .. " " .. vim.inspect(should_expand)) end) :recursor(function(node) + -- print(vim.inspect(expansion_count < M.MAX_FOLDER_DISCOVERY)) + -- print(vim.inspect(node.group_next and { node.group_next })) + -- print(vim.inspect((node.open and node.nodes))) + + -- print("recursor " .. node.name .. " " .. vim.inspect(expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)))) return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)) end) :iterate() From 7cf4cf84f5f0b23e743af5c8c8452e340a0738f2 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 1 Sep 2024 19:54:52 +0200 Subject: [PATCH 04/16] Remove print statements --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index e200f011ee9..ab62f992844 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -40,10 +40,6 @@ local function gen_iterator(should_expand_fn) local expansion_count = 0 return function(parent) - -- if parent.parent and parent.nodes and not parent.open then - -- expansion_count = expansion_count + 1 - -- expand(parent) - -- end expand(parent) Iterator.builder({parent}) @@ -55,14 +51,8 @@ local function gen_iterator(should_expand_fn) expansion_count = expansion_count + 1 expand(node) end - -- print("iterator " .. node.name .. " " .. vim.inspect(should_expand)) end) :recursor(function(node) - -- print(vim.inspect(expansion_count < M.MAX_FOLDER_DISCOVERY)) - -- print(vim.inspect(node.group_next and { node.group_next })) - -- print(vim.inspect((node.open and node.nodes))) - - -- print("recursor " .. node.name .. " " .. vim.inspect(expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)))) return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)) end) :iterate() From e8c4ce49c6d6b1c669e288dde83e622024ac334a Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 1 Sep 2024 20:23:23 +0200 Subject: [PATCH 05/16] Pass function to populate_node to userland --- .../actions/tree/modifiers/expand-all.lua | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index ab62f992844..9fa64dce604 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -3,6 +3,7 @@ local renderer = require "nvim-tree.renderer" local Iterator = require "nvim-tree.iterators.node-iterator" local notify = require "nvim-tree.notify" local lib = require "nvim-tree.lib" +local git = require "nvim-tree.git" local M = {} @@ -18,18 +19,28 @@ local function to_lookup_table(list) end ---@param node Node -local function expand(node) - node = lib.get_last_group_node(node) - node.open = true +local function populate_node(node) + -- noop if it is a file + if node.nodes == nil then + return + end + local cwd = node.link_to or node.absolute_path + local handle = vim.loop.fs_scandir(cwd) + if not handle then + return + end + local status = git.load_project_status(cwd) + if #node.nodes == 0 then - core.get_explorer():expand(node) + core.get_explorer():expand(node, status) end end ---@param expansion_count integer ---@param node Node +---@param populate_node function ---@return boolean, function -local function expand_until_max_or_empty(expansion_count, node) +local function expand_until_max_or_empty(expansion_count, node, populate_node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[node.name] local result = not should_halt and node.nodes and not node.open and not should_exclude @@ -40,16 +51,17 @@ local function gen_iterator(should_expand_fn) local expansion_count = 0 return function(parent) - expand(parent) + parent.open = true - Iterator.builder({parent}) + Iterator.builder({ parent }) :hidden() :applier(function(node) - local should_expand, should_expand_next_fn = should_expand_fn(expansion_count, node) + local should_expand, should_expand_next_fn = should_expand_fn(expansion_count, node, populate_node) should_expand_fn = should_expand_next_fn if should_expand then expansion_count = expansion_count + 1 - expand(node) + node = lib.get_last_group_node(node) + node.open = true end end) :recursor(function(node) From 45103323fd9a13fcedc610ec72bb82a8d1c9b6a4 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 1 Sep 2024 20:24:05 +0200 Subject: [PATCH 06/16] format code --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index 9fa64dce604..f419d386c7c 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -27,12 +27,12 @@ local function populate_node(node) local cwd = node.link_to or node.absolute_path local handle = vim.loop.fs_scandir(cwd) if not handle then - return + return end local status = git.load_project_status(cwd) if #node.nodes == 0 then - core.get_explorer():expand(node, status) + core.get_explorer():expand(node, status) end end From a3f15887419445e9f7a7aee192e861febdf245d5 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 1 Sep 2024 20:31:09 +0200 Subject: [PATCH 07/16] Call populate_node in case it wasn't called by the user --- .../actions/tree/modifiers/expand-all.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index f419d386c7c..aa7363ecf12 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -24,14 +24,13 @@ local function populate_node(node) if node.nodes == nil then return end - local cwd = node.link_to or node.absolute_path - local handle = vim.loop.fs_scandir(cwd) - if not handle then - return - end - local status = git.load_project_status(cwd) - if #node.nodes == 0 then + local cwd = node.link_to or node.absolute_path + local handle = vim.loop.fs_scandir(cwd) + if not handle then + return + end + local status = git.load_project_status(cwd) core.get_explorer():expand(node, status) end end @@ -51,6 +50,7 @@ local function gen_iterator(should_expand_fn) local expansion_count = 0 return function(parent) + populate_node(parent) parent.open = true Iterator.builder({ parent }) @@ -60,6 +60,7 @@ local function gen_iterator(should_expand_fn) should_expand_fn = should_expand_next_fn if should_expand then expansion_count = expansion_count + 1 + populate_node(node) node = lib.get_last_group_node(node) node.open = true end From e0e12a494795712951d6d61710196a814bcbe6a9 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 1 Sep 2024 20:40:41 +0200 Subject: [PATCH 08/16] Ignore unused variable --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index aa7363ecf12..29c12b0e6db 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -39,12 +39,14 @@ end ---@param node Node ---@param populate_node function ---@return boolean, function +-- luacheck: push ignore populate_node local function expand_until_max_or_empty(expansion_count, node, populate_node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[node.name] local result = not should_halt and node.nodes and not node.open and not should_exclude return result, expand_until_max_or_empty end +-- luacheck: pop local function gen_iterator(should_expand_fn) local expansion_count = 0 From 96ffae1e8a4f193379e2ff2af017e4c56edbbc66 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 1 Sep 2024 22:07:58 +0200 Subject: [PATCH 09/16] Remove iterator from should_expand --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 10 +++++----- lua/nvim-tree/explorer/node.lua | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index 29c12b0e6db..f76b2027901 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -38,13 +38,13 @@ end ---@param expansion_count integer ---@param node Node ---@param populate_node function ----@return boolean, function +---@return boolean -- luacheck: push ignore populate_node local function expand_until_max_or_empty(expansion_count, node, populate_node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[node.name] local result = not should_halt and node.nodes and not node.open and not should_exclude - return result, expand_until_max_or_empty + return result end -- luacheck: pop @@ -58,8 +58,7 @@ local function gen_iterator(should_expand_fn) Iterator.builder({ parent }) :hidden() :applier(function(node) - local should_expand, should_expand_next_fn = should_expand_fn(expansion_count, node, populate_node) - should_expand_fn = should_expand_next_fn + local should_expand = should_expand_fn(expansion_count, node, populate_node) if should_expand then expansion_count = expansion_count + 1 populate_node(node) @@ -68,7 +67,8 @@ local function gen_iterator(should_expand_fn) end end) :recursor(function(node) - return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)) + local should_recurse = should_expand_fn(expansion_count - 1, node, populate_node) + return expansion_count < M.MAX_FOLDER_DISCOVERY and (should_recurse and node.open and node.nodes) end) :iterate() diff --git a/lua/nvim-tree/explorer/node.lua b/lua/nvim-tree/explorer/node.lua index 16e149f72f8..de44ba293d8 100644 --- a/lua/nvim-tree/explorer/node.lua +++ b/lua/nvim-tree/explorer/node.lua @@ -36,7 +36,7 @@ end ---@param node Node ---@return boolean function M.has_one_child_folder(node) - return #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R") or false + return node.nodes ~= nil and #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R") or false end ---@param node Node From 7bda685edd9467560b3d3825d928b62554d81cf3 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Tue, 3 Sep 2024 09:07:18 +0200 Subject: [PATCH 10/16] Cleanup code from redundant stuff --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index f76b2027901..f317d59b009 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -3,7 +3,6 @@ local renderer = require "nvim-tree.renderer" local Iterator = require "nvim-tree.iterators.node-iterator" local notify = require "nvim-tree.notify" local lib = require "nvim-tree.lib" -local git = require "nvim-tree.git" local M = {} @@ -30,23 +29,19 @@ local function populate_node(node) if not handle then return end - local status = git.load_project_status(cwd) - core.get_explorer():expand(node, status) + core.get_explorer():expand(node) end end ---@param expansion_count integer ---@param node Node ----@param populate_node function ---@return boolean --- luacheck: push ignore populate_node -local function expand_until_max_or_empty(expansion_count, node, populate_node) +local function expand_until_max_or_empty(expansion_count, node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[node.name] local result = not should_halt and node.nodes and not node.open and not should_exclude return result end --- luacheck: pop local function gen_iterator(should_expand_fn) local expansion_count = 0 From 06aad161895be961f9c413343da87a1b697850a0 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Tue, 3 Sep 2024 09:35:09 +0200 Subject: [PATCH 11/16] Add documentation --- doc/nvim-tree-lua.txt | 21 ++++++++++++++++++- .../actions/tree/modifiers/expand-all.lua | 8 +++---- lua/nvim-tree/api.lua | 5 +++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 273d34e3acb..2b2dd7cd07d 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1808,10 +1808,29 @@ tree.collapse_all({keep_buffers}) *nvim-tree-api.tree.collapse_all()* Parameters: ~ • {keep_buffers} (boolean) do not collapse nodes with open buffers. -tree.expand_all() *nvim-tree-api.tree.expand_all()* +tree.expand_all({opts}) *nvim-tree-api.tree.expand_all()* Recursively expand all nodes in the tree. Folder: only the nodes underneath that folder. + Parameters: ~ + • {opts} (table) optional parameters + + Options: ~ + • {expand_until} (function) A function returning boolean that + specifies terminating condition for node/tree expansion. If not + provided the function that expands recursively entire node/tree will be + used. + Example: > + + -- expand only 5 levels deep + local function my_expand(expansion_count, node) + local should_halt = expansion_count >= 5 + return not should_halt + end + + -- on_attach + vim.keymap.set('n', 'Z', api.tree.expand_all(node, { expand_until = my_expand })) +< *nvim-tree-api.tree.toggle_enable_filters()* tree.toggle_enable_filters() Toggle |nvim-tree.filters.enable| all filters. diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index f317d59b009..c2e1281e9ef 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -39,8 +39,7 @@ end local function expand_until_max_or_empty(expansion_count, node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[node.name] - local result = not should_halt and node.nodes and not node.open and not should_exclude - return result + return not should_halt and node.nodes and not node.open and not should_exclude end local function gen_iterator(should_expand_fn) @@ -74,8 +73,9 @@ local function gen_iterator(should_expand_fn) end ---@param base_node table -function M.fn(base_node, expand_until) - expand_until = expand_until or expand_until_max_or_empty +---@param expand_opts ApiTreeExpandAllOpts|nil +function M.fn(base_node, expand_opts) + local expand_until = (expand_opts and expand_opts.expand_until) or expand_until_max_or_empty local node = base_node.nodes and base_node or core.get_explorer() if gen_iterator(expand_until)(node) then notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 815c1dd7794..0a3f61dd54b 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -144,7 +144,12 @@ Api.tree.get_nodes = wrap(lib.get_nodes) Api.tree.find_file = wrap(actions.tree.find_file.fn) Api.tree.search_node = wrap(actions.finders.search_node.fn) Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn) + +---@class ApiTreeExpandAllOpts +---@field expand_until function|nil + Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn) + Api.tree.toggle_enable_filters = wrap(actions.tree.modifiers.toggles.enable) Api.tree.toggle_gitignore_filter = wrap(actions.tree.modifiers.toggles.git_ignored) Api.tree.toggle_git_clean_filter = wrap(actions.tree.modifiers.toggles.git_clean) From 57af3c4b39d304151761b61fad250ae3b8afe898 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Tue, 3 Sep 2024 09:43:11 +0200 Subject: [PATCH 12/16] Try to minimize the changes --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index c2e1281e9ef..5e9b56a856d 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -42,7 +42,7 @@ local function expand_until_max_or_empty(expansion_count, node) return not should_halt and node.nodes and not node.open and not should_exclude end -local function gen_iterator(should_expand_fn) +local function gen_iterator(should_expand) local expansion_count = 0 return function(parent) @@ -52,8 +52,7 @@ local function gen_iterator(should_expand_fn) Iterator.builder({ parent }) :hidden() :applier(function(node) - local should_expand = should_expand_fn(expansion_count, node, populate_node) - if should_expand then + if should_expand(expansion_count, node, populate_node) then expansion_count = expansion_count + 1 populate_node(node) node = lib.get_last_group_node(node) @@ -61,7 +60,7 @@ local function gen_iterator(should_expand_fn) end end) :recursor(function(node) - local should_recurse = should_expand_fn(expansion_count - 1, node, populate_node) + local should_recurse = should_expand(expansion_count - 1, node, populate_node) return expansion_count < M.MAX_FOLDER_DISCOVERY and (should_recurse and node.open and node.nodes) end) :iterate() From 38d9e074336e36e507d98023eea1d956cd13dcbc Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Tue, 3 Sep 2024 11:33:30 +0200 Subject: [PATCH 13/16] Minimize amount of changes further --- .../actions/tree/modifiers/expand-all.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index 5e9b56a856d..e031e818029 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -44,19 +44,24 @@ end local function gen_iterator(should_expand) local expansion_count = 0 + local function expand(node) + populate_node(node) + node = lib.get_last_group_node(node) + node.open = true + end return function(parent) - populate_node(parent) - parent.open = true + if parent.parent and parent.nodes and not parent.open then + expansion_count = expansion_count + 1 + expand(parent) + end - Iterator.builder({ parent }) + Iterator.builder(parent.nodes) :hidden() :applier(function(node) if should_expand(expansion_count, node, populate_node) then expansion_count = expansion_count + 1 - populate_node(node) - node = lib.get_last_group_node(node) - node.open = true + expand(node) end end) :recursor(function(node) From b506e05cd7394c6f4711df03067befd008d579bf Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Tue, 3 Sep 2024 11:38:31 +0200 Subject: [PATCH 14/16] Remove redundant condition --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index e031e818029..ed6fe8efc35 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -66,7 +66,7 @@ local function gen_iterator(should_expand) end) :recursor(function(node) local should_recurse = should_expand(expansion_count - 1, node, populate_node) - return expansion_count < M.MAX_FOLDER_DISCOVERY and (should_recurse and node.open and node.nodes) + return expansion_count < M.MAX_FOLDER_DISCOVERY and should_recurse and node.nodes end) :iterate() From b67209a2ce363ad91e5198f3d20eee573eeee98a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 7 Sep 2024 15:01:55 +1000 Subject: [PATCH 15/16] luals types for expand_until, rename variable for consistency --- doc/nvim-tree-lua.txt | 3 ++- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 7 ++++--- lua/nvim-tree/api.lua | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 2b2dd7cd07d..0bb324cdf90 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1816,7 +1816,8 @@ tree.expand_all({opts}) *nvim-tree-api.tree.expand_all()* • {opts} (table) optional parameters Options: ~ - • {expand_until} (function) A function returning boolean that + • {expand_until} (fun(expansion_count: integer, node: Node): boolean) + A function returning boolean that specifies terminating condition for node/tree expansion. If not provided the function that expands recursively entire node/tree will be used. diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index ed6fe8efc35..358771284ab 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -42,7 +42,8 @@ local function expand_until_max_or_empty(expansion_count, node) return not should_halt and node.nodes and not node.open and not should_exclude end -local function gen_iterator(should_expand) +---@param expand_until fun(expansion_count: integer, node: Node): boolean +local function gen_iterator(expand_until) local expansion_count = 0 local function expand(node) populate_node(node) @@ -59,13 +60,13 @@ local function gen_iterator(should_expand) Iterator.builder(parent.nodes) :hidden() :applier(function(node) - if should_expand(expansion_count, node, populate_node) then + if expand_until(expansion_count, node, populate_node) then expansion_count = expansion_count + 1 expand(node) end end) :recursor(function(node) - local should_recurse = should_expand(expansion_count - 1, node, populate_node) + local should_recurse = expand_until(expansion_count - 1, node, populate_node) return expansion_count < M.MAX_FOLDER_DISCOVERY and should_recurse and node.nodes end) :iterate() diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 0a3f61dd54b..723337da1d0 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -146,7 +146,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn) ---@class ApiTreeExpandAllOpts ----@field expand_until function|nil +---@field expand_until (fun(expansion_count: integer, node: Node): boolean)|nil Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn) From 3c093d4b72f7f084abc6611aa12c77b7d020b9b0 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 7 Sep 2024 15:10:59 +1000 Subject: [PATCH 16/16] doc format and example clarification --- doc/nvim-tree-lua.txt | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 0bb324cdf90..c247277f3e2 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1817,20 +1817,25 @@ tree.expand_all({opts}) *nvim-tree-api.tree.expand_all()* Options: ~ • {expand_until} (fun(expansion_count: integer, node: Node): boolean) - A function returning boolean that - specifies terminating condition for node/tree expansion. If not - provided the function that expands recursively entire node/tree will be - used. - Example: > - -- expand only 5 levels deep - local function my_expand(expansion_count, node) - local should_halt = expansion_count >= 5 - return not should_halt - end + A function returning boolean that specifies terminating condition + for node/tree expansion. If not provided the function that expands + recursively entire node/tree will be used. - -- on_attach - vim.keymap.set('n', 'Z', api.tree.expand_all(node, { expand_until = my_expand })) + Example: > + -- expand only 5 levels deep + local function my_expand_until(expansion_count, node) + print("my_expand_until " .. expansion_count .. " " .. tostring(node and node.absolute_path)) + local should_halt = expansion_count >= 5 + return not should_halt + end + + local function my_expand_all() + api.tree.expand_all(nil, { expand_until = my_expand_until }) + end + + -- on_attach + vim.keymap.set("n", "Z", my_expand_all, opts("My Expand All")) < *nvim-tree-api.tree.toggle_enable_filters()* tree.toggle_enable_filters()