diff --git a/lua/nvim-tree/actions/find-file.lua b/lua/nvim-tree/actions/find-file.lua index f2d7571e..84db41ba 100644 --- a/lua/nvim-tree/actions/find-file.lua +++ b/lua/nvim-tree/actions/find-file.lua @@ -41,7 +41,7 @@ function M.fn(fname) if #node.nodes == 0 then local git_finished = false git.load_project_status(node.absolute_path, function(status) - explorer_module.explore(node, node.absolute_path, status) + explorer_module.explore(node, status) git_finished = true end) while not vim.wait(10, function() return git_finished end, 10) do end diff --git a/lua/nvim-tree/actions/reloaders.lua b/lua/nvim-tree/actions/reloaders.lua index bc15b04a..9ff05b3d 100644 --- a/lua/nvim-tree/actions/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders.lua @@ -7,9 +7,9 @@ local explorer_module = require'nvim-tree.explorer' local M = {} local function refresh_nodes(node, projects) - local cwd = node.absolute_path or node.cwd + local cwd = node.cwd or node.link_to or node.absolute_path local project_root = git.get_project_root(cwd) - explorer_module.reload(node, cwd, projects[project_root] or {}) + explorer_module.reload(node, projects[project_root] or {}) for _, _node in ipairs(node.nodes) do if _node.nodes and _node.open then refresh_nodes(_node, projects) diff --git a/lua/nvim-tree/explorer/explore.lua b/lua/nvim-tree/explorer/explore.lua index d008aa8d..7de1bfad 100644 --- a/lua/nvim-tree/explorer/explore.lua +++ b/lua/nvim-tree/explorer/explore.lua @@ -7,7 +7,8 @@ local builders = require'nvim-tree.explorer.node-builders' local M = {} -function M.explore(node, cwd, status) +function M.explore(node, status) + local cwd = node.cwd or node.link_to or node.absolute_path local handle = uv.fs_scandir(cwd) if type(handle) == 'string' then api.nvim_err_writeln(handle) @@ -37,14 +38,12 @@ function M.explore(node, cwd, status) end local is_root = node.cwd ~= nil - if vim.g.nvim_tree_group_empty == 1 and not is_root and #(node.nodes) == 1 then - local child_node = node.nodes[1] - if child_node.nodes and uv.fs_access(child_node.absolute_path, 'R') then - node.group_next = child_node - local ns = M.explore(child_node, child_node.absolute_path, status) - node.nodes = ns or {} - return ns - end + local child_folder_only = eutils.has_one_child_folder(node) and node.nodes[1] + if vim.g.nvim_tree_group_empty == 1 and not is_root and child_folder_only then + node.group_next = child_folder_only + local ns = M.explore(child_folder_only, status) + node.nodes = ns or {} + return ns end utils.merge_sort(node.nodes, eutils.node_comparator) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 7f8912fd..710e86aa 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -19,9 +19,10 @@ function Explorer.new(cwd) }, Explorer) end -function Explorer:_load(cwd, node) +function Explorer:_load(node) + local cwd = node.cwd or node.link_to or node.absolute_path git.load_project_status(cwd, function(git_statuses) - M.explore(node, cwd, git_statuses) + M.explore(node, git_statuses) if type(self.init_cb) == "function" then self.init_cb(self) self.init_cb = nil @@ -31,12 +32,12 @@ end function Explorer:expand(node) self.init_cb = renderer.draw - self:_load(node.link_to or node.absolute_path, node) + self:_load(node) end function Explorer:init(f) self.init_cb = f - self:_load(self.cwd, self) + self:_load(self) end function M.setup(opts) diff --git a/lua/nvim-tree/explorer/reload.lua b/lua/nvim-tree/explorer/reload.lua index 44f83c0f..b3a58ed7 100644 --- a/lua/nvim-tree/explorer/reload.lua +++ b/lua/nvim-tree/explorer/reload.lua @@ -28,7 +28,8 @@ local function update_status(nodes_by_path, node_ignored, status) end end -function M.reload(node, cwd, status) +function M.reload(node, status) + local cwd = node.cwd or node.link_to or node.absolute_path local handle = uv.fs_scandir(cwd) if type(handle) == 'string' then api.nvim_err_writeln(handle) @@ -75,14 +76,12 @@ function M.reload(node, cwd, status) )) local is_root = node.cwd ~= nil - if vim.g.nvim_tree_group_empty == 1 and not is_root and #(node.nodes) == 1 then - local child_node = node.nodes[1] - if child_node.nodes and uv.fs_access(child_node.absolute_path, 'R') then - node.group_next = child_node - local ns = M.reload(child_node, child_node.absolute_path, status) - node.nodes = ns or {} - return ns - end + local child_folder_only = eutils.has_one_child_folder(node) and node.nodes[1] + if vim.g.nvim_tree_group_empty == 1 and not is_root and child_folder_only then + node.group_next = child_folder_only + local ns = M.reload(child_folder_only, status) + node.nodes = ns or {} + return ns end utils.merge_sort(node.nodes, eutils.node_comparator) diff --git a/lua/nvim-tree/explorer/utils.lua b/lua/nvim-tree/explorer/utils.lua index d604517c..e02d853e 100644 --- a/lua/nvim-tree/explorer/utils.lua +++ b/lua/nvim-tree/explorer/utils.lua @@ -1,3 +1,5 @@ +local uv = vim.loop + local utils = require'nvim-tree.utils' local M = { @@ -60,6 +62,12 @@ function M.should_ignore_git(path, status) and (M.config.filter_git_ignored and status and status[path] == '!!') end +function M.has_one_child_folder(node) + return #node.nodes == 1 + and node.nodes[1].nodes + and uv.fs_access(node.nodes[1].absolute_path, 'R') +end + function M.setup(opts) M.config = { filter_ignored = true,