mirror of
https://github.com/neovim/neovim.git
synced 2025-12-13 10:02:49 +00:00
refactor(watch): simplify filechanges processing
This commit is contained in:
@@ -131,8 +131,10 @@ function M.watchdirs(path, opts, callback)
|
|||||||
|
|
||||||
local timer = assert(uv.new_timer())
|
local timer = assert(uv.new_timer())
|
||||||
|
|
||||||
---@type table[]
|
--- Map of file path to boolean indicating if the file has been changed
|
||||||
local changesets = {}
|
--- at some point within the debounce cycle.
|
||||||
|
--- @type table<string, boolean>
|
||||||
|
local filechanges = {}
|
||||||
|
|
||||||
local process_changes --- @type fun()
|
local process_changes --- @type fun()
|
||||||
|
|
||||||
@@ -146,34 +148,25 @@ function M.watchdirs(path, opts, callback)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(changesets, {
|
if not filechanges[fullpath] then
|
||||||
fullpath = fullpath,
|
filechanges[fullpath] = events.change or false
|
||||||
events = events,
|
end
|
||||||
})
|
|
||||||
timer:start(debounce, 0, process_changes)
|
timer:start(debounce, 0, process_changes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
process_changes = function()
|
process_changes = function()
|
||||||
---@type table<string, table[]>
|
-- Since the callback is debounced it may have also been deleted later on
|
||||||
local filechanges = vim.defaulttable()
|
-- so we always need to check the existence of the file:
|
||||||
for i, change in ipairs(changesets) do
|
-- stat succeeds, changed=true -> Changed
|
||||||
changesets[i] = nil
|
-- stat succeeds, changed=false -> Created
|
||||||
if not skip(change.fullpath) then
|
-- stat fails -> Removed
|
||||||
table.insert(filechanges[change.fullpath], change.events)
|
for fullpath, changed in pairs(filechanges) do
|
||||||
end
|
|
||||||
end
|
|
||||||
for fullpath, events_list in pairs(filechanges) do
|
|
||||||
uv.fs_stat(fullpath, function(_, stat)
|
uv.fs_stat(fullpath, function(_, stat)
|
||||||
---@type vim._watch.FileChangeType
|
---@type vim._watch.FileChangeType
|
||||||
local change_type
|
local change_type
|
||||||
if stat then
|
if stat then
|
||||||
change_type = M.FileChangeType.Created
|
change_type = changed and M.FileChangeType.Changed or M.FileChangeType.Created
|
||||||
for _, event in ipairs(events_list) do
|
|
||||||
if event.change then
|
|
||||||
change_type = M.FileChangeType.Changed
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if stat.type == 'directory' then
|
if stat.type == 'directory' then
|
||||||
local handle = handles[fullpath]
|
local handle = handles[fullpath]
|
||||||
if not handle then
|
if not handle then
|
||||||
@@ -195,6 +188,7 @@ function M.watchdirs(path, opts, callback)
|
|||||||
callback(fullpath, change_type)
|
callback(fullpath, change_type)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
filechanges = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
local root_handle = assert(uv.new_fs_event())
|
local root_handle = assert(uv.new_fs_event())
|
||||||
|
|||||||
Reference in New Issue
Block a user