From 4199252d6b7d11964b5435c90ffbb28b6552c686 Mon Sep 17 00:00:00 2001 From: Jiahe Jiang <61075605+Isrothy@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:23:30 -0500 Subject: [PATCH] perf: Cache highlights to improve performance --- lua/nord/init.lua | 40 +++++++++++++------------- lua/nord/utils/init.lua | 62 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/lua/nord/init.lua b/lua/nord/init.lua index 63fe159..08dfa42 100644 --- a/lua/nord/init.lua +++ b/lua/nord/init.lua @@ -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 diff --git a/lua/nord/utils/init.lua b/lua/nord/utils/init.lua index e7b375c..367140f 100644 --- a/lua/nord/utils/init.lua +++ b/lua/nord/utils/init.lua @@ -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) @@ -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