diff --git a/lua/nightfox/config.lua b/lua/nightfox/config.lua index 43e74b75..1dd1f3f5 100644 --- a/lua/nightfox/config.lua +++ b/lua/nightfox/config.lua @@ -115,9 +115,4 @@ function M.get_compiled_info(opts) return output_path, util.join_paths(output_path, style .. file_suffix) end -function M.hash() - local hash = require("nightfox.lib.hash")(M.options) - return hash and hash or 0 -end - return M diff --git a/lua/nightfox/init.lua b/lua/nightfox/init.lua index 7194280a..ca385bc8 100644 --- a/lua/nightfox/init.lua +++ b/lua/nightfox/init.lua @@ -92,9 +92,13 @@ function M.setup(opts) local git_path = util.join_paths(debug.getinfo(1).source:sub(2, -23), ".git", "ORIG_HEAD") local git = vim.fn.getftime(git_path) - local hash = require("nightfox.lib.hash")(opts) .. (git == -1 and git_path or git) + local hash = require("nightfox.lib.hash")({ + opts = opts, + git = git, + path = git_path, + }) - if cached ~= hash then + if cached ~= tostring(hash) then M.compile() write_file(cached_path, hash) end diff --git a/lua/nightfox/lib/hash.lua b/lua/nightfox/lib/hash.lua index 52591469..61b7b30b 100644 --- a/lua/nightfox/lib/hash.lua +++ b/lua/nightfox/lib/hash.lua @@ -1,28 +1,30 @@ local bitop = bit or bit32 or require("nightfox.lib.native_bit") +-- Reference for dj2b algorithem -- https://theartincode.stanis.me/008-djb2/ -local function djb2(s) - local h = 5381 - local t = { string.byte(s, 1, #s) } - for i = 1, #t do - h = bitop.lshift(h, 5) + t[i] -- h * 33 + c - end - return h -end --- Reference: https://github.com/catppuccin/nvim/blob/151b5f6aa74f08a707a7862519bdc38bb2b9f505/lua/catppuccin/lib/hashing.lua -local function hash(x) - local t = type(x) +local function serialize(val) + local t = type(val) if t == "table" then - local h = 0 - for k, v in pairs(x) do - h = bitop.bxor(h, djb2(k .. hash(v))) + local result = "" + for k, v in pairs(val) do + result = result .. tostring(k) .. serialize(v) end - return h + return result elseif t == "function" then - return djb2(string.dump(x)) + return string.dump(val) + else + return tostring(val) + end +end + +local function checksum(val) + local str = serialize(val) + local hash = 5381 + for i = 1, #str do + hash = bit.lshift(hash, 5) + str:byte(i) end - return tostring(x) + return hash end -return hash +return checksum diff --git a/lua/nightfox/override.lua b/lua/nightfox/override.lua index ef502c90..28fd9872 100644 --- a/lua/nightfox/override.lua +++ b/lua/nightfox/override.lua @@ -14,11 +14,6 @@ local function reset() store.has_override = false end -local function hash() - local hash = require("nightfox.lib.hash")(store) - return hash and hash or 0 -end - local function check_link(tbl) for _, style in pairs(tbl) do for _, opts in pairs(style) do @@ -27,7 +22,7 @@ local function check_link(tbl) end end -return setmetatable({ reset = reset, hash = hash }, { +return setmetatable({ reset = reset }, { __index = function(_, value) if store[value] then return store[value]