Skip to content

Commit 1c57a82

Browse files
committed
fix(#1731): watcher refreshes node rather than the first node matching absolute path, profile refresh
1 parent e38e061 commit 1c57a82

File tree

5 files changed

+57
-23
lines changed

5 files changed

+57
-23
lines changed

lua/nvim-tree/actions/fs/create-file.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ function M.fn(node)
111111
-- synchronous call required so that we may focus the file now
112112
node = node.nodes ~= nil and node or node.parent
113113
if node then
114-
watch.refresh_path(node.absolute_path)
114+
watch.refresh_node(node)
115115
end
116116
end
117+
-- TODO #1731 #1716 this gets upset by watcher add new file to linked directories above
117118
utils.focus_file(utils.path_remove_trailing(new_file_path))
118119
end)
119120
end

lua/nvim-tree/explorer/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function Explorer.new(cwd)
1515
local explorer = setmetatable({
1616
absolute_path = cwd,
1717
nodes = {},
18-
watcher = watch.create_watcher(cwd),
1918
open = true,
2019
}, Explorer)
20+
explorer.watcher = watch.create_watcher(explorer)
2121
explorer:_load(explorer)
2222
return explorer
2323
end

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

+17-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function M.folder(parent, absolute_path, name)
1010
local handle = vim.loop.fs_scandir(absolute_path)
1111
local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
1212

13-
return {
13+
local node = {
1414
type = "directory",
1515
absolute_path = absolute_path,
1616
fs_stat = vim.loop.fs_stat(absolute_path),
@@ -20,8 +20,11 @@ function M.folder(parent, absolute_path, name)
2020
nodes = {},
2121
open = false,
2222
parent = parent,
23-
watcher = watch.create_watcher(absolute_path),
2423
}
24+
25+
node.watcher = watch.create_watcher(node)
26+
27+
return node
2528
end
2629

2730
function M.is_executable(parent, absolute_path, ext)
@@ -63,16 +66,18 @@ end
6366
function M.link(parent, absolute_path, name)
6467
--- I dont know if this is needed, because in my understanding, there isn't hard links in windows, but just to be sure i changed it.
6568
local link_to = vim.loop.fs_realpath(absolute_path)
66-
local open, nodes, has_children, watcher
67-
if (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory" then
69+
local open, nodes, has_children
70+
71+
local is_dir_link = (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory"
72+
73+
if is_dir_link then
6874
local handle = vim.loop.fs_scandir(link_to)
6975
has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
7076
open = false
7177
nodes = {}
72-
watcher = watch.create_watcher(link_to)
7378
end
7479

75-
return {
80+
local node = {
7681
type = "link",
7782
absolute_path = absolute_path,
7883
fs_stat = vim.loop.fs_stat(absolute_path),
@@ -83,8 +88,13 @@ function M.link(parent, absolute_path, name)
8388
nodes = nodes,
8489
open = open,
8590
parent = parent,
86-
watcher = watcher,
8791
}
92+
93+
if is_dir_link then
94+
node.watcher = watch.create_watcher(node)
95+
end
96+
97+
return node
8898
end
8999

90100
return M

lua/nvim-tree/explorer/reload.lua

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local filters = require "nvim-tree.explorer.filters"
55
local sorters = require "nvim-tree.explorer.sorters"
66
local live_filter = require "nvim-tree.live-filter"
77
local notify = require "nvim-tree.notify"
8+
local log = require "nvim-tree.log"
89

910
local M = {}
1011

@@ -25,6 +26,8 @@ function M.reload(node, status)
2526
return
2627
end
2728

29+
local ps = log.profile_start("reload %s", cwd)
30+
2831
if node.group_next then
2932
node.nodes = { node.group_next }
3033
node.group_next = nil
@@ -110,11 +113,13 @@ function M.reload(node, status)
110113
node.group_next = child_folder_only
111114
local ns = M.reload(child_folder_only, status)
112115
node.nodes = ns or {}
116+
log.profile_end(ps, "reload %s", cwd)
113117
return ns
114118
end
115119

116120
sorters.merge_sort(node.nodes, sorters.node_comparator)
117121
live_filter.apply_filter(node)
122+
log.profile_end(ps, "reload %s", cwd)
118123
return node.nodes
119124
end
120125

lua/nvim-tree/explorer/watch.lua

+32-14
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,57 @@ local function is_folder_ignored(path)
4646
return false
4747
end
4848

49-
function M.refresh_path(path)
50-
log.line("watcher", "node event executing '%s'", path)
51-
local n = utils.get_node_from_path(path)
52-
if not n then
49+
function M.refresh_node(node)
50+
if type(node) ~= "table" then
5351
return
5452
end
5553

56-
local node = utils.get_parent_of_group(n)
54+
if node.link_to then
55+
log.line("watcher", "node event executing refresh '%s' -> '%s'", node.link_to, node.absolute_path)
56+
else
57+
log.line("watcher", "node event executing refresh '%s'", node.absolute_path)
58+
end
59+
60+
local path = node.absolute_path
61+
62+
local parent_node = utils.get_parent_of_group(node)
63+
5764
local project_root, project = reload_and_get_git_project(path)
58-
require("nvim-tree.explorer.reload").reload(node, project)
59-
update_parent_statuses(node, project, project_root)
65+
66+
require("nvim-tree.explorer.reload").reload(parent_node, project)
67+
68+
update_parent_statuses(parent_node, project, project_root)
6069

6170
require("nvim-tree.renderer").draw()
6271
end
6372

64-
function M.create_watcher(absolute_path)
65-
if not M.enabled then
73+
function M.create_watcher(node)
74+
if not M.enabled or type(node) ~= "table" then
6675
return nil
6776
end
68-
if is_git(absolute_path) or is_folder_ignored(absolute_path) then
77+
78+
local path
79+
if node.type == "link" then
80+
path = node.link_to
81+
else
82+
path = node.absolute_path
83+
end
84+
85+
if is_git(path) or is_folder_ignored(path) then
6986
return nil
7087
end
7188

7289
local function callback(watcher)
73-
log.line("watcher", "node event scheduled %s", watcher.context)
90+
log.line("watcher", "node event scheduled refresh %s", watcher.context)
7491
utils.debounce(watcher.context, M.debounce_delay, function()
75-
M.refresh_path(watcher._path)
92+
M.refresh_node(node)
7693
end)
7794
end
7895

7996
M.uid = M.uid + 1
80-
return Watcher:new(absolute_path, nil, callback, {
81-
context = "explorer:watch:" .. absolute_path .. ":" .. M.uid,
97+
return Watcher:new(path, nil, callback, {
98+
context = "explorer:watch:" .. path .. ":" .. M.uid,
99+
node = node,
82100
})
83101
end
84102

0 commit comments

Comments
 (0)