Neovim-SpecSwitcher/lua/SpecSwitcher/ExtensionMap.lua
2024-10-12 17:35:32 +02:00

74 lines
1.6 KiB
Lua

local ExtMap = {}
function ExtMap:new (o)
o = o or {}
setmetatable(o, self)
self.__index = self
self["list"] = {}
return o
end
function ExtMap:add_single (in_ext, out_ext)
if self[in_ext] ~= nil then
if type(out_ext) == "table" then
for _, out in ipairs(out_ext) do
table.insert(self[in_ext], out)
end
else
table.insert(self[in_ext], out_ext)
end
else
if type(out_ext) == "table" then
self[in_ext] = out_ext
else
self[in_ext] = {out_ext}
end
end
local in_exists = false
for _, exts in pairs(self["list"]) do
if exts == in_ext then
in_exists = true
end
end
if in_exists == false then
table.insert(self["list"], in_ext)
end
end
function ExtMap:list_exts()
return self["list"]
end
function ExtMap:add (in_exts, out_exts)
if type(in_exts) == "table" then
for _, in_ext in ipairs(in_exts) do
self:add_single( in_ext, out_exts)
end
else
self:add_single( in_exts, out_exts )
end
if type(out_exts) == "table" then
for _, out_ext in ipairs(out_exts) do
self:add_single( out_ext, in_exts)
end
else
self:add_single( out_exts, in_exts )
end
end
function ExtMap:print ()
base = "%s -> "
for I in pairs(self) do
local in_map = string.format(base, I)
for _, O in ipairs(self[I]) do
in_map = in_map .. O .. ","
end
print(in_map)
end
end
return ExtMap