chore: resolve undefined-field

This commit is contained in:
Alexander Courtis
2024-10-26 14:55:46 +11:00
parent 52ff301e07
commit d663059c3e
3 changed files with 107 additions and 78 deletions

View File

@@ -66,9 +66,10 @@ function M.create_watcher(node)
return nil return nil
end end
---@param watcher Watcher
local function callback(watcher) local function callback(watcher)
log.line("watcher", "node event scheduled refresh %s", watcher.context) log.line("watcher", "node event scheduled refresh %s", watcher.data.context)
utils.debounce(watcher.context, M.config.filesystem_watchers.debounce_delay, function() utils.debounce(watcher.data.context, M.config.filesystem_watchers.debounce_delay, function()
if watcher.destroyed then if watcher.destroyed then
return return
end end
@@ -82,7 +83,7 @@ function M.create_watcher(node)
end end
M.uid = M.uid + 1 M.uid = M.uid + 1
return Watcher:new(path, nil, callback, { return Watcher:create(path, nil, callback, {
context = "explorer:watch:" .. path .. ":" .. M.uid, context = "explorer:watch:" .. path .. ":" .. M.uid,
}) })
end end

View File

@@ -260,18 +260,19 @@ function M.load_project_status(path)
if M.config.filesystem_watchers.enable then if M.config.filesystem_watchers.enable then
log.line("watcher", "git start") log.line("watcher", "git start")
---@param w Watcher
local callback = function(w) local callback = function(w)
log.line("watcher", "git event scheduled '%s'", w.toplevel) log.line("watcher", "git event scheduled '%s'", w.data.toplevel)
utils.debounce("git:watcher:" .. w.toplevel, M.config.filesystem_watchers.debounce_delay, function() utils.debounce("git:watcher:" .. w.data.toplevel, M.config.filesystem_watchers.debounce_delay, function()
if w.destroyed then if w.destroyed then
return return
end end
reload_tree_at(w.toplevel) reload_tree_at(w.data.toplevel)
end) end)
end end
local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join({ toplevel, ".git" }) 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, toplevel = toplevel,
}) })
end end

View File

@@ -2,21 +2,7 @@ local notify = require("nvim-tree.notify")
local log = require("nvim-tree.log") local log = require("nvim-tree.log")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local M = { local Class = require("nvim-tree.class")
config = {},
}
---@class Event
local Event = {
_events = {},
}
Event.__index = Event
---@class Watcher
local Watcher = {
_watchers = {},
}
Watcher.__index = Watcher
local FS_EVENT_FLAGS = { local FS_EVENT_FLAGS = {
-- inotify or equivalent will be used; fallback to stat has not yet been implemented -- inotify or equivalent will be used; fallback to stat has not yet been implemented
@@ -25,20 +11,40 @@ local FS_EVENT_FLAGS = {
recursive = false, 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 ---@param path string
---@return Event|nil ---@return Event|nil
function Event:new(path) function Event:create(path)
log.line("watcher", "Event:new '%s'", path) log.line("watcher", "Event:create '%s'", path)
local e = setmetatable({ ---@type Event
_path = path, local o = {
_fs_event = nil, destroyed = false,
_listeners = {}, path = path,
}, Event) fs_event = nil,
listeners = {},
}
o = self:new(o) --[[@as Event]]
if e:start() then if o:start() then
Event._events[path] = e events[path] = o
return e return o
else else
return nil return nil
end end
@@ -46,21 +52,21 @@ end
---@return boolean ---@return boolean
function Event:start() function Event:start()
log.line("watcher", "Event:start '%s'", self._path) log.line("watcher", "Event:start '%s'", self.path)
local rc, _, name local rc, _, name
self._fs_event, _, name = vim.loop.new_fs_event() self.fs_event, _, name = vim.loop.new_fs_event()
if not self._fs_event then if not self.fs_event then
self._fs_event = nil self.fs_event = nil
notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self._path, name)) notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self.path, name))
return false return false
end end
local event_cb = vim.schedule_wrap(function(err, filename) local event_cb = vim.schedule_wrap(function(err, filename)
if err then if err then
log.line("watcher", "event_cb '%s' '%s' FAIL : %s", self._path, filename, err) 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) 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 if err == "EPERM" and (utils.is_windows or utils.is_wsl) then
-- on directory removal windows will cascade the filesystem events out of order -- on directory removal windows will cascade the filesystem events out of order
log.line("watcher", message) log.line("watcher", message)
@@ -69,19 +75,19 @@ function Event:start()
self:destroy(message) self:destroy(message)
end end
else else
log.line("watcher", "event_cb '%s' '%s'", self._path, filename) log.line("watcher", "event_cb '%s' '%s'", self.path, filename)
for _, listener in ipairs(self._listeners) do for _, listener in ipairs(self.listeners) do
listener(filename) listener(filename)
end end
end 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 rc ~= 0 then
if name == "EMFILE" 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") M.disable_watchers("fs.inotify.max_user_watches exceeded, see https://github.com/nvim-tree/nvim-tree.lua/wiki/Troubleshooting")
else 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 end
return false return false
end end
@@ -91,81 +97,102 @@ end
---@param listener function ---@param listener function
function Event:add(listener) function Event:add(listener)
table.insert(self._listeners, listener) table.insert(self.listeners, listener)
end end
---@param listener function ---@param listener function
function Event:remove(listener) function Event:remove(listener)
utils.array_remove(self._listeners, listener) utils.array_remove(self.listeners, listener)
if #self._listeners == 0 then if #self.listeners == 0 then
self:destroy() self:destroy()
end end
end end
---@param message string|nil ---@param message string|nil
function Event:destroy(message) 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 if message then
notify.warn(message) notify.warn(message)
end end
local rc, _, name = self._fs_event:stop() local rc, _, name = self.fs_event:stop()
if rc ~= 0 then 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 end
self._fs_event = nil self.fs_event = nil
end end
Event._events[self._path] = nil
self.destroyed = true self.destroyed = true
events[self.path] = nil
end 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 path string
---@param files string[]|nil ---@param files string[]|nil
---@param callback function ---@param callback fun(watcher: Watcher)
---@param data table ---@param data table user data
---@return Watcher|nil ---@return Watcher|nil
function Watcher:new(path, files, callback, data) function Watcher:create(path, files, callback, data)
log.line("watcher", "Watcher:new '%s' %s", path, vim.inspect(files)) log.line("watcher", "Watcher:create '%s' %s", path, vim.inspect(files))
local w = setmetatable(data, Watcher) local event = events[path] or Event:create(path)
if not event then
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
return nil return nil
end 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 end
function Watcher:start() function Watcher:start()
self._listener = function(filename) self.listener = function(filename)
if not self._files or vim.tbl_contains(self._files, filename) then if not self.files or vim.tbl_contains(self.files, filename) then
self._callback(self) self.callback(self)
end end
end end
self._event:add(self._listener) self.event:add(self.listener)
end end
function Watcher:destroy() 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 self.destroyed = true
end end
@@ -183,11 +210,11 @@ end
function M.purge_watchers() function M.purge_watchers()
log.line("watcher", "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() w:destroy()
end end
for _, e in pairs(Event._events) do for _, e in pairs(events) do
e:destroy() e:destroy()
end end
end end