chore: simplify reloader and start fixing group nodes refresh

This commit is contained in:
kiyan 2022-02-06 22:59:37 +01:00
parent 7d33b0f74a
commit 7fec0f658b
3 changed files with 36 additions and 31 deletions

View File

@ -11,7 +11,7 @@ end
local function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd)
explorer_module.reload(node.nodes, node.absolute_path or node.cwd, node, projects[project_root] or {})
explorer_module.reload(node, node.absolute_path or node.cwd, projects[project_root] or {})
for _, _node in ipairs(node.nodes) do
if _node.nodes and _node.open then
refresh_nodes(_node, projects)

View File

@ -40,14 +40,14 @@ function M.explore(node, cwd, status)
local child_node = node.nodes[1]
if #(node.nodes) == 1 and child_node.nodes and uv.fs_access(child_node.absolute_path, 'R') then
node.group_next = child_node
child_node.git_status = node.git_status
node.nodes = {}
M.explore(child_node, child_node.absolute_path, status)
return
local ns = M.explore(child_node, child_node.absolute_path, status)
node.nodes = ns or {}
return ns
end
end
utils.merge_sort(node.nodes, eutils.node_comparator)
return node.nodes
end
return M

View File

@ -7,7 +7,7 @@ local builders = require'nvim-tree.explorer.node-builders'
local M = {}
function M.reload(nodes, cwd, parent_node, status)
function M.reload(node, cwd, status)
local handle = luv.fs_scandir(cwd)
if type(handle) == 'string' then
api.nvim_err_writeln(handle)
@ -17,13 +17,13 @@ function M.reload(nodes, cwd, parent_node, status)
local named_nodes = {}
local cached_nodes = {}
local nodes_idx = {}
for i, node in ipairs(nodes) do
node.git_status = (parent_node and parent_node.git_status == '!!' and '!!')
or (status.files and status.files[node.absolute_path])
or (status.dirs and status.dirs[node.absolute_path])
cached_nodes[i] = node.name
nodes_idx[node.name] = i
named_nodes[node.name] = node
for i, child in ipairs(node.nodes) do
child.git_status = (node and node.git_status == '!!' and '!!')
or (status.files and status.files[child.absolute_path])
or (status.dirs and status.dirs[child.absolute_path])
cached_nodes[i] = child.name
nodes_idx[child.name] = i
named_nodes[child.name] = child
end
local dirs = {}
@ -58,34 +58,37 @@ function M.reload(nodes, cwd, parent_node, status)
end
-- Handle grouped dirs
local next_node = parent_node.group_next
if next_node then
next_node.open = parent_node.open
if num_new_nodes ~= 1 or not new_nodes[next_node.name] then
local child_node = node.group_next
if child_node then
child_node.open = true
if num_new_nodes ~= 1 or not new_nodes[child_node.name] then
-- dir is no longer only containing a group dir, or group dir has been removed
-- either way: sever the group link on current dir
parent_node.group_next = nil
named_nodes[next_node.name] = next_node
node.nodes = node.group_next
node.group_next = nil
named_nodes[child_node.name] = child_node
else
M.reload(nodes, next_node.absolute_path, next_node, status)
return
node.group_next = child_node
local ns = M.reload(child_node, child_node.absolute_path, status)
node.nodes = ns or {}
return ns
end
end
local idx = 1
for _, name in ipairs(cached_nodes) do
local node = named_nodes[name]
if node and node.link_to then
local named_node = named_nodes[name]
if named_node and named_node.link_to then
-- If the link has been modified: remove it in case the link target has changed.
local stat = luv.fs_stat(node.absolute_path)
if stat and node.last_modified ~= stat.mtime.sec then
if stat and named_node.last_modified ~= stat.mtime.sec then
new_nodes[name] = nil
named_nodes[name] = nil
end
end
if not new_nodes[name] then
table.remove(nodes, idx)
table.remove(node.nodes, idx)
else
idx = idx + 1
end
@ -100,7 +103,7 @@ function M.reload(nodes, cwd, parent_node, status)
local prev = nil
local change_prev
local new_nodes_added = false
local parent_ignored = parent_node.git_status == '!!'
local parent_ignored = node.git_status == '!!'
for _, e in ipairs(all) do
for _, name in ipairs(e.nodes) do
change_prev = true
@ -113,26 +116,28 @@ function M.reload(nodes, cwd, parent_node, status)
if prev then
idx = nodes_idx[prev] + 1
end
table.insert(nodes, idx, n)
table.insert(node.nodes, idx, n)
nodes_idx[name] = idx
cached_nodes[idx] = name
else
change_prev = false
end
end
if change_prev and not (next_node and next_node.name == name) then
if change_prev and not (child_node and child_node.name == name) then
prev = name
end
end
end
if next_node then
table.insert(nodes, 1, next_node)
if child_node then
table.insert(node.nodes, 1, child_node)
end
if new_nodes_added then
utils.merge_sort(nodes, eutils.node_comparator)
utils.merge_sort(node.nodes, eutils.node_comparator)
end
return node.nodes
end
return M