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 function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd) 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 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

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

View File

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