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

View File

@@ -9,18 +9,18 @@
-- https://github.com/rxi/classic -- https://github.com/rxi/classic
-- --
---@class (exact) Object ---@class (exact) Class
---@field super Object ---@field super Class
---@field private implements table<Object, boolean> ---@field private implements table<Class, boolean>
local Object = {} local Class = {}
Object.__index = Object ---@diagnostic disable-line: inject-field Class.__index = Class ---@diagnostic disable-line: inject-field
---Default constructor ---Default constructor
function Object:new(...) --luacheck: ignore 212 function Class:new(...) --luacheck: ignore 212
end end
---Extend a class, setting .super ---Extend a class, setting .super
function Object:extend() function Class:extend()
local cls = {} local cls = {}
for k, v in pairs(self) do for k, v in pairs(self) do
if k:find("__") == 1 then if k:find("__") == 1 then
@@ -35,14 +35,14 @@ end
---Implement the functions of a mixin ---Implement the functions of a mixin
---Add the mixin to .implements ---Add the mixin to .implements
---@param class Object ---@param mixin Class
function Object:implement(class) function Class:implement(mixin)
if not rawget(self, "implements") then if not rawget(self, "implements") then
-- set on the class itself instead of parents -- set on the class itself instead of parents
rawset(self, "implements", {}) rawset(self, "implements", {})
end end
self.implements[class] = true self.implements[mixin] = true
for k, v in pairs(class) do for k, v in pairs(mixin) do
if self[k] == nil and type(v) == "function" then if self[k] == nil and type(v) == "function" then
self[k] = v self[k] = v
end end
@@ -53,7 +53,7 @@ end
---@generic T ---@generic T
---@param class T ---@param class T
---@return boolean ---@return boolean
function Object:is(class) function Class:is(class)
local mt = getmetatable(self) local mt = getmetatable(self)
while mt do while mt do
if mt == class then if mt == class then
@@ -71,12 +71,12 @@ end
---@generic T ---@generic T
---@param class T ---@param class T
---@return T|nil ---@return T|nil
function Object:as(class) function Class:as(class)
return self:is(class) and self or nil return self:is(class) and self or nil
end end
---Constructor to create instance, call :new and return ---Constructor to create instance, call :new and return
function Object:__call(...) function Class:__call(...)
local obj = setmetatable({}, self) local obj = setmetatable({}, self)
obj:new(...) obj:new(...)
return obj return obj
@@ -84,7 +84,7 @@ end
-- avoid unused param warnings in abstract methods -- avoid unused param warnings in abstract methods
---@param ... any ---@param ... any
function Object:nop(...) --luacheck: ignore 212 function Class:nop(...) --luacheck: ignore 212
end 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 Filters = require("nvim-tree.explorer.filters")
local Marks = require("nvim-tree.marks") local Marks = require("nvim-tree.marks")
local LiveFilter = require("nvim-tree.explorer.live-filter") 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 Clipboard = require("nvim-tree.actions.fs.clipboard")
local Renderer = require("nvim-tree.renderer") local Renderer = require("nvim-tree.renderer")
@@ -57,7 +57,7 @@ function Explorer:new(args)
self.open = true self.open = true
self.opts = config self.opts = config
self.sorters = Sorters:create(config) self.sorters = Sorter(config)
self.renderer = Renderer:new(config, self) self.renderer = Renderer:new(config, self)
self.filters = Filters:new(config, self) self.filters = Filters:new(config, self)
self.live_filter = LiveFilter: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 DirectoryNode = require("nvim-tree.node.directory")
local C = {} local C = {}
@@ -12,23 +12,20 @@ local C = {}
---@field cfg SorterCfg ---@field cfg SorterCfg
---@field user fun(nodes: Node[])? ---@field user fun(nodes: Node[])?
---@field pre string? ---@field pre string?
local Sorter = Class:new() local Sorter = Class:extend()
---@class Sorter
---@overload fun(opts: table): Sorter
---@param opts table user options ---@param opts table user options
---@return Sorter function Sorter:new(opts)
function Sorter:create(opts) self.cfg = vim.deepcopy(opts.sort)
---@type Sorter
local o = {
cfg = vim.deepcopy(opts.sort),
}
o = self:new(o)
if type(o.cfg.sorter) == "function" then if type(self.cfg.sorter) == "function" then
o.user = o.cfg.sorter --[[@as fun(nodes: Node[])]] self.user = self.cfg.sorter --[[@as fun(nodes: Node[])]]
elseif type(o.cfg.sorter) == "string" then elseif type(self.cfg.sorter) == "string" then
o.pre = o.cfg.sorter --[[@as string]] self.pre = self.cfg.sorter --[[@as string]]
end end
return o
end end
--- Predefined comparator, defaulting to name --- Predefined comparator, defaulting to name