fix(explorer.watch): pcall watcher, since it can give errors on windows

This commit is contained in:
Folke Lemaitre 2025-02-04 18:59:04 +01:00
parent bc87992e71
commit af968181af
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -21,7 +21,7 @@ local running = {} ---@type table<string, fun()>
function M.abort() function M.abort()
for _, abort in pairs(running) do for _, abort in pairs(running) do
abort() pcall(abort)
end end
running = {} running = {}
end end
@ -38,37 +38,39 @@ function M.watch(cwd)
end end
M.abort() M.abort()
local timer = assert(uv.new_timer()) pcall(function()
local cancel = watch(cwd, { local timer = assert(uv.new_timer())
uvflags = { recursive = true }, local cancel = watch(cwd, {
}, function(path) uvflags = { recursive = true },
-- handle deletes }, function(path)
while not uv.fs_stat(path) do -- handle deletes
local p = vim.fs.dirname(path) while not uv.fs_stat(path) do
if p == path then local p = vim.fs.dirname(path)
return if p == path then
end return
path = p
end
Tree:refresh(path)
-- batch updates and give explorer the time to update before the watcher
timer:start(100, 0, function()
local picker = Snacks.picker.get({ source = "explorer" })[1]
if picker and Tree:is_dirty(picker:cwd(), picker.opts) then
if not picker.list.target then
picker.list:set_target()
end end
picker:find() path = p
end end
Tree:refresh(path)
-- batch updates and give explorer the time to update before the watcher
timer:start(100, 0, function()
local picker = Snacks.picker.get({ source = "explorer" })[1]
if picker and Tree:is_dirty(picker:cwd(), picker.opts) then
if not picker.list.target then
picker.list:set_target()
end
picker:find()
end
end)
end) end)
end) running[cwd] = function()
running[cwd] = function() if not timer:is_closing() then
if not timer:is_closing() then timer:close()
timer:close() end
cancel()
end end
cancel() end)
end
end end
return M return M