Expand your neovim sense with small UI components
2.2 kB
72 lines
1local function dec_to_rgb(num)
2 return {
3 r = bit.rshift(num, 16),
4 g = bit.band(bit.rshift(num, 8), 0xFF),
5 b = bit.band(num, 0xFF),
6 }
7end
8
9local function rgb_to_dec(rgb)
10 return bit.lshift(rgb.r, 16) + bit.lshift(rgb.g, 8) + rgb.b
11end
12
13---@param ns_id integer
14---@param opts vim.api.keyset.get_highlight
15local function read_hl(ns_id, opts)
16 opts.link = false
17 local hl = vim.api.nvim_get_hl(ns_id, opts)
18 local rich_hl = {}
19 if vim.tbl_isempty(hl) then
20 return
21 end
22 rich_hl.fg = hl.fg and dec_to_rgb(hl.fg)
23 rich_hl.bg = hl.bg and dec_to_rgb(hl.bg)
24 return rich_hl
25end
26
27---@param c table A hex color
28---@param percent number a negative number darkens and a positive one brightens
29local function tint(c, percent)
30 if not c.r or not c.g or not c.b then
31 return "NONE"
32 end
33 local blend = function(component)
34 component = math.floor(component * (1 + percent))
35 return math.min(math.max(component, 0), 255)
36 end
37 return {
38 r = blend(c.r),
39 g = blend(c.g),
40 b = blend(c.b),
41 }
42end
43
44local function set(name, from)
45 local hl = read_hl(0, { name = from, link = false })
46 if hl and hl.fg and not hl.bg then
47 hl.bg = rgb_to_dec(tint(hl.fg, -0.8))
48 hl.fg = hl.fg and rgb_to_dec(hl.fg)
49 else
50 hl = { link = from }
51 end
52 vim.api.nvim_set_hl(0, name, hl)
53end
54
55local function setup()
56 -- generate hl-groups based on existing colors
57 set("SenseVirtualTextError", "DiagnosticVirtualTextError")
58 set("SenseVirtualTextWarn", "DiagnosticVirtualTextWarn")
59 set("SenseVirtualTextInfo", "DiagnosticVirtualTextInfo")
60 set("SenseVirtualTextHint", "DiagnosticVirtualTextHint")
61 set("SenseStatusColError", "DiagnosticVirtualTextError")
62 set("SenseStatusColWarn", "DiagnosticVirtualTextWarn")
63 set("SenseStatusColInfo", "DiagnosticVirtualTextInfo")
64 set("SenseStatusColHint", "DiagnosticVirtualTextHint")
65end
66setup()
67vim.api.nvim_create_autocmd("ColorScheme", {
68 group = vim.api.nvim_create_augroup("sense.highlights", { clear = true }),
69 callback = function()
70 setup()
71 end,
72})