Skip to content

Commit

Permalink
perf: Cache highlights to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Isrothy committed Dec 3, 2024
1 parent 4cc1993 commit 4199252
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
40 changes: 20 additions & 20 deletions lua/nord/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ function nord.load(opts)
require("nord.terminal").apply()
end

utils.load(
require("nord.defaults").highlights(),
require("nord.lsp").highlights(),
require("nord.syntax").highlights(),
require("nord.terminal").highlights(),
require("nord.treesitter").highlights(),
require("nord.plugins.bufferline").highlights(),
require("nord.plugins.completion").highlights(),
require("nord.plugins.filetree").highlights(),
require("nord.plugins.git").highlights(),
require("nord.plugins.motion").highlights(),
require("nord.plugins.notify").highlights(),
require("nord.plugins.picker").highlights(),
require("nord.plugins.ui").highlights(),
require("nord.plugins.diffview").highlights(),
require("nord.plugins.neogit").highlights(),
require("nord.plugins.glance").highlights(),
require("nord.plugins.mini").highlights(),
require("nord.plugins.markview").highlights()
)
utils.load(function()
return require("nord.defaults").highlights(),
require("nord.lsp").highlights(),
require("nord.syntax").highlights(),
require("nord.terminal").highlights(),
require("nord.treesitter").highlights(),
require("nord.plugins.bufferline").highlights(),
require("nord.plugins.completion").highlights(),
require("nord.plugins.filetree").highlights(),
require("nord.plugins.git").highlights(),
require("nord.plugins.motion").highlights(),
require("nord.plugins.notify").highlights(),
require("nord.plugins.picker").highlights(),
require("nord.plugins.ui").highlights(),
require("nord.plugins.diffview").highlights(),
require("nord.plugins.neogit").highlights(),
require("nord.plugins.glance").highlights(),
require("nord.plugins.mini").highlights(),
require("nord.plugins.markview").highlights()
end)

vim.g.colors_name = "nord"
end
Expand Down
62 changes: 60 additions & 2 deletions lua/nord/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
local utils = {}
local c = require("nord.colors").palette

function utils.load(...)
local highlights = vim.tbl_extend("force", ...)
local function get_cachable_config()
local options = require("nord.config").options
local cached_options = vim.deepcopy(options)
cached_options.on_highlights = nil
cached_options.on_colors = nil
return {
opts = cached_options,
colors = c,
}
end

function utils.load(func)
local options = require("nord.config").options
local cacheable_config = get_cachable_config()
local ok, tbl = pcall(utils.load_from_file, "config")
local highlights = nil
if ok and tbl and vim.deep_equal(tbl.config, cacheable_config) then
highlights = tbl.highlights
else
highlights = vim.tbl_extend("force", func())
local ok, err = pcall(utils.save_to_file, "config", { config = cacheable_config, highlights = highlights })
if not ok then
vim.notify("Failed to save cache: " .. err, vim.log.levels.WARN)
end
end
options.on_highlights(highlights, c)
for group, hl in pairs(highlights) do
vim.api.nvim_set_hl(0, group, hl)
Expand Down Expand Up @@ -70,4 +92,40 @@ function utils.make_global_bg(transparent)
return c.polar_night.origin
end

local function cache_filename(filename)
return vim.fn.stdpath("cache") .. "/nord-" .. filename .. ".json"
end

function utils.save_to_file(filename, object)
filename = cache_filename(filename)
local file, err = io.open(filename, "w")
if not file then
error("Failed to open file for writing: " .. err)
end
local success, encoded = pcall(vim.json.encode, object)
if not success then
error("Failed to encode cache: " .. encoded)
end
file:write(encoded)
file:close()
end

function utils.load_from_file(filename)
filename = cache_filename(filename)
local file, err = io.open(filename, "r")
if not file then
error("Failed to open file for reading: " .. err)
end
local content = file:read("*a")
file:close()
local success, decoded = pcall(vim.json.decode, content, {
object = true,
array = true,
})
if not success then
error("Failed to decode file content: " .. decoded)
end
return decoded
end

return utils

0 comments on commit 4199252

Please sign in to comment.