mirror of
https://github.com/kristoferssolo/telescope-frecency.nvim.git
synced 2025-10-21 20:10:38 +00:00
* refactor: unite logic for finder & async_finder * chore: fix types * chore: add sleep to show results at first * refactor: fix to find results separatedly * test: remove unnecessary ones and fix others * test: add matrix for 0.9.x & Windows * test: use forked plenary.log for Windows * test: fix to use strptime in Windows * test: run again if segmentation fault in Windows * test: loosen timeout for Perl * test: use the latest plenary.nvim again * chore: fix types * chore: change variable name * feat: watch changes of DB to reload * chore: add comments to steps * test: copy whole modules for testing in Windows * fix: make valid paths for Windows * test: add tests for Native * test: use robust way to calculate time vim.fn.strptime cannot be used in Lua loop * chore: fix comments * refactor: simplify the code * test: loosen condition to detect failures * test: disable some logging Many loggings make the test fail. * test: run tests sequentially in Windows * test: loosen timeout not to fail on Windows
88 lines
2.2 KiB
Lua
88 lines
2.2 KiB
Lua
local async = require "plenary.async" --[[@as PlenaryAsync]]
|
|
local log = require "plenary.log"
|
|
local uv = vim.loop or vim.uv
|
|
|
|
---@class FrecencyNativeWatcherMtime
|
|
---@field sec integer
|
|
---@field nsec integer
|
|
local Mtime = {}
|
|
|
|
---@param mtime FsStatMtime
|
|
---@return FrecencyNativeWatcherMtime
|
|
Mtime.new = function(mtime)
|
|
return setmetatable({ sec = mtime.sec, nsec = mtime.nsec }, Mtime)
|
|
end
|
|
|
|
---@param other FrecencyNativeWatcherMtime
|
|
---@return boolean
|
|
function Mtime:__eq(other)
|
|
return self.sec == other.sec and self.nsec == other.nsec
|
|
end
|
|
|
|
---@return string
|
|
function Mtime:__tostring()
|
|
return string.format("%d.%d", self.sec, self.nsec)
|
|
end
|
|
|
|
---@class FrecencyNativeWatcher
|
|
---@field handler UvFsEventHandle
|
|
---@field path string
|
|
---@field mtime FrecencyNativeWatcherMtime
|
|
local Watcher = {}
|
|
|
|
---@return FrecencyNativeWatcher
|
|
Watcher.new = function()
|
|
return setmetatable({ path = "", mtime = Mtime.new { sec = 0, nsec = 0 } }, { __index = Watcher })
|
|
end
|
|
|
|
---@param path string
|
|
---@param tx PlenaryAsyncControlChannelTx
|
|
function Watcher:watch(path, tx)
|
|
if self.handler then
|
|
self.handler:stop()
|
|
end
|
|
self.handler = assert(uv.new_fs_event()) --[[@as UvFsEventHandle]]
|
|
self.handler:start(path, { recursive = true }, function(err, _, _)
|
|
if err then
|
|
log.debug("failed to watch path: " .. err)
|
|
return
|
|
end
|
|
async.void(function()
|
|
-- NOTE: wait for updating mtime
|
|
async.util.sleep(50)
|
|
local stat
|
|
err, stat = async.uv.fs_stat(path)
|
|
if err then
|
|
log.debug("failed to stat path: " .. err)
|
|
return
|
|
end
|
|
local mtime = Mtime.new(stat.mtime)
|
|
if self.mtime ~= mtime then
|
|
log.debug(("mtime changed: %s -> %s"):format(self.mtime, mtime))
|
|
self.mtime = mtime
|
|
tx.send()
|
|
end
|
|
end)()
|
|
end)
|
|
end
|
|
|
|
local watcher = Watcher.new()
|
|
|
|
return {
|
|
---@param path string
|
|
---@param tx PlenaryAsyncControlChannelTx
|
|
---@return nil
|
|
watch = function(path, tx)
|
|
log.debug("watch path: " .. path)
|
|
watcher:watch(path, tx)
|
|
end,
|
|
|
|
---@param stat FsStat
|
|
---@return nil
|
|
update = function(stat)
|
|
local mtime = Mtime.new(stat.mtime)
|
|
log.debug(("update mtime: %s -> %s"):format(watcher.mtime, mtime))
|
|
watcher.mtime = mtime
|
|
end,
|
|
}
|