refacto: simplify interface of reload and explore

also make common group empty check in explorer utils
This commit is contained in:
kiyan
2022-02-20 15:08:36 +01:00
parent 41f51508e3
commit 527d88d54e
6 changed files with 32 additions and 25 deletions

View File

@@ -41,7 +41,7 @@ function M.fn(fname)
if #node.nodes == 0 then if #node.nodes == 0 then
local git_finished = false local git_finished = false
git.load_project_status(node.absolute_path, function(status) 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 git_finished = true
end) end)
while not vim.wait(10, function() return git_finished end, 10) do end while not vim.wait(10, function() return git_finished end, 10) do end

View File

@@ -7,9 +7,9 @@ local explorer_module = require'nvim-tree.explorer'
local M = {} local M = {}
local function refresh_nodes(node, projects) 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) 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 for _, _node in ipairs(node.nodes) do
if _node.nodes and _node.open then if _node.nodes and _node.open then
refresh_nodes(_node, projects) refresh_nodes(_node, projects)

View File

@@ -7,7 +7,8 @@ local builders = require'nvim-tree.explorer.node-builders'
local M = {} 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) local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then if type(handle) == 'string' then
api.nvim_err_writeln(handle) api.nvim_err_writeln(handle)
@@ -37,14 +38,12 @@ function M.explore(node, cwd, status)
end end
local is_root = node.cwd ~= nil 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_folder_only = eutils.has_one_child_folder(node) and node.nodes[1]
local child_node = node.nodes[1] if vim.g.nvim_tree_group_empty == 1 and not is_root and child_folder_only then
if child_node.nodes and uv.fs_access(child_node.absolute_path, 'R') then node.group_next = child_folder_only
node.group_next = child_node local ns = M.explore(child_folder_only, status)
local ns = M.explore(child_node, child_node.absolute_path, status) node.nodes = ns or {}
node.nodes = ns or {} return ns
return ns
end
end end
utils.merge_sort(node.nodes, eutils.node_comparator) utils.merge_sort(node.nodes, eutils.node_comparator)

View File

@@ -19,9 +19,10 @@ function Explorer.new(cwd)
}, Explorer) }, Explorer)
end 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) 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 if type(self.init_cb) == "function" then
self.init_cb(self) self.init_cb(self)
self.init_cb = nil self.init_cb = nil
@@ -31,12 +32,12 @@ end
function Explorer:expand(node) function Explorer:expand(node)
self.init_cb = renderer.draw self.init_cb = renderer.draw
self:_load(node.link_to or node.absolute_path, node) self:_load(node)
end end
function Explorer:init(f) function Explorer:init(f)
self.init_cb = f self.init_cb = f
self:_load(self.cwd, self) self:_load(self)
end end
function M.setup(opts) function M.setup(opts)

View File

@@ -28,7 +28,8 @@ local function update_status(nodes_by_path, node_ignored, status)
end end
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) local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then if type(handle) == 'string' then
api.nvim_err_writeln(handle) api.nvim_err_writeln(handle)
@@ -75,14 +76,12 @@ function M.reload(node, cwd, status)
)) ))
local is_root = node.cwd ~= nil 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_folder_only = eutils.has_one_child_folder(node) and node.nodes[1]
local child_node = node.nodes[1] if vim.g.nvim_tree_group_empty == 1 and not is_root and child_folder_only then
if child_node.nodes and uv.fs_access(child_node.absolute_path, 'R') then node.group_next = child_folder_only
node.group_next = child_node local ns = M.reload(child_folder_only, status)
local ns = M.reload(child_node, child_node.absolute_path, status) node.nodes = ns or {}
node.nodes = ns or {} return ns
return ns
end
end end
utils.merge_sort(node.nodes, eutils.node_comparator) utils.merge_sort(node.nodes, eutils.node_comparator)

View File

@@ -1,3 +1,5 @@
local uv = vim.loop
local utils = require'nvim-tree.utils' local utils = require'nvim-tree.utils'
local M = { local M = {
@@ -60,6 +62,12 @@ function M.should_ignore_git(path, status)
and (M.config.filter_git_ignored and status and status[path] == '!!') and (M.config.filter_git_ignored and status and status[path] == '!!')
end 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) function M.setup(opts)
M.config = { M.config = {
filter_ignored = true, filter_ignored = true,