Skip to content

Commit 3f4ed9b

Browse files
authored
fix: reload git status of existing nodes (#975)
1 parent 95c331c commit 3f4ed9b

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

lua/nvim-tree/actions/reloaders.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ local explorer_module = require'nvim-tree.explorer'
77
local M = {}
88

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

lua/nvim-tree/explorer/node-builders.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {
55
is_windows = vim.fn.has('win32') == 1
66
}
77

8-
local function get_dir_git_status(parent_ignored, status, absolute_path)
8+
function M.get_dir_git_status(parent_ignored, status, absolute_path)
99
if parent_ignored then
1010
return '!!'
1111
end
@@ -14,13 +14,17 @@ local function get_dir_git_status(parent_ignored, status, absolute_path)
1414
return dir_status or file_status
1515
end
1616

17+
function M.get_git_status(parent_ignored, status, absolute_path)
18+
return parent_ignored and '!!' or status.files and status.files[absolute_path]
19+
end
20+
1721
function M.folder(absolute_path, name, status, parent_ignored)
1822
local handle = uv.fs_scandir(absolute_path)
1923
local has_children = handle and uv.fs_scandir_next(handle) ~= nil
2024

2125
return {
2226
absolute_path = absolute_path,
23-
git_status = get_dir_git_status(parent_ignored, status, absolute_path),
27+
git_status = M.get_dir_git_status(parent_ignored, status, absolute_path),
2428
group_next = nil, -- If node is grouped, this points to the next child dir/link node
2529
has_children = has_children,
2630
name = name,
@@ -43,7 +47,7 @@ function M.file(absolute_path, name, status, parent_ignored)
4347
absolute_path = absolute_path,
4448
executable = is_executable(absolute_path, ext),
4549
extension = ext,
46-
git_status = parent_ignored and '!!' or status.files and status.files[absolute_path],
50+
git_status = M.get_git_status(parent_ignored, status, absolute_path),
4751
name = name,
4852
}
4953
end
@@ -70,7 +74,7 @@ function M.link(absolute_path, name, status, parent_ignored)
7074

7175
return {
7276
absolute_path = absolute_path,
73-
git_status = parent_ignored and '!!' or status.files and status.files[absolute_path],
77+
git_status = M.get_git_status(parent_ignored, status, absolute_path),
7478
group_next = nil, -- If node is grouped, this points to the next child dir/link node
7579
last_modified = last_modified,
7680
link_to = link_to,

lua/nvim-tree/explorer/reload.lua

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ local function key_by(nodes, key)
1515
return v
1616
end
1717

18+
local function update_status(nodes_by_path, node_ignored, status)
19+
return function(node)
20+
if nodes_by_path[node.absolute_path] then
21+
if node.nodes then
22+
node.git_status = builders.get_dir_git_status(node_ignored, status, node.absolute_path)
23+
else
24+
node.git_status = builders.get_git_status(node_ignored, status, node.absolute_path)
25+
end
26+
end
27+
return node
28+
end
29+
end
30+
1831
function M.reload(node, cwd, status)
1932
local handle = uv.fs_scandir(cwd)
2033
if type(handle) == 'string' then
@@ -54,10 +67,12 @@ function M.reload(node, cwd, status)
5467
end
5568
end
5669

57-
node.nodes = vim.tbl_filter(
58-
function(n) return child_names[n.absolute_path] end,
59-
node.nodes
60-
)
70+
node.nodes = vim.tbl_map(
71+
update_status(nodes_by_path, node_ignored, status),
72+
vim.tbl_filter(
73+
function(n) return child_names[n.absolute_path] end,
74+
node.nodes
75+
))
6176

6277
local is_root = node.cwd ~= nil
6378
if vim.g.nvim_tree_group_empty == 1 and not is_root and #(node.nodes) == 1 then

0 commit comments

Comments
 (0)