Skip to content

Commit baeb71a

Browse files
committed
fix(cache): sanitize functions in config
1 parent c21f29b commit baeb71a

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

lua/cyberdream/cache.lua

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ local util = require("cyberdream.util")
44
local M = {}
55
local theme_cache_file = vim.fn.stdpath("cache") .. "/cyberdream_cache.json"
66

7+
-- sanitize a config object by removing functions
8+
local function sanitize_config(cfg)
9+
local sanitized = {}
10+
for k, v in pairs(cfg) do
11+
if type(v) == "table" then
12+
sanitized[k] = sanitize_config(v) -- recursively sanitize nested tables
13+
elseif type(v) ~= "function" then
14+
sanitized[k] = v
15+
end
16+
end
17+
return sanitized
18+
end
19+
720
--- build a cache file for a configured cyberdream theme
821
--- @param theme table
922
M.build = function(theme)
@@ -20,10 +33,13 @@ M.build = function(theme)
2033
end
2134
end
2235

23-
theme.terminal_colors = #terminal_colors > 0 and terminal_colors or nil
24-
theme.config = config.options
36+
-- Create a sanitized copy of the entire theme
37+
local sanitized_theme = vim.deepcopy(theme)
38+
sanitized_theme.config = sanitize_config(config.options)
39+
sanitized_theme.terminal_colors = #terminal_colors > 0 and terminal_colors or nil
2540

26-
cache:write(vim.json.encode(theme))
41+
-- Write the sanitized theme to cache
42+
cache:write(vim.json.encode(sanitized_theme))
2743
util.notify("Cache file written to " .. theme_cache_file)
2844
end
2945

@@ -69,8 +85,8 @@ M.load = function()
6985
vim.api.nvim_set_hl(0, group, opts)
7086
end
7187

72-
-- check if config has changed
73-
if not vim.deep_equal(theme.config, config.options) then
88+
-- check if config has changed (using sanitized comparison)
89+
if not vim.deep_equal(theme.config, sanitize_config(config.options)) then
7490
M.build(require("cyberdream.theme").setup())
7591
local notify = vim.defer_fn(function()
7692
util.notify(" Building cache...\n A restart may be required for changes to take effect.")

0 commit comments

Comments
 (0)