chore: class new is now generic
This commit is contained in:
@@ -34,7 +34,7 @@ end
|
|||||||
|
|
||||||
---@param node Node?
|
---@param node Node?
|
||||||
function M.fn(node)
|
function M.fn(node)
|
||||||
node = node or core.get_explorer() --[[@as Node]]
|
node = node or core.get_explorer()
|
||||||
if not node then
|
if not node then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,22 +1,24 @@
|
|||||||
---Generic class, useful for inheritence.
|
---Generic class, useful for inheritence.
|
||||||
---@class (exact) Class
|
---@class (exact) Class
|
||||||
---@field private __index? table
|
|
||||||
local Class = {}
|
local Class = {}
|
||||||
|
|
||||||
---@param o Class?
|
---@generic T
|
||||||
---@return Class
|
---@param self T
|
||||||
|
---@param o T|nil
|
||||||
|
---@return T
|
||||||
function Class:new(o)
|
function Class:new(o)
|
||||||
o = o or {}
|
o = o or {}
|
||||||
|
|
||||||
setmetatable(o, self)
|
setmetatable(o, self)
|
||||||
self.__index = self
|
self.__index = self ---@diagnostic disable-line: inject-field
|
||||||
|
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
---Object is an instance of class
|
---Object is an instance of class
|
||||||
---This will start with the lowest class and loop over all the superclasses.
|
---This will start with the lowest class and loop over all the superclasses.
|
||||||
---@param class table
|
---@generic T
|
||||||
|
---@param class T
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Class:is(class)
|
function Class:is(class)
|
||||||
local mt = getmetatable(self)
|
local mt = getmetatable(self)
|
||||||
@@ -32,7 +34,7 @@ end
|
|||||||
---Return object if it is an instance of class, otherwise nil
|
---Return object if it is an instance of class, otherwise nil
|
||||||
---@generic T
|
---@generic T
|
||||||
---@param class T
|
---@param class T
|
||||||
---@return `T`|nil
|
---@return T|nil
|
||||||
function Class:as(class)
|
function Class:as(class)
|
||||||
return self:is(class) and self or nil
|
return self:is(class) and self or nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ function Explorer:create(path)
|
|||||||
|
|
||||||
local o = RootNode:create(explorer_placeholder, path, "..", nil)
|
local o = RootNode:create(explorer_placeholder, path, "..", nil)
|
||||||
|
|
||||||
o = self:new(o) --[[@as Explorer]]
|
o = self:new(o)
|
||||||
|
|
||||||
o.explorer = o
|
o.explorer = o
|
||||||
|
|
||||||
|
|||||||
@@ -246,7 +246,7 @@ function GitRunner:run(opts)
|
|||||||
opts = opts,
|
opts = opts,
|
||||||
statuses = {},
|
statuses = {},
|
||||||
}
|
}
|
||||||
runner = GitRunner:new(runner) --[[@as GitRunner]]
|
runner = GitRunner:new(runner)
|
||||||
|
|
||||||
return runner:execute()
|
return runner:execute()
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name
|
|||||||
-- create DirectoryNode with the target path for the watcher
|
-- create DirectoryNode with the target path for the watcher
|
||||||
local o = DirectoryNode:create(explorer, parent, link_to, name, fs_stat)
|
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
|
-- reset absolute path to the link itself
|
||||||
o.absolute_path = absolute_path
|
o.absolute_path = absolute_path
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
|
|||||||
open = false,
|
open = false,
|
||||||
hidden_stats = nil,
|
hidden_stats = nil,
|
||||||
}
|
}
|
||||||
o = self:new(o) --[[@as DirectoryNode]]
|
o = self:new(o)
|
||||||
|
|
||||||
o.watcher = require("nvim-tree.explorer.watch").create_watcher(o)
|
o.watcher = require("nvim-tree.explorer.watch").create_watcher(o)
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ local FileLinkNode = FileNode:new()
|
|||||||
function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
|
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)
|
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.type = "link"
|
||||||
o.link_to = link_to
|
o.link_to = link_to
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
|
|||||||
|
|
||||||
extension = string.match(name, ".?[^.]+%.(.*)") or "",
|
extension = string.match(name, ".?[^.]+%.(.*)") or "",
|
||||||
}
|
}
|
||||||
o = self:new(o) --[[@as FileNode]]
|
o = self:new(o)
|
||||||
|
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ local RootNode = DirectoryNode:new()
|
|||||||
function RootNode:create(explorer, absolute_path, name, fs_stat)
|
function RootNode:create(explorer, absolute_path, name, fs_stat)
|
||||||
local o = DirectoryNode:create(explorer, nil, 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
|
return o
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ function DecoratorBookmarks:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_bookmarks] or HL_POSITION.none,
|
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,
|
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
|
if opts.renderer.icons.show.bookmarks then
|
||||||
o.icon = {
|
o.icon = {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ function DecoratorCopied:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
|
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
|
||||||
icon_placement = ICON_PLACEMENT.none,
|
icon_placement = ICON_PLACEMENT.none,
|
||||||
}
|
}
|
||||||
o = self:new(o) --[[@as DecoratorCopied]]
|
o = self:new(o)
|
||||||
|
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ function DecoratorCut:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
|
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
|
||||||
icon_placement = ICON_PLACEMENT.none,
|
icon_placement = ICON_PLACEMENT.none,
|
||||||
}
|
}
|
||||||
o = self:new(o) --[[@as DecoratorCut]]
|
o = self:new(o)
|
||||||
|
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ function DecoratorDiagnostics:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_diagnostics] or HL_POSITION.none,
|
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,
|
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
|
if not o.enabled then
|
||||||
return o
|
return o
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ function DecoratorGit:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_git] or HL_POSITION.none,
|
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,
|
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
|
if not o.enabled then
|
||||||
return o
|
return o
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ function DecoratorHidden:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_hidden] or HL_POSITION.none,
|
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,
|
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
|
if opts.renderer.icons.show.hidden then
|
||||||
o.icon = {
|
o.icon = {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ function DecoratorModified:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_modified] or HL_POSITION.none,
|
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,
|
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
|
if not o.enabled then
|
||||||
return o
|
return o
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ function DecoratorOpened:create(opts, explorer)
|
|||||||
hl_pos = HL_POSITION[opts.renderer.highlight_opened_files] or HL_POSITION.none,
|
hl_pos = HL_POSITION[opts.renderer.highlight_opened_files] or HL_POSITION.none,
|
||||||
icon_placement = ICON_PLACEMENT.none,
|
icon_placement = ICON_PLACEMENT.none,
|
||||||
}
|
}
|
||||||
o = self:new(o) --[[@as DecoratorOpened]]
|
o = self:new(o)
|
||||||
|
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ function Event:create(path)
|
|||||||
fs_event = nil,
|
fs_event = nil,
|
||||||
listeners = {},
|
listeners = {},
|
||||||
}
|
}
|
||||||
o = self:new(o) --[[@as Event]]
|
o = self:new(o)
|
||||||
|
|
||||||
if o:start() then
|
if o:start() then
|
||||||
events[path] = o
|
events[path] = o
|
||||||
@@ -168,7 +168,7 @@ function Watcher:create(path, files, callback, data)
|
|||||||
listener = nil,
|
listener = nil,
|
||||||
event = event,
|
event = event,
|
||||||
}
|
}
|
||||||
o = self:new(o) --[[@as Watcher]]
|
o = self:new(o)
|
||||||
|
|
||||||
o:start()
|
o:start()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user