Skip to content

Commit a7db5b2

Browse files
committed
typechecked optargs constructors for watcher and event
1 parent ac302ae commit a7db5b2

File tree

11 files changed

+94
-68
lines changed

11 files changed

+94
-68
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ local config
3838
local Explorer = RootNode:extend()
3939

4040
---@class Explorer
41-
---@overload fun(opts: ExplorerArgs): Explorer
41+
---@overload fun(args: ExplorerArgs): Explorer
4242

4343
---@class (exact) ExplorerArgs
4444
---@field path string

lua/nvim-tree/explorer/watch.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ function M.create_watcher(node)
8383
end
8484

8585
M.uid = M.uid + 1
86-
return Watcher:create(path, nil, callback, {
87-
context = "explorer:watch:" .. path .. ":" .. M.uid,
86+
return Watcher:create({
87+
path = path,
88+
callback = callback,
89+
data = {
90+
context = "explorer:watch:" .. path .. ":" .. M.uid
91+
}
8892
})
8993
end
9094

lua/nvim-tree/git/init.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,13 @@ function M.load_project(path)
298298
end
299299

300300
local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join({ toplevel, ".git" })
301-
watcher = Watcher:create(git_dir, WATCHED_FILES, callback, {
302-
toplevel = toplevel,
301+
watcher = Watcher:create({
302+
path = git_dir,
303+
files = WATCHED_FILES,
304+
callback = callback,
305+
data = {
306+
toplevel = toplevel,
307+
}
303308
})
304309
end
305310

lua/nvim-tree/node/directory-link.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local DirectoryLinkNode = DirectoryNode:extend()
99
DirectoryLinkNode:implement(LinkNode)
1010

1111
---@class DirectoryLinkNode
12-
---@overload fun(opts: LinkNodeArgs): DirectoryLinkNode
12+
---@overload fun(args: LinkNodeArgs): DirectoryLinkNode
1313

1414
---@protected
1515
---@param args LinkNodeArgs

lua/nvim-tree/node/directory.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local Node = require("nvim-tree.node")
1313
local DirectoryNode = Node:extend()
1414

1515
---@class DirectoryNode
16-
---@overload fun(opts: NodeArgs): DirectoryNode
16+
---@overload fun(args: NodeArgs): DirectoryNode
1717

1818
---@protected
1919
---@param args NodeArgs

lua/nvim-tree/node/file-link.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local FileLinkNode = FileNode:extend()
99
FileLinkNode:implement(LinkNode)
1010

1111
---@class FileLinkNode
12-
---@overload fun(opts: LinkNodeArgs): FileLinkNode
12+
---@overload fun(args: LinkNodeArgs): FileLinkNode
1313

1414
---@protected
1515
---@param args LinkNodeArgs

lua/nvim-tree/node/file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ local PICTURE_MAP = {
1818
local FileNode = Node:extend()
1919

2020
---@class FileNode
21-
---@overload fun(opts: NodeArgs): FileNode
21+
---@overload fun(args: NodeArgs): FileNode
2222

2323
---@protected
2424
---@param args NodeArgs

lua/nvim-tree/node/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
local Object = require("nvim-tree.classic")
1+
local Class = require("nvim-tree.classic")
22

33
---Abstract Node class.
4-
---@class (exact) Node: Object
4+
---@class (exact) Node: Class
55
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
66
---@field explorer Explorer
77
---@field absolute_path string
@@ -13,7 +13,7 @@ local Object = require("nvim-tree.classic")
1313
---@field parent DirectoryNode?
1414
---@field diag_status DiagStatus?
1515
---@field private is_dot boolean cached is_dotfile
16-
local Node = Object:extend()
16+
local Node = Class:extend()
1717

1818
---@class (exact) NodeArgs
1919
---@field explorer Explorer

lua/nvim-tree/node/link.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
local Object = require("nvim-tree.classic")
1+
local Class = require("nvim-tree.classic")
22

3-
---@class (exact) LinkNode: Object
3+
---@class (exact) LinkNode: Class
44
---@field link_to string
55
---@field protected fs_stat_target uv.fs_stat.result
6-
local LinkNode = Object:extend()
6+
local LinkNode = Class:extend()
77

88
---@class (exact) LinkNodeArgs: NodeArgs
99
---@field link_to string
@@ -12,8 +12,6 @@ local LinkNode = Object:extend()
1212
---@protected
1313
---@param args LinkNodeArgs
1414
function LinkNode:new(args)
15-
LinkNode.super.new(self, args)
16-
1715
self.link_to = args.link_to
1816
self.fs_stat_target = args.fs_stat_target
1917
end

lua/nvim-tree/node/root.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local DirectoryNode = require("nvim-tree.node.directory")
44
local RootNode = DirectoryNode:extend()
55

66
---@class RootNode
7-
---@overload fun(opts: NodeArgs): RootNode
7+
---@overload fun(args: NodeArgs): RootNode
88

99
---@protected
1010
---@param args NodeArgs

lua/nvim-tree/watcher.lua

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local notify = require("nvim-tree.notify")
22
local log = require("nvim-tree.log")
33
local utils = require("nvim-tree.utils")
44

5-
local Class = require("nvim-tree.class")
5+
local Class = require("nvim-tree.classic")
66

77
local FS_EVENT_FLAGS = {
88
-- inotify or equivalent will be used; fallback to stat has not yet been implemented
@@ -15,36 +15,45 @@ local M = {
1515
config = {},
1616
}
1717

18+
---Registry of all events
19+
---@type Event[]
20+
local events = {}
21+
1822
---@class (exact) Event: Class
1923
---@field destroyed boolean
2024
---@field private path string
2125
---@field private fs_event uv.uv_fs_event_t?
2226
---@field private listeners function[]
23-
local Event = Class:new()
27+
local Event = Class:extend()
2428

25-
---Registry of all events
26-
---@type Event[]
27-
local events = {}
29+
---@class Event
30+
---@overload fun(args: EventArgs): Event
31+
32+
---@class (exact) EventArgs
33+
---@field path string
34+
35+
---@private
36+
---@param args EventArgs
37+
function Event:new(args)
38+
self.destroyed = false
39+
self.path = args.path
40+
self.fs_event = nil
41+
self.listeners = {}
42+
end
2843

2944
---Static factory method
3045
---Creates and starts an Event
31-
---@param path string
32-
---@return Event|nil
33-
function Event:create(path)
34-
log.line("watcher", "Event:create '%s'", path)
35-
36-
---@type Event
37-
local o = {
38-
destroyed = false,
39-
path = path,
40-
fs_event = nil,
41-
listeners = {},
42-
}
43-
o = self:new(o)
44-
45-
if o:start() then
46-
events[path] = o
47-
return o
46+
---nil on failure to start
47+
---@param args EventArgs
48+
---@return Event?
49+
function Event:create(args)
50+
log.line("watcher", "Event:create '%s'", args.path)
51+
52+
local event = Event(args)
53+
54+
if event:start() then
55+
events[event.path] = event
56+
return event
4857
else
4958
return nil
5059
end
@@ -128,8 +137,10 @@ function Event:destroy(message)
128137
events[self.path] = nil
129138
end
130139

131-
---Static factory method
132-
---Creates and starts a Watcher
140+
---Registry of all watchers
141+
---@type Watcher[]
142+
local watchers = {}
143+
133144
---@class (exact) Watcher: Class
134145
---@field data table user data
135146
---@field destroyed boolean
@@ -138,43 +149,51 @@ end
138149
---@field private files string[]?
139150
---@field private listener fun(filename: string)?
140151
---@field private event Event
141-
local Watcher = Class:new()
142-
143-
---Registry of all watchers
144-
---@type Watcher[]
145-
local watchers = {}
152+
local Watcher = Class:extend()
153+
154+
---@class Watcher
155+
---@overload fun(args: WatcherArgs): Watcher
156+
157+
---@class (exact) WatcherArgs
158+
---@field path string
159+
---@field files string[]|nil
160+
---@field callback fun(watcher: Watcher)
161+
---@field data table? user data
162+
163+
---@private
164+
---@param args WatcherArgs
165+
function Watcher:new(args)
166+
self.data = args.data
167+
self.destroyed = false
168+
self.path = args.path
169+
self.callback = args.callback
170+
self.files = args.files
171+
self.listener = nil
172+
self.event = args.event
173+
end
146174

147175
---Static factory method
148-
---@param path string
149-
---@param files string[]|nil
150-
---@param callback fun(watcher: Watcher)
151-
---@param data table user data
176+
---Creates and starts a Watcher
177+
---nil on failure to create Event
178+
---@param args WatcherArgs
152179
---@return Watcher|nil
153-
function Watcher:create(path, files, callback, data)
154-
log.line("watcher", "Watcher:create '%s' %s", path, vim.inspect(files))
180+
function Watcher:create(args)
181+
log.line("watcher", "Watcher:create '%s' %s", args.path, vim.inspect(args.files))
155182

156-
local event = events[path] or Event:create(path)
183+
local event = events[args.path] or Event:create({ path = args.path })
157184
if not event then
158185
return nil
159186
end
160187

161-
---@type Watcher
162-
local o = {
163-
data = data,
164-
destroyed = false,
165-
path = path,
166-
callback = callback,
167-
files = files,
168-
listener = nil,
169-
event = event,
170-
}
171-
o = self:new(o)
188+
local watcher = Watcher(args)
189+
190+
watcher.event = event
172191

173-
o:start()
192+
watcher:start()
174193

175-
table.insert(watchers, o)
194+
table.insert(watchers, watcher)
176195

177-
return o
196+
return watcher
178197
end
179198

180199
function Watcher:start()

0 commit comments

Comments
 (0)