diff --git a/dots/.config/nvim/lua/plugins/configs/cmp.lua b/dots/.config/nvim/lua/plugins/configs/cmp.lua index 7e43baca..07f907e3 100644 --- a/dots/.config/nvim/lua/plugins/configs/cmp.lua +++ b/dots/.config/nvim/lua/plugins/configs/cmp.lua @@ -61,6 +61,39 @@ return { end end + local function hslToRgb(h, s, l) + h = h / 360 + s = s / 100 + l = l / 100 + + local r, g, b; + + if s == 0 then + r, g, b = l, l, l; -- achromatic + else + 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 + + local q = l < 0.5 and l * (1 + s) or l + s - l * s; + 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 + + local function round(num) + return num + (2 ^ 52 + 2 ^ 51) - (2 ^ 52 + 2 ^ 51) + end + return round(r * 255), round(g * 255), round(b * 255) + end + + ---@param sources table? local standard_sources = function(sources) sources = sources or {} @@ -116,7 +149,7 @@ return { mode = "symbol_text", maxwidth = 50, })(entry, vim_item) - if string.match(kind.kind, ".* Color$") then + if string.match(kind.kind, ".* Color$") or string.match(kind.kind, ".* Variable$") then ---@type string local hl = nil local cmp_item = entry.cache.entries.get_completion_item @@ -126,17 +159,37 @@ return { hl = cmp_item.label end - local s, _, red, green, blue = string.find(hl, "rgba%((%d+), (%d+), (%d+), .*%)") - if s ~= nil then - hl = string.format("#%x%x%x", red, green, blue) + local function rgbToHex(red, green, blue) + local function convertNumHex(num) + local conversion = string.format("%x", num) + if conversion:len() == 0 then + return "00" + elseif conversion:len() == 1 then + return string.format("0%s", conversion) + else + return conversion + end + end + return string.format("#%s%s%s", convertNumHex(red), convertNumHex(green), + convertNumHex(blue)) end - s, _, red, green, blue = string.find(hl, "rgb%((%d+), (%d+), (%d+))") + local s, _, red, green, blue = string.find(hl, "rgb.*%((%d+), (%d+), (%d+).*%)") if s ~= nil then - hl = string.format("#%x%x%x", red, green, blue) + hl = rgbToHex(red, green, blue) end - kind.kind_hl_group = handle_color_hl(hl) + local start, _, hue, saturation, lightness = hl:find( + "hsl.*%((%d+), (%d+)%%, (%d+)%%.*%)") + if start ~= nil then + red, green, blue = hslToRgb(tonumber(hue), tonumber(saturation), tonumber(lightness)) + hl = rgbToHex(red, green, blue) + end + + if hl:match("^#?%x%x%x%x%x%x$") ~= nil then + kind.kind_hl_group = handle_color_hl(hl) + kind.kind = " Color" + end end local strings = vim.split(kind.kind, "%s", { trimempty = true })