mirror of
https://github.com/gabehf/base46.git
synced 2026-03-14 01:37:03 -07:00
cache integration modules instead of minification in compile()
This commit is contained in:
parent
50d000aeca
commit
c8f82f7690
1 changed files with 41 additions and 34 deletions
|
|
@ -22,19 +22,6 @@ M.merge_tb = function(table1, table2)
|
||||||
return vim.tbl_deep_extend("force", table1, table2)
|
return vim.tbl_deep_extend("force", table1, table2)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.load_all_highlights = function()
|
|
||||||
-- reload highlights for theme switcher
|
|
||||||
require("plenary.reload").reload_module "base46"
|
|
||||||
|
|
||||||
M.compile()
|
|
||||||
|
|
||||||
local hl_files = vim.fn.stdpath "data" .. "/site/pack/base46_cache/start/compiled_themes/lua/base46_cache/"
|
|
||||||
|
|
||||||
for _, file in ipairs(vim.fn.readdir(hl_files)) do
|
|
||||||
require("base46_cache." .. vim.fn.fnamemodify(file, ":r"))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
M.turn_str_to_color = function(tb_in)
|
M.turn_str_to_color = function(tb_in)
|
||||||
local tb = vim.deepcopy(tb_in)
|
local tb = vim.deepcopy(tb_in)
|
||||||
local colors = M.get_theme_tb "base_30"
|
local colors = M.get_theme_tb "base_30"
|
||||||
|
|
@ -93,45 +80,65 @@ M.load_highlight = function(group)
|
||||||
return group
|
return group
|
||||||
end
|
end
|
||||||
|
|
||||||
-- save table
|
-- convert table into string
|
||||||
M.table_to_file = function(filename, tb)
|
M.table_to_str = function(filename, tb)
|
||||||
local theme_type = M.get_theme_tb "type" -- dark / light
|
local theme_type = M.get_theme_tb "type" -- dark / light
|
||||||
local cache_path = vim.fn.stdpath "data" .. "/site/pack/base46_cache/start/compiled_themes/lua/base46_cache/"
|
local result = filename == "defaults" and "vim.opt.bg='" .. theme_type .. "'" or ""
|
||||||
local file = io.open(cache_path .. filename, "w")
|
|
||||||
|
|
||||||
if file then
|
for hlgroupName, hlgroup_vals in pairs(tb) do
|
||||||
-- set vim.opt.bg code in defaults.lua only
|
local hlname = "'" .. hlgroupName .. "',"
|
||||||
local result = filename == "defaults.lua" and "vim.opt.bg='" .. theme_type .. "'" or ""
|
local opts = ""
|
||||||
|
|
||||||
for hlgroupName, hlgroup_vals in pairs(tb) do
|
for optName, optVal in pairs(hlgroup_vals) do
|
||||||
local hlname = "'" .. hlgroupName .. "',"
|
local valueInStr = type(optVal) == "boolean" and " " .. tostring(optVal) or '"' .. optVal .. '"'
|
||||||
local opts = ""
|
opts = opts .. optName .. "=" .. valueInStr .. ","
|
||||||
|
|
||||||
for optName, optVal in pairs(hlgroup_vals) do
|
|
||||||
local valueInStr = type(optVal) == "boolean" and " " .. tostring(optVal) or '"' .. optVal .. '"'
|
|
||||||
opts = opts .. optName .. "=" .. valueInStr .. ","
|
|
||||||
end
|
|
||||||
|
|
||||||
result = result .. "vim.api.nvim_set_hl(0," .. hlname .. "{" .. opts .. "})"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
file:write(result)
|
result = result .. "vim.api.nvim_set_hl(0," .. hlname .. "{" .. opts .. "})"
|
||||||
|
end
|
||||||
|
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
M.saveStr_to_cache = function(filename, tb)
|
||||||
|
-- Thanks to https://github.com/EdenEast/nightfox.nvim
|
||||||
|
-- It helped me understand string.dump stuff
|
||||||
|
|
||||||
|
local cache_path = vim.fn.stdpath "cache" .. "/nvchad/base46/"
|
||||||
|
local lines = 'require("base46").compiled = string.dump(function()' .. M.table_to_str(filename, tb) .. "end)"
|
||||||
|
local file = io.open(cache_path .. filename, "wb")
|
||||||
|
|
||||||
|
loadstring(lines, "=")()
|
||||||
|
|
||||||
|
if file then
|
||||||
|
file:write(require("base46").compiled)
|
||||||
file:close()
|
file:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.compile = function()
|
M.compile = function()
|
||||||
|
-- All integration modules, each file returns a table
|
||||||
local hl_files = vim.fn.stdpath "data" .. "/site/pack/packer/start/base46/lua/base46/integrations"
|
local hl_files = vim.fn.stdpath "data" .. "/site/pack/packer/start/base46/lua/base46/integrations"
|
||||||
|
|
||||||
for _, file in ipairs(vim.fn.readdir(hl_files)) do
|
for _, file in ipairs(vim.fn.readdir(hl_files)) do
|
||||||
local integration = M.load_highlight(vim.fn.fnamemodify(file, ":r"))
|
local filename = vim.fn.fnamemodify(file, ":r")
|
||||||
|
local integration = M.load_highlight(filename)
|
||||||
|
|
||||||
-- merge new hl groups added by users
|
-- merge new hl groups added by users
|
||||||
if vim.fn.fnamemodify(file, ":r") == "defaults" then
|
if filename == "defaults" then
|
||||||
integration = M.merge_tb(integration, (M.turn_str_to_color(config.ui.hl_add)))
|
integration = M.merge_tb(integration, (M.turn_str_to_color(config.ui.hl_add)))
|
||||||
end
|
end
|
||||||
|
|
||||||
M.table_to_file(file, integration)
|
M.saveStr_to_cache(filename, integration)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
M.load_all_highlights = function()
|
||||||
|
require("plenary.reload").reload_module "base46"
|
||||||
|
M.compile()
|
||||||
|
|
||||||
|
for _, file in ipairs(vim.fn.readdir(vim.g.base46_cache)) do
|
||||||
|
loadfile(vim.g.base46_cache .. file)()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue