fswatch: move-to <=> change (close #1171).

This commit is contained in:
L3MON4D3 2024-05-16 17:42:04 +02:00
parent 7b1dda0dfd
commit 6f23556cff
3 changed files with 81 additions and 1 deletions

View file

@ -185,9 +185,21 @@ function TreeWatcher:fs_event_callback(err, relpath, events)
end
if f_type == "file" then
self:new_file(relpath, full_path)
if self.files[relpath] then
-- rename and file exists => a new file was moved into its
-- place => handle as changed file.
self:change_file(relpath, full_path)
else
self:new_file(relpath, full_path)
end
return
elseif f_type == "directory" then
if self.dir_watchers[relpath] then
-- rename and directory exists => directory is overwritten
-- => stop recursively, clear, and start a new watcher.
self.dir_watchers[relpath]:stop()
self.dir_watchers[relpath] = nil
end
self:new_dir(relpath, full_path)
return
end

View file

@ -374,6 +374,13 @@ end
function M.scratch_mkdir(scratch_rel)
os.execute(('mkdir -p "%s/%s"'):format(scratchdir_path, scratch_rel))
end
function M.scratch_mv(scratch_from, scratch_to)
os.execute(('mv "%s/%s" "%s/%s"'):format(scratchdir_path, scratch_from, scratchdir_path, scratch_to))
end
-- mv -T
function M.scratch_mv_T(scratch_from, scratch_to)
os.execute(('mv -T "%s/%s" "%s/%s"'):format(scratchdir_path, scratch_from, scratchdir_path, scratch_to))
end
function M.scratch_touch(scratch_rel)
os.execute(('touch "%s/%s"'):format(scratchdir_path, scratch_rel))
end

View file

@ -3,6 +3,8 @@ local exec_lua = ls_helpers.exec_lua
local mkdir = ls_helpers.scratch_mkdir
local touch = ls_helpers.scratch_touch
local edit = ls_helpers.scratch_edit
local mv = ls_helpers.scratch_mv
local mv_T = ls_helpers.scratch_mv_T
describe("fs_events", function()
before_each(function()
@ -205,6 +207,65 @@ describe("fs_events", function()
}, exec_lua([[return {seen_files, seen_dirs, changed}]]))
end)
it("libuv triggers change on overwriting existing entry.", function()
mkdir("a")
touch("a/a")
touch("a/b")
mkdir("a/c")
touch("a/c/e")
mkdir("a/d")
exec_lua([[
changed = {
["a/a"] = 0,
["a/b"] = 0,
["a/c/e"] = 0,
["a/d/e"] = 0,
}
seen = {
["a/a"] = 0,
["a/b"] = 0,
["a/c/e"] = 0,
["a/d/e"] = 0,
}
ls.log.set_loglevel("debug")
watcher = scratch_tree_watcher("a", 3, {
change_file = function(path)
changed[path] = changed[path] + 1
end,
new_file = function(path)
seen[path] = seen[path] + 1
end
}, {lazy=false, fs_event_providers = {libuv = true} } )
ls.log.set_loglevel("warn")
]])
-- make sure a/b is marked as changed.
mv("a/a", "a/b")
-- make sure a/d/e is seen.
mv_T("a/c", "a/d")
assert.are.same({
{
["a/b"] = 1,
-- we don't handle file-removal, it's currently out of scope
-- for the loaders to remove snippets when the file is removed,
-- so we don't have to handle it here.
["a/a"] = 0,
-- both are only created, not changed.
["a/c/e"] = 0,
["a/d/e"] = 0,
},
{
["a/a"] = 1,
["a/b"] = 1,
["a/c/e"] = 1,
["a/d/e"] = 1
}
}, exec_lua([[return {changed, seen}]]))
end)
it("lazy registration works with libuv.", function()
mkdir("a")