From c1948cbce0bc828d5e1b40b73ddb47a4eb19dead Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 20 Jul 2025 21:07:49 +0200 Subject: [PATCH 01/20] feat: Allow to expand nodes until certain condition is met --- .../actions/tree/modifiers/expand.lua | 52 +++++++--- lua/nvim-tree/api.lua | 95 +++++++++++-------- 2 files changed, 97 insertions(+), 50 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 385ff72b6c5..f8ba12ae604 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -30,17 +30,30 @@ end ---@param expansion_count integer ---@param node Node ---@return boolean -local function should_expand(expansion_count, node) +local function descend_until_max_or_empty(expansion_count, node) local dir = node:as(DirectoryNode) if not dir then return false end + local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[dir.name] - return not should_halt and not dir.open and not should_exclude + + return not should_halt and not should_exclude +end + +---@param expansion_count integer +---@param node Node +---@param should_descend fun(expansion_count: integer, node: Node): boolean +---@return boolean +local function should_expand(expansion_count, node, should_descend) + local dir = node:as(DirectoryNode) + return dir and not dir.open and should_descend(expansion_count, node) end -local function gen_iterator() + +---@param should_descend fun(expansion_count: integer, node: Node): boolean +local function gen_iterator(should_descend) local expansion_count = 0 return function(parent) @@ -52,7 +65,7 @@ local function gen_iterator() Iterator.builder(parent.nodes) :hidden() :applier(function(node) - if should_expand(expansion_count, node) then + if should_expand(expansion_count, node, should_descend) then expansion_count = expansion_count + 1 node = node:as(DirectoryNode) if node then @@ -61,7 +74,19 @@ local function gen_iterator() 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)) + if not should_descend(expansion_count, node) then + return nil + end + + if node.group_next then + return { node.group_next } + end + + if node.open and node.nodes then + return node.nodes + end + + return nil end) :iterate() @@ -72,12 +97,13 @@ local function gen_iterator() end ---@param node Node? -local function expand_node(node) +---@param expand_opts ApiTreeExpandAllOpts? +local function expand_node(node, expand_opts) if not node then return end - - if gen_iterator()(node) then + local descend_until = (expand_opts and expand_opts.descend_until) or descend_until_max_or_empty + if gen_iterator(descend_until)(node) then notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") end @@ -89,18 +115,20 @@ end ---Expand the directory node or the root ---@param node Node -function M.all(node) - expand_node(node and node:as(DirectoryNode) or core.get_explorer()) +---@param expand_opts ApiTreeExpandAllOpts? +function M.all(node, expand_opts) + expand_node(node and node:as(DirectoryNode) or core.get_explorer(), expand_opts) end ---Expand the directory node or parent node ---@param node Node -function M.node(node) +---@param expand_opts ApiTreeExpandAllOpts? +function M.node(node, expand_opts) if not node then return end - expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode)) + expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode), expand_opts) end function M.setup(opts) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index c75115c6c62..f379f6ce965 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -121,6 +121,45 @@ local function wrap_explorer_member(explorer_member, member_method) end) end +--- +---@class NodeEditOpts +---@field quit_on_open boolean|nil default false +---@field focus boolean|nil default true + +---@param mode string +---@param node Node +---@param edit_opts NodeEditOpts? +local function edit(mode, node, edit_opts) + local file_link = node:as(FileLinkNode) + local path = file_link and file_link.link_to or node.absolute_path + local cur_tabpage = vim.api.nvim_get_current_tabpage() + + local explorer = core.get_explorer() + + actions.node.open_file.fn(mode, path) + + edit_opts = edit_opts or {} + + local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" + if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then + if explorer then + explorer.view:close(cur_tabpage, "api.edit " .. mode) + end + end + + local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" + local focus = edit_opts.focus == nil or edit_opts.focus == true + if not mode_unsupported_focus and not focus then + -- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab + if mode == "tabnew" then + vim.cmd(":tabprev") + end + if explorer then + explorer.view:focus() + end + end +end + ---@class ApiTreeOpenOpts ---@field path string|nil path ---@field current_window boolean|nil default false @@ -186,7 +225,25 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) ---@field keep_buffers boolean|nil default false Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) + +---@class ApiTreeExpandAllOpts +---@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil + Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) + +Api.tree.toggle_descend_until = wrap_node(function(node, descend_until) + if node.open then + local dir = node:as(DirectoryNode) + dir:expand_or_collapse("edit") + else + if node.nodes then + actions.tree.modifiers.expand.all(node, { descend_until = descend_until }) + else + edit("edit", node) + end + end +end) + Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle") Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored") Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean") @@ -225,44 +282,6 @@ Api.fs.copy.absolute_path = wrap_node(wrap_explorer_member("clipboard", "copy_ab Api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filename")) Api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename")) Api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path")) ---- ----@class NodeEditOpts ----@field quit_on_open boolean|nil default false ----@field focus boolean|nil default true - ----@param mode string ----@param node Node ----@param edit_opts NodeEditOpts? -local function edit(mode, node, edit_opts) - local file_link = node:as(FileLinkNode) - local path = file_link and file_link.link_to or node.absolute_path - local cur_tabpage = vim.api.nvim_get_current_tabpage() - - local explorer = core.get_explorer() - - actions.node.open_file.fn(mode, path) - - edit_opts = edit_opts or {} - - local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" - if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then - if explorer then - explorer.view:close(cur_tabpage, "api.edit " .. mode) - end - end - - local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" - local focus = edit_opts.focus == nil or edit_opts.focus == true - if not mode_unsupported_focus and not focus then - -- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab - if mode == "tabnew" then - vim.cmd(":tabprev") - end - if explorer then - explorer.view:focus() - end - end -end ---@param mode string ---@param toggle_group boolean? From 641f125f6bd7185816478d83bd2e4df3ad42937c Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 20 Jul 2025 21:24:29 +0200 Subject: [PATCH 02/20] Fix warnings --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 7 ++++++- lua/nvim-tree/api.lua | 14 +++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index f8ba12ae604..06d071c6ecc 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -48,11 +48,16 @@ end ---@return boolean local function should_expand(expansion_count, node, should_descend) local dir = node:as(DirectoryNode) - return dir and not dir.open and should_descend(expansion_count, node) + if not dir then + return false + end + + return not dir.open and should_descend(expansion_count, node) end ---@param should_descend fun(expansion_count: integer, node: Node): boolean +---@return fun(node): any local function gen_iterator(should_descend) local expansion_count = 0 diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index f379f6ce965..190722d0b46 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -232,15 +232,15 @@ Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) Api.tree.toggle_descend_until = wrap_node(function(node, descend_until) - if node.open then - local dir = node:as(DirectoryNode) - dir:expand_or_collapse("edit") - else - if node.nodes then - actions.tree.modifiers.expand.all(node, { descend_until = descend_until }) + local dir = node:as(DirectoryNode) + if dir then + if node.open then + dir:expand_or_collapse(nil) else - edit("edit", node) + actions.tree.modifiers.expand.all(node, { descend_until = descend_until }) end + else + edit("edit", node) end end) From 72404b9f52ac12c0b6e1266ce128e41b809d9741 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 14:40:39 +0200 Subject: [PATCH 03/20] Restore original position of edit function --- lua/nvim-tree/api.lua | 105 +++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 190722d0b46..04f2fc71af8 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -121,45 +121,6 @@ local function wrap_explorer_member(explorer_member, member_method) end) end ---- ----@class NodeEditOpts ----@field quit_on_open boolean|nil default false ----@field focus boolean|nil default true - ----@param mode string ----@param node Node ----@param edit_opts NodeEditOpts? -local function edit(mode, node, edit_opts) - local file_link = node:as(FileLinkNode) - local path = file_link and file_link.link_to or node.absolute_path - local cur_tabpage = vim.api.nvim_get_current_tabpage() - - local explorer = core.get_explorer() - - actions.node.open_file.fn(mode, path) - - edit_opts = edit_opts or {} - - local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" - if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then - if explorer then - explorer.view:close(cur_tabpage, "api.edit " .. mode) - end - end - - local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" - local focus = edit_opts.focus == nil or edit_opts.focus == true - if not mode_unsupported_focus and not focus then - -- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab - if mode == "tabnew" then - vim.cmd(":tabprev") - end - if explorer then - explorer.view:focus() - end - end -end - ---@class ApiTreeOpenOpts ---@field path string|nil path ---@field current_window boolean|nil default false @@ -230,20 +191,6 @@ Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) ---@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) - -Api.tree.toggle_descend_until = wrap_node(function(node, descend_until) - local dir = node:as(DirectoryNode) - if dir then - if node.open then - dir:expand_or_collapse(nil) - else - actions.tree.modifiers.expand.all(node, { descend_until = descend_until }) - end - else - edit("edit", node) - end -end) - Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle") Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored") Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean") @@ -283,6 +230,58 @@ Api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filenam Api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename")) Api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path")) +--- +---@class NodeEditOpts +---@field quit_on_open boolean|nil default false +---@field focus boolean|nil default true + +---@param mode string +---@param node Node +---@param edit_opts NodeEditOpts? +local function edit(mode, node, edit_opts) + local file_link = node:as(FileLinkNode) + local path = file_link and file_link.link_to or node.absolute_path + local cur_tabpage = vim.api.nvim_get_current_tabpage() + + local explorer = core.get_explorer() + + actions.node.open_file.fn(mode, path) + + edit_opts = edit_opts or {} + + local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" + if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then + if explorer then + explorer.view:close(cur_tabpage, "api.edit " .. mode) + end + end + + local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" + local focus = edit_opts.focus == nil or edit_opts.focus == true + if not mode_unsupported_focus and not focus then + -- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab + if mode == "tabnew" then + vim.cmd(":tabprev") + end + if explorer then + explorer.view:focus() + end + end +end + +Api.tree.toggle_descend_until = wrap_node(function(node, descend_until) + local dir = node:as(DirectoryNode) + if dir then + if node.open then + dir:expand_or_collapse(nil) + else + actions.tree.modifiers.expand.all(node, { descend_until = descend_until }) + end + else + edit("edit", node) + end +end) + ---@param mode string ---@param toggle_group boolean? ---@return fun(node: Node, edit_opts: NodeEditOpts?) From 06575bce2b0c17a61729e77393bee17936996075 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 14:42:16 +0200 Subject: [PATCH 04/20] Rename field to match the api method name --- lua/nvim-tree/api.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 04f2fc71af8..f1745587656 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -188,7 +188,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) ---@class ApiTreeExpandAllOpts ----@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil +---@field expand_until (fun(expansion_count: integer, node: Node): boolean)|nil Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle") From 89364601a6adb98b12ea9301c10299ac5e6ed2c9 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 14:43:44 +0200 Subject: [PATCH 05/20] Rename ApiTreeExpandAllOpts to ApiTreeExpandOpts --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 6 +++--- lua/nvim-tree/api.lua | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 06d071c6ecc..1da0d234b87 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -102,7 +102,7 @@ local function gen_iterator(should_descend) end ---@param node Node? ----@param expand_opts ApiTreeExpandAllOpts? +---@param expand_opts ApiTreeExpandOpts? local function expand_node(node, expand_opts) if not node then return @@ -120,14 +120,14 @@ end ---Expand the directory node or the root ---@param node Node ----@param expand_opts ApiTreeExpandAllOpts? +---@param expand_opts ApiTreeExpandOpts? function M.all(node, expand_opts) expand_node(node and node:as(DirectoryNode) or core.get_explorer(), expand_opts) end ---Expand the directory node or parent node ---@param node Node ----@param expand_opts ApiTreeExpandAllOpts? +---@param expand_opts ApiTreeExpandOpts? function M.node(node, expand_opts) if not node then return diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index f1745587656..e17336f7821 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -187,7 +187,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) ----@class ApiTreeExpandAllOpts +---@class ApiTreeExpandOpts ---@field expand_until (fun(expansion_count: integer, node: Node): boolean)|nil Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) From 71271e3149f5163c7cd989e04e40ea3141245052 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 14:46:19 +0200 Subject: [PATCH 06/20] Remove toggle_descend_until --- lua/nvim-tree/api.lua | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 9896776d261..dac6ab2b19b 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -269,19 +269,6 @@ local function edit(mode, node, edit_opts) end end -Api.tree.toggle_descend_until = wrap_node(function(node, descend_until) - local dir = node:as(DirectoryNode) - if dir then - if node.open then - dir:expand_or_collapse(nil) - else - actions.tree.modifiers.expand.all(node, { descend_until = descend_until }) - end - else - edit("edit", node) - end -end) - ---@param mode string ---@param toggle_group boolean? ---@return fun(node: Node, edit_opts: NodeEditOpts?) From 03e3a155628d46b8e37417bc718b4b3f0944a367 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 14:49:47 +0200 Subject: [PATCH 07/20] Remove redundant empty line --- lua/nvim-tree/api.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index dac6ab2b19b..27f819ffe03 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -229,7 +229,6 @@ Api.fs.copy.absolute_path = wrap_node(wrap_explorer_member("clipboard", "copy_ab Api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filename")) Api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename")) Api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path")) - --- ---@class NodeEditOpts ---@field quit_on_open boolean|nil default false From 4ab16dd3a756d68bed1e4f64c5c96b7d50303a81 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 14:53:33 +0200 Subject: [PATCH 08/20] Update :help for changed methods --- doc/nvim-tree-lua.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index adf54f1c2b3..6761cb691c3 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1846,6 +1846,7 @@ tree.expand_all({node}) *nvim-tree-api.tree.expand_all()* Parameters: ~ • {node} (Node|nil) folder + • {expand_opts} (ApiTreeExpandOpts|nil) optional parameters *nvim-tree-api.tree.toggle_enable_filters()* tree.toggle_enable_filters() @@ -2285,6 +2286,7 @@ node.expand({node}) *nvim-tree-api.node.expand()* Parameters: ~ • {node} (Node|nil) file or folder + • {expand_opts} (ApiTreeExpandOpts|nil) optional parameters node.collapse({node}, {opts}) *nvim-tree-api.node.collapse()* Collapse the tree under a directory or a file's parent directory. From 3dac0be704d3edb1bc931251854a31674cf385e2 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 16:09:58 +0200 Subject: [PATCH 09/20] Fix partial expansion of grouped nodes --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 1da0d234b87..8b897251cf7 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -52,7 +52,15 @@ local function should_expand(expansion_count, node, should_descend) return false end - return not dir.open and should_descend(expansion_count, node) + if not dir.open and should_descend(expansion_count, node) then + core.get_explorer():expand(node) + if node.group_next then + return should_expand(expansion_count, node.group_next, should_descend) + else + return true + end + end + return false end From 160ae4fce6e91ac7b8d12417145478c19e3f042e Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 16:13:07 +0200 Subject: [PATCH 10/20] Fix lint error --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 12 ++++++------ lua/nvim-tree/api.lua | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 8b897251cf7..fcd1a4abcea 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -44,7 +44,7 @@ end ---@param expansion_count integer ---@param node Node ----@param should_descend fun(expansion_count: integer, node: Node): boolean +---@param should_descend fun(expansion_count: integer, node: DirectoryNode): boolean ---@return boolean local function should_expand(expansion_count, node, should_descend) local dir = node:as(DirectoryNode) @@ -52,10 +52,10 @@ local function should_expand(expansion_count, node, should_descend) return false end - if not dir.open and should_descend(expansion_count, node) then - core.get_explorer():expand(node) - if node.group_next then - return should_expand(expansion_count, node.group_next, should_descend) + if not dir.open and should_descend(expansion_count, dir) then + core.get_explorer():expand(dir) -- populate node.group_next + if dir.group_next then + return should_expand(expansion_count, dir.group_next, should_descend) else return true end @@ -64,7 +64,7 @@ local function should_expand(expansion_count, node, should_descend) end ----@param should_descend fun(expansion_count: integer, node: Node): boolean +---@param should_descend fun(expansion_count: integer, node: DirectoryNode): boolean ---@return fun(node): any local function gen_iterator(should_descend) local expansion_count = 0 diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 27f819ffe03..059101dfa6b 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -188,7 +188,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) ---@class ApiTreeExpandOpts ----@field expand_until (fun(expansion_count: integer, node: Node): boolean)|nil +---@field expand_until (fun(expansion_count: integer, node: DirectoryNode): boolean)|nil Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle") From 37b351691cd37ec8a79391de25d42c4ba1ffa63a Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 2 Aug 2025 16:18:41 +0200 Subject: [PATCH 11/20] Fix linting error --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 6 +++--- lua/nvim-tree/api.lua | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index fcd1a4abcea..16c9b9f8128 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -44,7 +44,7 @@ end ---@param expansion_count integer ---@param node Node ----@param should_descend fun(expansion_count: integer, node: DirectoryNode): boolean +---@param should_descend fun(expansion_count: integer, node: Node): boolean ---@return boolean local function should_expand(expansion_count, node, should_descend) local dir = node:as(DirectoryNode) @@ -52,7 +52,7 @@ local function should_expand(expansion_count, node, should_descend) return false end - if not dir.open and should_descend(expansion_count, dir) then + if not dir.open and should_descend(expansion_count, node) then core.get_explorer():expand(dir) -- populate node.group_next if dir.group_next then return should_expand(expansion_count, dir.group_next, should_descend) @@ -64,7 +64,7 @@ local function should_expand(expansion_count, node, should_descend) end ----@param should_descend fun(expansion_count: integer, node: DirectoryNode): boolean +---@param should_descend fun(expansion_count: integer, node: Node): boolean ---@return fun(node): any local function gen_iterator(should_descend) local expansion_count = 0 diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 059101dfa6b..27f819ffe03 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -188,7 +188,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) ---@class ApiTreeExpandOpts ----@field expand_until (fun(expansion_count: integer, node: DirectoryNode): boolean)|nil +---@field expand_until (fun(expansion_count: integer, node: Node): boolean)|nil Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle") From eabd08cf25b3eee9f391cc1c608e6ef6a48b7cc0 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 10 Aug 2025 17:11:03 +0200 Subject: [PATCH 12/20] Fix incorrect open/close indicator state --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 16c9b9f8128..d3c60f5f4a2 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -53,9 +53,16 @@ local function should_expand(expansion_count, node, should_descend) end if not dir.open and should_descend(expansion_count, node) then - core.get_explorer():expand(dir) -- populate node.group_next + if #node.nodes == 0 then + core.get_explorer():expand(node) -- populate node.group_next + end + if dir.group_next then - return should_expand(expansion_count, dir.group_next, should_descend) + local expand_next = should_expand(expansion_count, dir.group_next, should_descend) + if expand_next then + dir.open = true + end + return expand_next else return true end From 669a79f961d1a5d1e5ed12ed07da87fac697499c Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 10 Aug 2025 17:13:35 +0200 Subject: [PATCH 13/20] Update docs --- doc/nvim-tree-lua.txt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 6761cb691c3..857e9a98e88 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1841,12 +1841,17 @@ tree.collapse_all({opts}) *nvim-tree-api.tree.collapse_all()* Options: ~ • {keep_buffers} (boolean) do not collapse nodes with open buffers. -tree.expand_all({node}) *nvim-tree-api.tree.expand_all()* +tree.expand_all({node}, {opts}) *nvim-tree-api.tree.expand_all()* Recursively expand all nodes under the tree root or specified folder. Parameters: ~ • {node} (Node|nil) folder - • {expand_opts} (ApiTreeExpandOpts|nil) optional parameters + • {opts} (ApiTreeExpandOpts) optional parameters + + Options: ~ + • {expand_until} ((fun(expansion_count: integer, node: Node?): boolean)?) + Return true if {node} should be expanded. + {expansion_count} is the total number of folders expanded. *nvim-tree-api.tree.toggle_enable_filters()* tree.toggle_enable_filters() @@ -2280,13 +2285,18 @@ node.buffer.wipe({node}, {opts}) *nvim-tree-api.node.buffer.wipe()* Options: ~ • {force} (boolean) wipe even if buffer is modified, default false -node.expand({node}) *nvim-tree-api.node.expand()* +node.expand({node}, {opts}) *nvim-tree-api.node.expand()* Recursively expand all nodes under a directory or a file's parent directory. Parameters: ~ • {node} (Node|nil) file or folder - • {expand_opts} (ApiTreeExpandOpts|nil) optional parameters + • {opts} (ApiTreeExpandOpts) optional parameters + + Options: ~ + • {expand_until} ((fun(expansion_count: integer, node: Node?): boolean)?) + Return true if {node} should be expanded. + {expansion_count} is the total number of folders expanded. node.collapse({node}, {opts}) *nvim-tree-api.node.collapse()* Collapse the tree under a directory or a file's parent directory. From 8425ef03ceb32dc4cd616af22e4a5d84cffec5f0 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 10 Aug 2025 17:16:51 +0200 Subject: [PATCH 14/20] Rename descend_until option to expand_until --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index d3c60f5f4a2..9486a092639 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -122,7 +122,7 @@ local function expand_node(node, expand_opts) if not node then return end - local descend_until = (expand_opts and expand_opts.descend_until) or descend_until_max_or_empty + local descend_until = (expand_opts and expand_opts.expand_until) or descend_until_max_or_empty if gen_iterator(descend_until)(node) then notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") end From c4777ab3eb651b2bacd88a079b98508e263ba6ed Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 10 Aug 2025 17:25:52 +0200 Subject: [PATCH 15/20] Always check directory expansion limit --- .../actions/tree/modifiers/expand.lua | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 9486a092639..999e9c89f77 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -27,19 +27,30 @@ local function expand(node) end end +---@param should_descend fun(expansion_count: integer, node: Node): boolean +---@return fun(expansion_count: integer, node: Node): boolean +local function limit_folder_discovery(should_descend) + return function(expansion_count, node) + local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY + if should_halt then + return false + end + + return should_descend(expansion_count, node) + end +end + ---@param expansion_count integer ---@param node Node ---@return boolean -local function descend_until_max_or_empty(expansion_count, node) +local function descend_until_empty(_, node) local dir = node:as(DirectoryNode) if not dir then return false end - local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY local should_exclude = M.EXCLUDE[dir.name] - - return not should_halt and not should_exclude + return not should_exclude end ---@param expansion_count integer @@ -122,7 +133,7 @@ local function expand_node(node, expand_opts) if not node then return end - local descend_until = (expand_opts and expand_opts.expand_until) or descend_until_max_or_empty + local descend_until = limit_folder_discovery((expand_opts and expand_opts.expand_until) or descend_until_empty) if gen_iterator(descend_until)(node) then notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") end From cc256800b8eb02261f5c8ce9dfc5796d11f7c9e7 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 10 Aug 2025 17:27:43 +0200 Subject: [PATCH 16/20] Fix linter errors --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 999e9c89f77..1fed5325e97 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -43,7 +43,7 @@ end ---@param expansion_count integer ---@param node Node ---@return boolean -local function descend_until_empty(_, node) +local function descend_until_empty(expansion_count, node) local dir = node:as(DirectoryNode) if not dir then return false @@ -65,7 +65,7 @@ local function should_expand(expansion_count, node, should_descend) if not dir.open and should_descend(expansion_count, node) then if #node.nodes == 0 then - core.get_explorer():expand(node) -- populate node.group_next + core.get_explorer():expand(dir) -- populate node.group_next end if dir.group_next then From ba2d11fcd89f8bd9048b30377d244119f68eb1a5 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 10 Aug 2025 17:32:49 +0200 Subject: [PATCH 17/20] Ignore unused param warning --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 1fed5325e97..8af7f1c91b2 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -43,7 +43,8 @@ end ---@param expansion_count integer ---@param node Node ---@return boolean -local function descend_until_empty(expansion_count, node) +local function descend_until_empty(expansion_count, node) --luacheck: ignore 212 + local dir = node:as(DirectoryNode) if not dir then return false From 8ecfcd04b96b6303bbc692dce917b3e552c41ffd Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 11 Aug 2025 13:44:47 +1000 Subject: [PATCH 18/20] Apply suggestions from code review --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 8af7f1c91b2..473195cbdce 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -40,10 +40,10 @@ local function limit_folder_discovery(should_descend) end end ----@param expansion_count integer +---@param _ integer expansion_count ---@param node Node ---@return boolean -local function descend_until_empty(expansion_count, node) --luacheck: ignore 212 +local function descend_until_empty(_, node) local dir = node:as(DirectoryNode) if not dir then From 8c42c02706397651423a4f6dc4866d734b1be071 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 11 Aug 2025 13:46:19 +1000 Subject: [PATCH 19/20] simplify MAX_FOLDER_DISCOVERY warning --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 473195cbdce..5d28bdeaa61 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -33,6 +33,7 @@ local function limit_folder_discovery(should_descend) return function(expansion_count, node) local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY if should_halt then + notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") return false end @@ -121,10 +122,6 @@ local function gen_iterator(should_descend) return nil end) :iterate() - - if expansion_count >= M.MAX_FOLDER_DISCOVERY then - return true - end end end @@ -135,9 +132,7 @@ local function expand_node(node, expand_opts) return end local descend_until = limit_folder_discovery((expand_opts and expand_opts.expand_until) or descend_until_empty) - if gen_iterator(descend_until)(node) then - notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") - end + gen_iterator(descend_until)(node) local explorer = core.get_explorer() if explorer then From 3c666c4746115c0d51ed8183f8ba4d883f5a5ae1 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 11 Aug 2025 13:49:31 +1000 Subject: [PATCH 20/20] fix bad comment whitespace --- lua/nvim-tree/actions/tree/modifiers/expand.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua index 5d28bdeaa61..9b48bf1d1b7 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -41,7 +41,7 @@ local function limit_folder_discovery(should_descend) end end ----@param _ integer expansion_count +---@param _ integer expansion_count ---@param node Node ---@return boolean local function descend_until_empty(_, node)