typechecked optargs constructors for Sorter

This commit is contained in:
Alexander Courtis 2024-11-06 17:07:08 +11:00
parent b2f7b9a876
commit 692fff7745
4 changed files with 36 additions and 39 deletions

View File

@ -1,12 +1,12 @@
---Generic class, useful for inheritence.
---@class (exact) Class
local Class = {}
---@class (exact) ClassOld
local ClassOld = {}
---@generic T
---@param self T
---@param o T|nil
---@return T
function Class:new(o)
function ClassOld:new(o)
o = o or {}
setmetatable(o, self)
@ -20,7 +20,7 @@ end
---@generic T
---@param class T
---@return boolean
function Class:is(class)
function ClassOld:is(class)
local mt = getmetatable(self)
while mt do
if mt == class then
@ -35,13 +35,13 @@ end
---@generic T
---@param class T
---@return T|nil
function Class:as(class)
function ClassOld:as(class)
return self:is(class) and self or nil
end
-- avoid unused param warnings in abstract methods
---@param ... any
function Class:nop(...) --luacheck: ignore 212
function ClassOld:nop(...) --luacheck: ignore 212
end
return Class
return ClassOld

View File

@ -9,18 +9,18 @@
-- https://github.com/rxi/classic
--
---@class (exact) Object
---@field super Object
---@field private implements table<Object, boolean>
local Object = {}
Object.__index = Object ---@diagnostic disable-line: inject-field
---@class (exact) Class
---@field super Class
---@field private implements table<Class, boolean>
local Class = {}
Class.__index = Class ---@diagnostic disable-line: inject-field
---Default constructor
function Object:new(...) --luacheck: ignore 212
function Class:new(...) --luacheck: ignore 212
end
---Extend a class, setting .super
function Object:extend()
function Class:extend()
local cls = {}
for k, v in pairs(self) do
if k:find("__") == 1 then
@ -35,14 +35,14 @@ end
---Implement the functions of a mixin
---Add the mixin to .implements
---@param class Object
function Object:implement(class)
---@param mixin Class
function Class:implement(mixin)
if not rawget(self, "implements") then
-- set on the class itself instead of parents
rawset(self, "implements", {})
end
self.implements[class] = true
for k, v in pairs(class) do
self.implements[mixin] = true
for k, v in pairs(mixin) do
if self[k] == nil and type(v) == "function" then
self[k] = v
end
@ -53,7 +53,7 @@ end
---@generic T
---@param class T
---@return boolean
function Object:is(class)
function Class:is(class)
local mt = getmetatable(self)
while mt do
if mt == class then
@ -71,12 +71,12 @@ end
---@generic T
---@param class T
---@return T|nil
function Object:as(class)
function Class:as(class)
return self:is(class) and self or nil
end
---Constructor to create instance, call :new and return
function Object:__call(...)
function Class:__call(...)
local obj = setmetatable({}, self)
obj:new(...)
return obj
@ -84,7 +84,7 @@ end
-- avoid unused param warnings in abstract methods
---@param ... any
function Object:nop(...) --luacheck: ignore 212
function Class:nop(...) --luacheck: ignore 212
end
return Object
return Class

View File

@ -17,7 +17,7 @@ local NodeIterator = require("nvim-tree.iterators.node-iterator")
local Filters = require("nvim-tree.explorer.filters")
local Marks = require("nvim-tree.marks")
local LiveFilter = require("nvim-tree.explorer.live-filter")
local Sorters = require("nvim-tree.explorer.sorters")
local Sorter = require("nvim-tree.explorer.sorter")
local Clipboard = require("nvim-tree.actions.fs.clipboard")
local Renderer = require("nvim-tree.renderer")
@ -57,7 +57,7 @@ function Explorer:new(args)
self.open = true
self.opts = config
self.sorters = Sorters:create(config)
self.sorters = Sorter(config)
self.renderer = Renderer:new(config, self)
self.filters = Filters:new(config, self)
self.live_filter = LiveFilter:new(config, self)

View File

@ -1,4 +1,4 @@
local Class = require("nvim-tree.class")
local Class = require("nvim-tree.classic")
local DirectoryNode = require("nvim-tree.node.directory")
local C = {}
@ -12,23 +12,20 @@ local C = {}
---@field cfg SorterCfg
---@field user fun(nodes: Node[])?
---@field pre string?
local Sorter = Class:new()
local Sorter = Class:extend()
---@class Sorter
---@overload fun(opts: table): Sorter
---@param opts table user options
---@return Sorter
function Sorter:create(opts)
---@type Sorter
local o = {
cfg = vim.deepcopy(opts.sort),
}
o = self:new(o)
function Sorter:new(opts)
self.cfg = vim.deepcopy(opts.sort)
if type(o.cfg.sorter) == "function" then
o.user = o.cfg.sorter --[[@as fun(nodes: Node[])]]
elseif type(o.cfg.sorter) == "string" then
o.pre = o.cfg.sorter --[[@as string]]
if type(self.cfg.sorter) == "function" then
self.user = self.cfg.sorter --[[@as fun(nodes: Node[])]]
elseif type(self.cfg.sorter) == "string" then
self.pre = self.cfg.sorter --[[@as string]]
end
return o
end
--- Predefined comparator, defaulting to name