fix(#1731): watcher refreshes node rather than the first node matching absolute path, profile refresh

This commit is contained in:
Alexander Courtis 2022-11-19 17:07:05 +11:00
parent e38e061710
commit 1c57a8284d
5 changed files with 57 additions and 23 deletions

View File

@ -111,9 +111,10 @@ function M.fn(node)
-- synchronous call required so that we may focus the file now
node = node.nodes ~= nil and node or node.parent
if node then
watch.refresh_path(node.absolute_path)
watch.refresh_node(node)
end
end
-- TODO #1731 #1716 this gets upset by watcher add new file to linked directories above
utils.focus_file(utils.path_remove_trailing(new_file_path))
end)
end

View File

@ -15,9 +15,9 @@ function Explorer.new(cwd)
local explorer = setmetatable({
absolute_path = cwd,
nodes = {},
watcher = watch.create_watcher(cwd),
open = true,
}, Explorer)
explorer.watcher = watch.create_watcher(explorer)
explorer:_load(explorer)
return explorer
end

View File

@ -10,7 +10,7 @@ function M.folder(parent, absolute_path, name)
local handle = vim.loop.fs_scandir(absolute_path)
local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
return {
local node = {
type = "directory",
absolute_path = absolute_path,
fs_stat = vim.loop.fs_stat(absolute_path),
@ -20,8 +20,11 @@ function M.folder(parent, absolute_path, name)
nodes = {},
open = false,
parent = parent,
watcher = watch.create_watcher(absolute_path),
}
node.watcher = watch.create_watcher(node)
return node
end
function M.is_executable(parent, absolute_path, ext)
@ -63,16 +66,18 @@ end
function M.link(parent, absolute_path, name)
--- 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.
local link_to = vim.loop.fs_realpath(absolute_path)
local open, nodes, has_children, watcher
if (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory" then
local open, nodes, has_children
local is_dir_link = (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory"
if is_dir_link then
local handle = vim.loop.fs_scandir(link_to)
has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
open = false
nodes = {}
watcher = watch.create_watcher(link_to)
end
return {
local node = {
type = "link",
absolute_path = absolute_path,
fs_stat = vim.loop.fs_stat(absolute_path),
@ -83,8 +88,13 @@ function M.link(parent, absolute_path, name)
nodes = nodes,
open = open,
parent = parent,
watcher = watcher,
}
if is_dir_link then
node.watcher = watch.create_watcher(node)
end
return node
end
return M

View File

@ -5,6 +5,7 @@ local filters = require "nvim-tree.explorer.filters"
local sorters = require "nvim-tree.explorer.sorters"
local live_filter = require "nvim-tree.live-filter"
local notify = require "nvim-tree.notify"
local log = require "nvim-tree.log"
local M = {}
@ -25,6 +26,8 @@ function M.reload(node, status)
return
end
local ps = log.profile_start("reload %s", cwd)
if node.group_next then
node.nodes = { node.group_next }
node.group_next = nil
@ -110,11 +113,13 @@ function M.reload(node, status)
node.group_next = child_folder_only
local ns = M.reload(child_folder_only, status)
node.nodes = ns or {}
log.profile_end(ps, "reload %s", cwd)
return ns
end
sorters.merge_sort(node.nodes, sorters.node_comparator)
live_filter.apply_filter(node)
log.profile_end(ps, "reload %s", cwd)
return node.nodes
end

View File

@ -46,39 +46,57 @@ local function is_folder_ignored(path)
return false
end
function M.refresh_path(path)
log.line("watcher", "node event executing '%s'", path)
local n = utils.get_node_from_path(path)
if not n then
function M.refresh_node(node)
if type(node) ~= "table" then
return
end
local node = utils.get_parent_of_group(n)
if node.link_to then
log.line("watcher", "node event executing refresh '%s' -> '%s'", node.link_to, node.absolute_path)
else
log.line("watcher", "node event executing refresh '%s'", node.absolute_path)
end
local path = node.absolute_path
local parent_node = utils.get_parent_of_group(node)
local project_root, project = reload_and_get_git_project(path)
require("nvim-tree.explorer.reload").reload(node, project)
update_parent_statuses(node, project, project_root)
require("nvim-tree.explorer.reload").reload(parent_node, project)
update_parent_statuses(parent_node, project, project_root)
require("nvim-tree.renderer").draw()
end
function M.create_watcher(absolute_path)
if not M.enabled then
function M.create_watcher(node)
if not M.enabled or type(node) ~= "table" then
return nil
end
if is_git(absolute_path) or is_folder_ignored(absolute_path) then
local path
if node.type == "link" then
path = node.link_to
else
path = node.absolute_path
end
if is_git(path) or is_folder_ignored(path) then
return nil
end
local function callback(watcher)
log.line("watcher", "node event scheduled %s", watcher.context)
log.line("watcher", "node event scheduled refresh %s", watcher.context)
utils.debounce(watcher.context, M.debounce_delay, function()
M.refresh_path(watcher._path)
M.refresh_node(node)
end)
end
M.uid = M.uid + 1
return Watcher:new(absolute_path, nil, callback, {
context = "explorer:watch:" .. absolute_path .. ":" .. M.uid,
return Watcher:new(path, nil, callback, {
context = "explorer:watch:" .. path .. ":" .. M.uid,
node = node,
})
end