typechecked optargs constructors for watcher and event

This commit is contained in:
Alexander Courtis
2024-11-06 16:36:14 +11:00
parent ac302ae16a
commit a7db5b29a4
11 changed files with 91 additions and 65 deletions

View File

@@ -38,7 +38,7 @@ local config
local Explorer = RootNode:extend() local Explorer = RootNode:extend()
---@class Explorer ---@class Explorer
---@overload fun(opts: ExplorerArgs): Explorer ---@overload fun(args: ExplorerArgs): Explorer
---@class (exact) ExplorerArgs ---@class (exact) ExplorerArgs
---@field path string ---@field path string

View File

@@ -83,8 +83,12 @@ function M.create_watcher(node)
end end
M.uid = M.uid + 1 M.uid = M.uid + 1
return Watcher:create(path, nil, callback, { return Watcher:create({
context = "explorer:watch:" .. path .. ":" .. M.uid, path = path,
callback = callback,
data = {
context = "explorer:watch:" .. path .. ":" .. M.uid
}
}) })
end end

View File

@@ -298,8 +298,13 @@ function M.load_project(path)
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:create(git_dir, WATCHED_FILES, callback, { watcher = Watcher:create({
toplevel = toplevel, path = git_dir,
files = WATCHED_FILES,
callback = callback,
data = {
toplevel = toplevel,
}
}) })
end end

View File

@@ -9,7 +9,7 @@ local DirectoryLinkNode = DirectoryNode:extend()
DirectoryLinkNode:implement(LinkNode) DirectoryLinkNode:implement(LinkNode)
---@class DirectoryLinkNode ---@class DirectoryLinkNode
---@overload fun(opts: LinkNodeArgs): DirectoryLinkNode ---@overload fun(args: LinkNodeArgs): DirectoryLinkNode
---@protected ---@protected
---@param args LinkNodeArgs ---@param args LinkNodeArgs

View File

@@ -13,7 +13,7 @@ local Node = require("nvim-tree.node")
local DirectoryNode = Node:extend() local DirectoryNode = Node:extend()
---@class DirectoryNode ---@class DirectoryNode
---@overload fun(opts: NodeArgs): DirectoryNode ---@overload fun(args: NodeArgs): DirectoryNode
---@protected ---@protected
---@param args NodeArgs ---@param args NodeArgs

View File

@@ -9,7 +9,7 @@ local FileLinkNode = FileNode:extend()
FileLinkNode:implement(LinkNode) FileLinkNode:implement(LinkNode)
---@class FileLinkNode ---@class FileLinkNode
---@overload fun(opts: LinkNodeArgs): FileLinkNode ---@overload fun(args: LinkNodeArgs): FileLinkNode
---@protected ---@protected
---@param args LinkNodeArgs ---@param args LinkNodeArgs

View File

@@ -18,7 +18,7 @@ local PICTURE_MAP = {
local FileNode = Node:extend() local FileNode = Node:extend()
---@class FileNode ---@class FileNode
---@overload fun(opts: NodeArgs): FileNode ---@overload fun(args: NodeArgs): FileNode
---@protected ---@protected
---@param args NodeArgs ---@param args NodeArgs

View File

@@ -1,7 +1,7 @@
local Object = require("nvim-tree.classic") local Class = require("nvim-tree.classic")
---Abstract Node class. ---Abstract Node class.
---@class (exact) Node: Object ---@class (exact) Node: Class
---@field type "file" | "directory" | "link" uv.fs_stat.result.type ---@field type "file" | "directory" | "link" uv.fs_stat.result.type
---@field explorer Explorer ---@field explorer Explorer
---@field absolute_path string ---@field absolute_path string
@@ -13,7 +13,7 @@ local Object = require("nvim-tree.classic")
---@field parent DirectoryNode? ---@field parent DirectoryNode?
---@field diag_status DiagStatus? ---@field diag_status DiagStatus?
---@field private is_dot boolean cached is_dotfile ---@field private is_dot boolean cached is_dotfile
local Node = Object:extend() local Node = Class:extend()
---@class (exact) NodeArgs ---@class (exact) NodeArgs
---@field explorer Explorer ---@field explorer Explorer

View File

@@ -1,9 +1,9 @@
local Object = require("nvim-tree.classic") local Class = require("nvim-tree.classic")
---@class (exact) LinkNode: Object ---@class (exact) LinkNode: Class
---@field link_to string ---@field link_to string
---@field protected fs_stat_target uv.fs_stat.result ---@field protected fs_stat_target uv.fs_stat.result
local LinkNode = Object:extend() local LinkNode = Class:extend()
---@class (exact) LinkNodeArgs: NodeArgs ---@class (exact) LinkNodeArgs: NodeArgs
---@field link_to string ---@field link_to string
@@ -12,8 +12,6 @@ local LinkNode = Object:extend()
---@protected ---@protected
---@param args LinkNodeArgs ---@param args LinkNodeArgs
function LinkNode:new(args) function LinkNode:new(args)
LinkNode.super.new(self, args)
self.link_to = args.link_to self.link_to = args.link_to
self.fs_stat_target = args.fs_stat_target self.fs_stat_target = args.fs_stat_target
end end

View File

@@ -4,7 +4,7 @@ local DirectoryNode = require("nvim-tree.node.directory")
local RootNode = DirectoryNode:extend() local RootNode = DirectoryNode:extend()
---@class RootNode ---@class RootNode
---@overload fun(opts: NodeArgs): RootNode ---@overload fun(args: NodeArgs): RootNode
---@protected ---@protected
---@param args NodeArgs ---@param args NodeArgs

View File

@@ -2,7 +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 Class = require("nvim-tree.class") local Class = require("nvim-tree.classic")
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
@@ -15,36 +15,45 @@ local M = {
config = {}, config = {},
} }
---Registry of all events
---@type Event[]
local events = {}
---@class (exact) Event: Class ---@class (exact) Event: Class
---@field destroyed boolean ---@field destroyed boolean
---@field private path string ---@field private path string
---@field private fs_event uv.uv_fs_event_t? ---@field private fs_event uv.uv_fs_event_t?
---@field private listeners function[] ---@field private listeners function[]
local Event = Class:new() local Event = Class:extend()
---Registry of all events ---@class Event
---@type Event[] ---@overload fun(args: EventArgs): Event
local events = {}
---@class (exact) EventArgs
---@field path string
---@private
---@param args EventArgs
function Event:new(args)
self.destroyed = false
self.path = args.path
self.fs_event = nil
self.listeners = {}
end
---Static factory method ---Static factory method
---Creates and starts an Event ---Creates and starts an Event
---@param path string ---nil on failure to start
---@return Event|nil ---@param args EventArgs
function Event:create(path) ---@return Event?
log.line("watcher", "Event:create '%s'", path) function Event:create(args)
log.line("watcher", "Event:create '%s'", args.path)
---@type Event local event = Event(args)
local o = {
destroyed = false,
path = path,
fs_event = nil,
listeners = {},
}
o = self:new(o)
if o:start() then if event:start() then
events[path] = o events[event.path] = event
return o return event
else else
return nil return nil
end end
@@ -128,8 +137,10 @@ function Event:destroy(message)
events[self.path] = nil events[self.path] = nil
end end
---Static factory method ---Registry of all watchers
---Creates and starts a Watcher ---@type Watcher[]
local watchers = {}
---@class (exact) Watcher: Class ---@class (exact) Watcher: Class
---@field data table user data ---@field data table user data
---@field destroyed boolean ---@field destroyed boolean
@@ -138,43 +149,51 @@ end
---@field private files string[]? ---@field private files string[]?
---@field private listener fun(filename: string)? ---@field private listener fun(filename: string)?
---@field private event Event ---@field private event Event
local Watcher = Class:new() local Watcher = Class:extend()
---Registry of all watchers ---@class Watcher
---@type Watcher[] ---@overload fun(args: WatcherArgs): Watcher
local watchers = {}
---@class (exact) WatcherArgs
---@field path string
---@field files string[]|nil
---@field callback fun(watcher: Watcher)
---@field data table? user data
---@private
---@param args WatcherArgs
function Watcher:new(args)
self.data = args.data
self.destroyed = false
self.path = args.path
self.callback = args.callback
self.files = args.files
self.listener = nil
self.event = args.event
end
---Static factory method ---Static factory method
---@param path string ---Creates and starts a Watcher
---@param files string[]|nil ---nil on failure to create Event
---@param callback fun(watcher: Watcher) ---@param args WatcherArgs
---@param data table user data
---@return Watcher|nil ---@return Watcher|nil
function Watcher:create(path, files, callback, data) function Watcher:create(args)
log.line("watcher", "Watcher:create '%s' %s", path, vim.inspect(files)) log.line("watcher", "Watcher:create '%s' %s", args.path, vim.inspect(args.files))
local event = events[path] or Event:create(path) local event = events[args.path] or Event:create({ path = args.path })
if not event then if not event then
return nil return nil
end end
---@type Watcher local watcher = Watcher(args)
local o = {
data = data,
destroyed = false,
path = path,
callback = callback,
files = files,
listener = nil,
event = event,
}
o = self:new(o)
o:start() watcher.event = event
table.insert(watchers, o) watcher:start()
return o table.insert(watchers, watcher)
return watcher
end end
function Watcher:start() function Watcher:start()