summaryrefslogtreecommitdiff
path: root/lua/blink/cmp/completion/windows/render/tailwind.lua
blob: 0bff6b24f40f22855603221ff27e1ea665efd41b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
local tailwind = {}

local kinds = require('blink.cmp.types').CompletionItemKind

--- @param item blink.cmp.CompletionItem
--- @return string|nil
function tailwind.get_hex_color(item)
  local doc = item.documentation
  if item.kind ~= kinds.Color or not doc then return end
  local content = type(doc) == 'string' and doc or doc.value
  if content and content:match('^#%x%x%x%x%x%x$') then return content end
end

--- @param item blink.cmp.CompletionItem
--- @return string?
function tailwind.get_kind_icon(item)
  if tailwind.get_hex_color(item) then return '██' end
end

--- @param ctx blink.cmp.DrawItemContext
--- @return string|nil
function tailwind.get_hl(ctx)
  local hex_color = tailwind.get_hex_color(ctx.item)
  if not hex_color then return end

  local hl_name = 'HexColor' .. hex_color:sub(2)
  if #vim.api.nvim_get_hl(0, { name = hl_name }) == 0 then vim.api.nvim_set_hl(0, hl_name, { fg = hex_color }) end
  return hl_name
end

return tailwind