Skip to content

Commit f85af83

Browse files
authored
#1217 show git status for link targets, when no status on the link itself (#1263)
1 parent 82ec79a commit f85af83

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

lua/nvim-tree/explorer/common.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,39 @@ local uv = vim.loop
22

33
local M = {}
44

5+
local function get_dir_git_status(parent_ignored, status, absolute_path)
6+
if parent_ignored then
7+
return "!!"
8+
end
9+
local dir_status = status.dirs and status.dirs[absolute_path]
10+
local file_status = status.files and status.files[absolute_path]
11+
return dir_status or file_status
12+
end
13+
14+
local function get_git_status(parent_ignored, status, absolute_path)
15+
return parent_ignored and "!!" or status.files and status.files[absolute_path]
16+
end
17+
518
function M.has_one_child_folder(node)
619
return #node.nodes == 1 and node.nodes[1].nodes and uv.fs_access(node.nodes[1].absolute_path, "R")
720
end
821

22+
function M.update_git_status(node, parent_ignored, status)
23+
-- status of the node's absolute path
24+
if node.nodes then
25+
node.git_status = get_dir_git_status(parent_ignored, status, node.absolute_path)
26+
else
27+
node.git_status = get_git_status(parent_ignored, status, node.absolute_path)
28+
end
29+
30+
-- status of the link target, if the link itself is not dirty
31+
if node.link_to and not node.git_status then
32+
if node.nodes then
33+
node.git_status = get_dir_git_status(parent_ignored, status, node.link_to)
34+
else
35+
node.git_status = get_git_status(parent_ignored, status, node.link_to)
36+
end
37+
end
38+
end
39+
940
return M

lua/nvim-tree/explorer/explore.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ local function populate_children(handle, cwd, node, status)
2929
and not filters.should_ignore_git(abs, status.files)
3030
and not nodes_by_path[abs]
3131
then
32+
local child = nil
3233
if t == "directory" and uv.fs_access(abs, "R") then
33-
table.insert(node.nodes, builders.folder(node, abs, name, status, node_ignored))
34+
child = builders.folder(node, abs, name, status, node_ignored)
3435
elseif t == "file" then
35-
table.insert(node.nodes, builders.file(node, abs, name, status, node_ignored))
36+
child = builders.file(node, abs, name, status, node_ignored)
3637
elseif t == "link" then
3738
local link = builders.link(node, abs, name, status, node_ignored)
3839
if link.link_to ~= nil then
39-
table.insert(node.nodes, link)
40+
child = link
4041
end
4142
end
43+
if child then
44+
table.insert(node.nodes, child)
45+
common.update_git_status(child, node_ignored, status)
46+
end
4247
end
4348
end
4449
end

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,13 @@ local M = {
55
is_windows = vim.fn.has "win32" == 1,
66
}
77

8-
function M.get_dir_git_status(parent_ignored, status, absolute_path)
9-
if parent_ignored then
10-
return "!!"
11-
end
12-
local dir_status = status.dirs and status.dirs[absolute_path]
13-
local file_status = status.files and status.files[absolute_path]
14-
return dir_status or file_status
15-
end
16-
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-
21-
function M.folder(parent, absolute_path, name, status, parent_ignored)
8+
function M.folder(parent, absolute_path, name)
229
local handle = uv.fs_scandir(absolute_path)
2310
local has_children = handle and uv.fs_scandir_next(handle) ~= nil
2411

2512
return {
2613
absolute_path = absolute_path,
2714
fs_stat = uv.fs_stat(absolute_path),
28-
git_status = M.get_dir_git_status(parent_ignored, status, absolute_path),
2915
group_next = nil, -- If node is grouped, this points to the next child dir/link node
3016
has_children = has_children,
3117
name = name,
@@ -42,15 +28,14 @@ local function is_executable(absolute_path, ext)
4228
return uv.fs_access(absolute_path, "X")
4329
end
4430

45-
function M.file(parent, absolute_path, name, status, parent_ignored)
31+
function M.file(parent, absolute_path, name)
4632
local ext = string.match(name, ".?[^.]+%.(.*)") or ""
4733

4834
return {
4935
absolute_path = absolute_path,
5036
executable = is_executable(absolute_path, ext),
5137
extension = ext,
5238
fs_stat = uv.fs_stat(absolute_path),
53-
git_status = M.get_git_status(parent_ignored, status, absolute_path),
5439
name = name,
5540
parent = parent,
5641
}
@@ -61,7 +46,7 @@ end
6146
-- links (for instance libr2.so in /usr/lib) and thus even with a C program realpath fails
6247
-- when it has no real reason to. Maybe there is a reason, but errno is definitely wrong.
6348
-- So we need to check for link_to ~= nil when adding new links to the main tree
64-
function M.link(parent, absolute_path, name, status, parent_ignored)
49+
function M.link(parent, absolute_path, name)
6550
--- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it.
6651
local link_to = uv.fs_realpath(absolute_path)
6752
local open, nodes, has_children
@@ -75,7 +60,6 @@ function M.link(parent, absolute_path, name, status, parent_ignored)
7560
return {
7661
absolute_path = absolute_path,
7762
fs_stat = uv.fs_stat(absolute_path),
78-
git_status = M.get_git_status(parent_ignored, status, absolute_path),
7963
group_next = nil, -- If node is grouped, this points to the next child dir/link node
8064
has_children = has_children,
8165
link_to = link_to,

lua/nvim-tree/explorer/reload.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ local M = {}
1212
local function update_status(nodes_by_path, node_ignored, status)
1313
return function(node)
1414
if nodes_by_path[node.absolute_path] then
15-
if node.nodes then
16-
node.git_status = builders.get_dir_git_status(node_ignored, status, node.absolute_path)
17-
else
18-
node.git_status = builders.get_git_status(node_ignored, status, node.absolute_path)
19-
end
15+
common.update_git_status(node, node_ignored, status)
2016
end
2117
return node
2218
end

0 commit comments

Comments
 (0)