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()
---@class Explorer
---@overload fun(opts: ExplorerArgs): Explorer
---@overload fun(args: ExplorerArgs): Explorer
---@class (exact) ExplorerArgs
---@field path string

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
local Object = require("nvim-tree.classic")
local Class = require("nvim-tree.classic")
---Abstract Node class.
---@class (exact) Node: Object
---@class (exact) Node: Class
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
---@field explorer Explorer
---@field absolute_path string
@@ -13,7 +13,7 @@ local Object = require("nvim-tree.classic")
---@field parent DirectoryNode?
---@field diag_status DiagStatus?
---@field private is_dot boolean cached is_dotfile
local Node = Object:extend()
local Node = Class:extend()
---@class (exact) NodeArgs
---@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 protected fs_stat_target uv.fs_stat.result
local LinkNode = Object:extend()
local LinkNode = Class:extend()
---@class (exact) LinkNodeArgs: NodeArgs
---@field link_to string
@@ -12,8 +12,6 @@ local LinkNode = Object:extend()
---@protected
---@param args LinkNodeArgs
function LinkNode:new(args)
LinkNode.super.new(self, args)
self.link_to = args.link_to
self.fs_stat_target = args.fs_stat_target
end

View File

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

View File

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