Initial Commit

This commit is contained in:
Henrik Hautakoski 2026-05-17 16:26:31 +02:00
commit 694515c168
16 changed files with 1325 additions and 0 deletions

107
lua/skift/utils/cache.lua Normal file
View file

@ -0,0 +1,107 @@
local M = {}
local CACHE_VERSION = 1
local CACHE_DIR = vim.fn.stdpath("cache") .. "/skift"
local SOURCE_FILES = {
"lua/skift/palette.lua",
"lua/skift/init.lua",
"lua/skift/groups/editor.lua",
"lua/skift/groups/syntax.lua",
"lua/skift/groups/treesitter.lua",
"lua/skift/groups/lsp.lua",
"lua/skift/groups/integrations.lua",
"lua/skift/utils/palette.lua",
}
local function function_fingerprint(fn)
if type(fn) ~= "function" then
return tostring(fn)
end
local ok, dumped = pcall(string.dump, fn)
if ok and dumped then
return vim.fn.sha256(dumped)
end
local info = debug.getinfo(fn, "S")
if info and info.source then
return string.format("%s:%d", info.source, info.linedefined or 0)
end
return tostring(fn)
end
local function source_fingerprint()
local stamp = {}
for _, pattern in ipairs(SOURCE_FILES) do
local path = vim.api.nvim_get_runtime_file(pattern, false)[1]
if path then
local stat = vim.loop.fs_stat(path)
stamp[#stamp + 1] = {
file = pattern,
mtime = stat and stat.mtime and stat.mtime.sec or 0,
size = stat and stat.size or 0,
}
else
stamp[#stamp + 1] = { file = pattern, mtime = 0, size = 0 }
end
end
return stamp
end
local function cache_file(cache_key)
return CACHE_DIR .. "/highlights-" .. cache_key .. ".json"
end
---@param config Config
function M.key(config)
local payload = {
version = CACHE_VERSION,
transparent = config.transparent,
bold = config.bold,
italic = config.italic,
italic_comments = config.italic_comments,
on_colors = function_fingerprint(config.on_colors),
on_highlights = function_fingerprint(config.on_highlights),
sources = source_fingerprint(),
}
return vim.fn.sha256(vim.json.encode(payload))
end
function M.read(cache_key)
local path = cache_file(cache_key)
if vim.fn.filereadable(path) == 0 then
return nil
end
local ok_read, lines = pcall(vim.fn.readfile, path)
if not ok_read or not lines then
return nil
end
local raw = table.concat(lines, "\n")
local ok_decode, decoded = pcall(vim.json.decode, raw)
if not ok_decode then
return nil
end
return decoded
end
function M.write(cache_key, payload)
vim.fn.mkdir(CACHE_DIR, "p")
local path = cache_file(cache_key)
local encoded = vim.json.encode(payload)
local ok_write = pcall(vim.fn.writefile, { encoded }, path)
return ok_write
end
function M.clear()
vim.fn.delete(CACHE_DIR, "rf")
end
return M

71
lua/skift/utils/hsl.lua Normal file
View file

@ -0,0 +1,71 @@
-- https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua
local M = {}
--- Converts an HSL color value to RGB. Conversion formula
--- adapted from http://en.wikipedia.org/wiki/HSL_color_space.
--- Assumes h, s, and l are contained in the set [0, 1] and
--- returns r, g, and b in the set [0, 255].
---
--- @param h number The hue
--- @param s number The saturation
--- @param l number The lightness
--- @return number, number, number # The RGB representation
function M.hslToRgb(h, s, l)
--- @type number, number, number
local r, g, b
if s == 0 then
r, g, b = l, l, l -- achromatic
else
--- @param p number
--- @param q number
--- @param t number
local function hue2rgb(p, q, t)
if t < 0 then
t = t + 1
end
if t > 1 then
t = t - 1
end
if t < 1 / 6 then
return p + (q - p) * 6 * t
end
if t < 1 / 2 then
return q
end
if t < 2 / 3 then
return p + (q - p) * (2 / 3 - t) * 6
end
return p
end
--- @type number
local q
if l < 0.5 then
q = l * (1 + s)
else
q = l + s - l * s
end
local p = 2 * l - q
r = hue2rgb(p, q, h + 1 / 3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h - 1 / 3)
end
return r * 255, g * 255, b * 255
end
--- Converts an HSL color value to RGB in Hex representation.
--- @param h number The hue
--- @param s number The saturation
--- @param l number The lightness
--- @return string # The hex representation
function M.hslToHex(h, s, l)
local r, g, b = M.hslToRgb(h / 360, s / 100, l / 100)
return string.format("#%02x%02x%02x", r, g, b)
end
return M

View file

@ -0,0 +1,53 @@
local cache = setmetatable({}, { __mode = 'k' })
---@param definitions table<string, string>
---@return table<string, string>
return function(definitions)
local cached = cache[definitions]
if cached ~= nil then
return cached
end
local resolved = {}
local resolving = {}
local function resolve(key)
if resolved[key] ~= nil then
return resolved[key]
end
local value = definitions[key]
if value == nil then
return nil
end
if type(value) ~= 'string' then
resolved[key] = value
return value
end
if resolving[key] then
error('sora.palette: cyclic color alias detected for key "' .. key .. '"')
end
if definitions[value] == nil then
resolved[key] = value
return value
end
resolving[key] = true
local alias_value = resolve(value)
resolving[key] = nil
resolved[key] = alias_value
return alias_value
end
for key in pairs(definitions) do
resolve(key)
end
cache[definitions] = resolved
return resolved
end