chore: class new is now generic

This commit is contained in:
Alexander Courtis 2024-10-28 14:00:52 +11:00
parent 5647bc3c7c
commit 9b365279df
18 changed files with 26 additions and 24 deletions

View File

@ -34,7 +34,7 @@ end
---@param node Node?
function M.fn(node)
node = node or core.get_explorer() --[[@as Node]]
node = node or core.get_explorer()
if not node then
return
end

View File

@ -1,22 +1,24 @@
---Generic class, useful for inheritence.
---@class (exact) Class
---@field private __index? table
local Class = {}
---@param o Class?
---@return Class
---@generic T
---@param self T
---@param o T|nil
---@return T
function Class:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
self.__index = self ---@diagnostic disable-line: inject-field
return o
end
---Object is an instance of class
---This will start with the lowest class and loop over all the superclasses.
---@param class table
---@generic T
---@param class T
---@return boolean
function Class:is(class)
local mt = getmetatable(self)
@ -32,7 +34,7 @@ end
---Return object if it is an instance of class, otherwise nil
---@generic T
---@param class T
---@return `T`|nil
---@return T|nil
function Class:as(class)
return self:is(class) and self or nil
end

View File

@ -59,7 +59,7 @@ function Explorer:create(path)
local o = RootNode:create(explorer_placeholder, path, "..", nil)
o = self:new(o) --[[@as Explorer]]
o = self:new(o)
o.explorer = o

View File

@ -246,7 +246,7 @@ function GitRunner:run(opts)
opts = opts,
statuses = {},
}
runner = GitRunner:new(runner) --[[@as GitRunner]]
runner = GitRunner:new(runner)
return runner:execute()
end

View File

@ -20,7 +20,7 @@ function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name
-- create DirectoryNode with the target path for the watcher
local o = DirectoryNode:create(explorer, parent, link_to, name, fs_stat)
o = self:new(o) --[[@as DirectoryLinkNode]]
o = self:new(o)
-- reset absolute path to the link itself
o.absolute_path = absolute_path

View File

@ -43,7 +43,7 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
open = false,
hidden_stats = nil,
}
o = self:new(o) --[[@as DirectoryNode]]
o = self:new(o)
o.watcher = require("nvim-tree.explorer.watch").create_watcher(o)

View File

@ -19,7 +19,7 @@ local FileLinkNode = FileNode:new()
function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
local o = FileNode:create(explorer, parent, absolute_path, name, fs_stat)
o = self:new(o) --[[@as FileLinkNode]]
o = self:new(o)
o.type = "link"
o.link_to = link_to

View File

@ -31,7 +31,7 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
extension = string.match(name, ".?[^.]+%.(.*)") or "",
}
o = self:new(o) --[[@as FileNode]]
o = self:new(o)
return o
end

View File

@ -12,7 +12,7 @@ local RootNode = DirectoryNode:new()
function RootNode:create(explorer, absolute_path, name, fs_stat)
local o = DirectoryNode:create(explorer, nil, absolute_path, name, fs_stat)
o = self:new(o) --[[@as RootNode]]
o = self:new(o)
return o
end

View File

@ -19,7 +19,7 @@ function DecoratorBookmarks:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_bookmarks] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorBookmarks]]
o = self:new(o)
if opts.renderer.icons.show.bookmarks then
o.icon = {

View File

@ -19,7 +19,7 @@ function DecoratorCopied:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorCopied]]
o = self:new(o)
return o
end

View File

@ -18,7 +18,7 @@ function DecoratorCut:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorCut]]
o = self:new(o)
return o
end

View File

@ -49,7 +49,7 @@ function DecoratorDiagnostics:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_diagnostics] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[opts.renderer.icons.diagnostics_placement] or ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorDiagnostics]]
o = self:new(o)
if not o.enabled then
return o

View File

@ -34,7 +34,7 @@ function DecoratorGit:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_git] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorGit]]
o = self:new(o)
if not o.enabled then
return o

View File

@ -20,7 +20,7 @@ function DecoratorHidden:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_hidden] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[opts.renderer.icons.hidden_placement] or ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorHidden]]
o = self:new(o)
if opts.renderer.icons.show.hidden then
o.icon = {

View File

@ -22,7 +22,7 @@ function DecoratorModified:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_modified] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[opts.renderer.icons.modified_placement] or ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorModified]]
o = self:new(o)
if not o.enabled then
return o

View File

@ -21,7 +21,7 @@ function DecoratorOpened:create(opts, explorer)
hl_pos = HL_POSITION[opts.renderer.highlight_opened_files] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT.none,
}
o = self:new(o) --[[@as DecoratorOpened]]
o = self:new(o)
return o
end

View File

@ -40,7 +40,7 @@ function Event:create(path)
fs_event = nil,
listeners = {},
}
o = self:new(o) --[[@as Event]]
o = self:new(o)
if o:start() then
events[path] = o
@ -168,7 +168,7 @@ function Watcher:create(path, files, callback, data)
listener = nil,
event = event,
}
o = self:new(o) --[[@as Watcher]]
o = self:new(o)
o:start()