chore: resolve undefined-field
This commit is contained in:
parent
52ff301e07
commit
d663059c3e
@ -66,9 +66,10 @@ function M.create_watcher(node)
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param watcher Watcher
|
||||
local function callback(watcher)
|
||||
log.line("watcher", "node event scheduled refresh %s", watcher.context)
|
||||
utils.debounce(watcher.context, M.config.filesystem_watchers.debounce_delay, function()
|
||||
log.line("watcher", "node event scheduled refresh %s", watcher.data.context)
|
||||
utils.debounce(watcher.data.context, M.config.filesystem_watchers.debounce_delay, function()
|
||||
if watcher.destroyed then
|
||||
return
|
||||
end
|
||||
@ -82,7 +83,7 @@ function M.create_watcher(node)
|
||||
end
|
||||
|
||||
M.uid = M.uid + 1
|
||||
return Watcher:new(path, nil, callback, {
|
||||
return Watcher:create(path, nil, callback, {
|
||||
context = "explorer:watch:" .. path .. ":" .. M.uid,
|
||||
})
|
||||
end
|
||||
|
||||
@ -260,18 +260,19 @@ function M.load_project_status(path)
|
||||
if M.config.filesystem_watchers.enable then
|
||||
log.line("watcher", "git start")
|
||||
|
||||
---@param w Watcher
|
||||
local callback = function(w)
|
||||
log.line("watcher", "git event scheduled '%s'", w.toplevel)
|
||||
utils.debounce("git:watcher:" .. w.toplevel, M.config.filesystem_watchers.debounce_delay, function()
|
||||
log.line("watcher", "git event scheduled '%s'", w.data.toplevel)
|
||||
utils.debounce("git:watcher:" .. w.data.toplevel, M.config.filesystem_watchers.debounce_delay, function()
|
||||
if w.destroyed then
|
||||
return
|
||||
end
|
||||
reload_tree_at(w.toplevel)
|
||||
reload_tree_at(w.data.toplevel)
|
||||
end)
|
||||
end
|
||||
|
||||
local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join({ toplevel, ".git" })
|
||||
watcher = Watcher:new(git_dir, WATCHED_FILES, callback, {
|
||||
watcher = Watcher:create(git_dir, WATCHED_FILES, callback, {
|
||||
toplevel = toplevel,
|
||||
})
|
||||
end
|
||||
|
||||
@ -2,21 +2,7 @@ local notify = require("nvim-tree.notify")
|
||||
local log = require("nvim-tree.log")
|
||||
local utils = require("nvim-tree.utils")
|
||||
|
||||
local M = {
|
||||
config = {},
|
||||
}
|
||||
|
||||
---@class Event
|
||||
local Event = {
|
||||
_events = {},
|
||||
}
|
||||
Event.__index = Event
|
||||
|
||||
---@class Watcher
|
||||
local Watcher = {
|
||||
_watchers = {},
|
||||
}
|
||||
Watcher.__index = Watcher
|
||||
local Class = require("nvim-tree.class")
|
||||
|
||||
local FS_EVENT_FLAGS = {
|
||||
-- inotify or equivalent will be used; fallback to stat has not yet been implemented
|
||||
@ -25,20 +11,40 @@ local FS_EVENT_FLAGS = {
|
||||
recursive = false,
|
||||
}
|
||||
|
||||
local M = {
|
||||
config = {},
|
||||
}
|
||||
|
||||
---@class (exact) Event: Class
|
||||
---@field destroyed boolean
|
||||
---@field private path string
|
||||
---@field private fs_event uv.uv_fs_event_t?
|
||||
---@field private listeners function[]
|
||||
local Event = Class:new()
|
||||
|
||||
---Registry of all events
|
||||
---@type Event[]
|
||||
local events = {}
|
||||
|
||||
---Static factory method
|
||||
---Creates and starts an Event
|
||||
---@param path string
|
||||
---@return Event|nil
|
||||
function Event:new(path)
|
||||
log.line("watcher", "Event:new '%s'", path)
|
||||
function Event:create(path)
|
||||
log.line("watcher", "Event:create '%s'", path)
|
||||
|
||||
local e = setmetatable({
|
||||
_path = path,
|
||||
_fs_event = nil,
|
||||
_listeners = {},
|
||||
}, Event)
|
||||
---@type Event
|
||||
local o = {
|
||||
destroyed = false,
|
||||
path = path,
|
||||
fs_event = nil,
|
||||
listeners = {},
|
||||
}
|
||||
o = self:new(o) --[[@as Event]]
|
||||
|
||||
if e:start() then
|
||||
Event._events[path] = e
|
||||
return e
|
||||
if o:start() then
|
||||
events[path] = o
|
||||
return o
|
||||
else
|
||||
return nil
|
||||
end
|
||||
@ -46,21 +52,21 @@ end
|
||||
|
||||
---@return boolean
|
||||
function Event:start()
|
||||
log.line("watcher", "Event:start '%s'", self._path)
|
||||
log.line("watcher", "Event:start '%s'", self.path)
|
||||
|
||||
local rc, _, name
|
||||
|
||||
self._fs_event, _, name = vim.loop.new_fs_event()
|
||||
if not self._fs_event then
|
||||
self._fs_event = nil
|
||||
notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self._path, name))
|
||||
self.fs_event, _, name = vim.loop.new_fs_event()
|
||||
if not self.fs_event then
|
||||
self.fs_event = nil
|
||||
notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self.path, name))
|
||||
return false
|
||||
end
|
||||
|
||||
local event_cb = vim.schedule_wrap(function(err, filename)
|
||||
if err then
|
||||
log.line("watcher", "event_cb '%s' '%s' FAIL : %s", self._path, filename, err)
|
||||
local message = string.format("File system watcher failed (%s) for path %s, halting watcher.", err, self._path)
|
||||
log.line("watcher", "event_cb '%s' '%s' FAIL : %s", self.path, filename, err)
|
||||
local message = string.format("File system watcher failed (%s) for path %s, halting watcher.", err, self.path)
|
||||
if err == "EPERM" and (utils.is_windows or utils.is_wsl) then
|
||||
-- on directory removal windows will cascade the filesystem events out of order
|
||||
log.line("watcher", message)
|
||||
@ -69,19 +75,19 @@ function Event:start()
|
||||
self:destroy(message)
|
||||
end
|
||||
else
|
||||
log.line("watcher", "event_cb '%s' '%s'", self._path, filename)
|
||||
for _, listener in ipairs(self._listeners) do
|
||||
log.line("watcher", "event_cb '%s' '%s'", self.path, filename)
|
||||
for _, listener in ipairs(self.listeners) do
|
||||
listener(filename)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
rc, _, name = self._fs_event:start(self._path, FS_EVENT_FLAGS, event_cb)
|
||||
rc, _, name = self.fs_event:start(self.path, FS_EVENT_FLAGS, event_cb)
|
||||
if rc ~= 0 then
|
||||
if name == "EMFILE" then
|
||||
M.disable_watchers("fs.inotify.max_user_watches exceeded, see https://github.com/nvim-tree/nvim-tree.lua/wiki/Troubleshooting")
|
||||
else
|
||||
notify.warn(string.format("Could not start the fs_event watcher for path %s : %s", self._path, name))
|
||||
notify.warn(string.format("Could not start the fs_event watcher for path %s : %s", self.path, name))
|
||||
end
|
||||
return false
|
||||
end
|
||||
@ -91,81 +97,102 @@ end
|
||||
|
||||
---@param listener function
|
||||
function Event:add(listener)
|
||||
table.insert(self._listeners, listener)
|
||||
table.insert(self.listeners, listener)
|
||||
end
|
||||
|
||||
---@param listener function
|
||||
function Event:remove(listener)
|
||||
utils.array_remove(self._listeners, listener)
|
||||
if #self._listeners == 0 then
|
||||
utils.array_remove(self.listeners, listener)
|
||||
if #self.listeners == 0 then
|
||||
self:destroy()
|
||||
end
|
||||
end
|
||||
|
||||
---@param message string|nil
|
||||
function Event:destroy(message)
|
||||
log.line("watcher", "Event:destroy '%s'", self._path)
|
||||
log.line("watcher", "Event:destroy '%s'", self.path)
|
||||
|
||||
if self._fs_event then
|
||||
if self.fs_event then
|
||||
if message then
|
||||
notify.warn(message)
|
||||
end
|
||||
|
||||
local rc, _, name = self._fs_event:stop()
|
||||
local rc, _, name = self.fs_event:stop()
|
||||
if rc ~= 0 then
|
||||
notify.warn(string.format("Could not stop the fs_event watcher for path %s : %s", self._path, name))
|
||||
notify.warn(string.format("Could not stop the fs_event watcher for path %s : %s", self.path, name))
|
||||
end
|
||||
self._fs_event = nil
|
||||
self.fs_event = nil
|
||||
end
|
||||
|
||||
Event._events[self._path] = nil
|
||||
|
||||
self.destroyed = true
|
||||
events[self.path] = nil
|
||||
end
|
||||
|
||||
---Static factory method
|
||||
---Creates and starts a Watcher
|
||||
---@class (exact) Watcher: Class
|
||||
---@field data table user data
|
||||
---@field destroyed boolean
|
||||
---@field private path string
|
||||
---@field private callback fun(watcher: Watcher)
|
||||
---@field private files string[]?
|
||||
---@field private listener fun(filename: string)?
|
||||
---@field private event Event
|
||||
local Watcher = Class:new()
|
||||
|
||||
---Registry of all watchers
|
||||
---@type Watcher[]
|
||||
local watchers = {}
|
||||
|
||||
---Static factory method
|
||||
---@param path string
|
||||
---@param files string[]|nil
|
||||
---@param callback function
|
||||
---@param data table
|
||||
---@param callback fun(watcher: Watcher)
|
||||
---@param data table user data
|
||||
---@return Watcher|nil
|
||||
function Watcher:new(path, files, callback, data)
|
||||
log.line("watcher", "Watcher:new '%s' %s", path, vim.inspect(files))
|
||||
function Watcher:create(path, files, callback, data)
|
||||
log.line("watcher", "Watcher:create '%s' %s", path, vim.inspect(files))
|
||||
|
||||
local w = setmetatable(data, Watcher)
|
||||
|
||||
w._event = Event._events[path] or Event:new(path)
|
||||
w._listener = nil
|
||||
w._path = path
|
||||
w._files = files
|
||||
w._callback = callback
|
||||
|
||||
if not w._event then
|
||||
local event = events[path] or Event:create(path)
|
||||
if not event then
|
||||
return nil
|
||||
end
|
||||
|
||||
w:start()
|
||||
---@type Watcher
|
||||
local o = {
|
||||
data = data,
|
||||
destroyed = false,
|
||||
path = path,
|
||||
callback = callback,
|
||||
files = files,
|
||||
listener = nil,
|
||||
event = event,
|
||||
}
|
||||
o = self:new(o) --[[@as Watcher]]
|
||||
|
||||
table.insert(Watcher._watchers, w)
|
||||
o:start()
|
||||
|
||||
return w
|
||||
table.insert(watchers, o)
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
function Watcher:start()
|
||||
self._listener = function(filename)
|
||||
if not self._files or vim.tbl_contains(self._files, filename) then
|
||||
self._callback(self)
|
||||
self.listener = function(filename)
|
||||
if not self.files or vim.tbl_contains(self.files, filename) then
|
||||
self.callback(self)
|
||||
end
|
||||
end
|
||||
|
||||
self._event:add(self._listener)
|
||||
self.event:add(self.listener)
|
||||
end
|
||||
|
||||
function Watcher:destroy()
|
||||
log.line("watcher", "Watcher:destroy '%s'", self._path)
|
||||
log.line("watcher", "Watcher:destroy '%s'", self.path)
|
||||
|
||||
self._event:remove(self._listener)
|
||||
self.event:remove(self.listener)
|
||||
|
||||
utils.array_remove(Watcher._watchers, self)
|
||||
utils.array_remove(watchers, self)
|
||||
|
||||
self.destroyed = true
|
||||
end
|
||||
@ -183,11 +210,11 @@ end
|
||||
function M.purge_watchers()
|
||||
log.line("watcher", "purge_watchers")
|
||||
|
||||
for _, w in ipairs(utils.array_shallow_clone(Watcher._watchers)) do
|
||||
for _, w in ipairs(utils.array_shallow_clone(watchers)) do
|
||||
w:destroy()
|
||||
end
|
||||
|
||||
for _, e in pairs(Event._events) do
|
||||
for _, e in pairs(events) do
|
||||
e:destroy()
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user