From 733ee9925dcc9374964e631e0196ed6facd82fb3 Mon Sep 17 00:00:00 2001 From: Hawtian Wang Date: Thu, 28 Mar 2024 23:27:07 +0800 Subject: [PATCH] ... --- lua/dotvim/libs/.vim-template:*.lua | 4 ++ lua/dotvim/libs/class.lua | 73 +++++++++++++++++++++++++++++ lua/dotvim/libs/init.lua | 0 3 files changed, 77 insertions(+) create mode 100644 lua/dotvim/libs/.vim-template:*.lua create mode 100644 lua/dotvim/libs/class.lua create mode 100644 lua/dotvim/libs/init.lua diff --git a/lua/dotvim/libs/.vim-template:*.lua b/lua/dotvim/libs/.vim-template:*.lua new file mode 100644 index 00000000..ed2e96fe --- /dev/null +++ b/lua/dotvim/libs/.vim-template:*.lua @@ -0,0 +1,4 @@ +---@class dotvim.libs.%FILE% +local M = {} + +return M diff --git a/lua/dotvim/libs/class.lua b/lua/dotvim/libs/class.lua new file mode 100644 index 00000000..b0d1dfc3 --- /dev/null +++ b/lua/dotvim/libs/class.lua @@ -0,0 +1,73 @@ +---@class dotvim.libs.class +local M = {} + +---@class dotvim.libs.class.Metatable +---@field __class__ dotvim.libs.class.Class +---@field __index table|fun():any +---@field __newindex fun():any +---@field __tostring fun():any + +---@class dotvim.libs.class.Class +---@field meta dotvim.libs.class.Metatable +---@field __super__ dotvim.libs.class.Class + +---@param meta? table +---@return dotvim.libs.class.Class +function M.new_class(meta) + local c = { meta = meta or {} } + c.meta.__index = c.meta + c.__class__ = c + return c +end + +---@param target dotvim.libs.class.Class +---@param base dotvim.libs.class.Class +function M.extend_class(target, base) + target.__super__ = base + local mt = setmetatable({ __index = base }, base) + local base_metatable = getmetatable(base) + if base_metatable then + if type(base_metatable.__index) == "function" then + mt.__index = base_metatable.__index + end + if type(base_metatable.__newindex) == "function" then + mt.__newindex = base_metatable.__newindex + end + end + setmetatable(target, mt) + setmetatable(target.meta, base.meta) + + if type(base.meta.__tostring) == "function" then + target.meta.__tostring = base.meta.__tostring + end + if type(base.meta.__index) == "function" then + target.meta.__index = base.meta.__index + end + if type(base.meta.__newindex) == "function" then + target.meta.__newindex = base.meta.__newindex + end +end + +function M.new_instance(class, ...) + local instance = setmetatable({}, class.meta) + if instance.__init__ then + instance:__init__(...) + end + return instance +end + +function M.instanceof(obj, class) + if type(obj) ~= "table" or type(class) ~= "table" then + return false + end + local cls = obj.__class__ + while cls ~= nil do + if cls == class then + return true + end + cls = cls.__super__ + end + return false +end + +return M diff --git a/lua/dotvim/libs/init.lua b/lua/dotvim/libs/init.lua new file mode 100644 index 00000000..e69de29b