fix: reload git status of existing nodes (#975)

This commit is contained in:
Kiyan 2022-02-13 13:44:09 +01:00 committed by GitHub
parent 95c331ce32
commit 3f4ed9b6c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 10 deletions

View File

@ -7,8 +7,9 @@ local explorer_module = require'nvim-tree.explorer'
local M = {}
local function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd)
explorer_module.reload(node, node.absolute_path or node.cwd, projects[project_root] or {})
local cwd = node.absolute_path or node.cwd
local project_root = git.get_project_root(cwd)
explorer_module.reload(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

@ -5,7 +5,7 @@ local M = {
is_windows = vim.fn.has('win32') == 1
}
local function get_dir_git_status(parent_ignored, status, absolute_path)
function M.get_dir_git_status(parent_ignored, status, absolute_path)
if parent_ignored then
return '!!'
end
@ -14,13 +14,17 @@ local function get_dir_git_status(parent_ignored, status, absolute_path)
return dir_status or file_status
end
function M.get_git_status(parent_ignored, status, absolute_path)
return parent_ignored and '!!' or status.files and status.files[absolute_path]
end
function M.folder(absolute_path, name, status, parent_ignored)
local handle = uv.fs_scandir(absolute_path)
local has_children = handle and uv.fs_scandir_next(handle) ~= nil
return {
absolute_path = absolute_path,
git_status = get_dir_git_status(parent_ignored, status, absolute_path),
git_status = M.get_dir_git_status(parent_ignored, status, absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node
has_children = has_children,
name = name,
@ -43,7 +47,7 @@ function M.file(absolute_path, name, status, parent_ignored)
absolute_path = absolute_path,
executable = is_executable(absolute_path, ext),
extension = ext,
git_status = parent_ignored and '!!' or status.files and status.files[absolute_path],
git_status = M.get_git_status(parent_ignored, status, absolute_path),
name = name,
}
end
@ -70,7 +74,7 @@ function M.link(absolute_path, name, status, parent_ignored)
return {
absolute_path = absolute_path,
git_status = parent_ignored and '!!' or status.files and status.files[absolute_path],
git_status = M.get_git_status(parent_ignored, status, absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node
last_modified = last_modified,
link_to = link_to,

View File

@ -15,6 +15,19 @@ local function key_by(nodes, key)
return v
end
local function update_status(nodes_by_path, node_ignored, status)
return function(node)
if nodes_by_path[node.absolute_path] then
if node.nodes then
node.git_status = builders.get_dir_git_status(node_ignored, status, node.absolute_path)
else
node.git_status = builders.get_git_status(node_ignored, status, node.absolute_path)
end
end
return node
end
end
function M.reload(node, cwd, status)
local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then
@ -54,10 +67,12 @@ function M.reload(node, cwd, status)
end
end
node.nodes = vim.tbl_filter(
function(n) return child_names[n.absolute_path] end,
node.nodes
)
node.nodes = vim.tbl_map(
update_status(nodes_by_path, node_ignored, status),
vim.tbl_filter(
function(n) return child_names[n.absolute_path] end,
node.nodes
))
local is_root = node.cwd ~= nil
if vim.g.nvim_tree_group_empty == 1 and not is_root and #(node.nodes) == 1 then