Skip to content

Commit b2bbef5

Browse files
authored
feat: TailwindCSS highlight support (#143)
* Initial tailwind support * Implement to all autocomplete drawers * Fix formatting
1 parent aeb6195 commit b2bbef5

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lua/blink/cmp/utils.lua

+16
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,20 @@ function utils.get_regex_around_cursor(range, regex, exclude_from_prefix_regex)
8383
return { start_col = start_col, length = length }
8484
end
8585

86+
--- @param ctx blink.cmp.CompletionRenderContext
87+
--- @return string|nil
88+
function utils.try_get_tailwind_hl(ctx)
89+
local doc = ctx.item.documentation
90+
if ctx.kind == 'Color' and doc then
91+
local content = type(doc) == 'string' and doc or doc.value
92+
if ctx.kind == 'Color' and content and content:match('^#%x%x%x%x%x%x$') then
93+
local hl_name = 'HexColor' .. content:sub(2)
94+
if #vim.api.nvim_get_hl(0, { name = hl_name }) == 0 then
95+
vim.api.nvim_set_hl(0, hl_name, { fg = content })
96+
end
97+
return hl_name
98+
end
99+
end
100+
end
101+
86102
return utils

lua/blink/cmp/windows/autocomplete.lua

+16-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
--- @field draw_item_minimal blink.cmp.CompletionDrawFn
5252

5353
local config = require('blink.cmp.config')
54+
local utils = require('blink.cmp.utils')
5455
local renderer = require('blink.cmp.windows.lib.render')
5556
local text_edits_lib = require('blink.cmp.accept.text-edits')
5657
local autocmp_config = config.windows.autocomplete
@@ -358,7 +359,11 @@ end
358359
function autocomplete.draw_item_simple(ctx)
359360
return {
360361
' ',
361-
{ ctx.kind_icon, ctx.icon_gap, hl_group = 'BlinkCmpKind' .. ctx.kind },
362+
{
363+
ctx.kind_icon,
364+
ctx.icon_gap,
365+
hl_group = utils.try_get_tailwind_hl(ctx) or ('BlinkCmpKind' .. ctx.kind),
366+
},
362367
{
363368
ctx.label,
364369
ctx.kind == 'Snippet' and '~' or '',
@@ -384,7 +389,12 @@ function autocomplete.draw_item_reversed(ctx)
384389
max_width = 50,
385390
},
386391
' ',
387-
{ ctx.kind_icon, ctx.icon_gap, ctx.kind, hl_group = 'BlinkCmpKind' .. ctx.kind },
392+
{
393+
ctx.kind_icon,
394+
ctx.icon_gap,
395+
ctx.kind,
396+
hl_group = utils.try_get_tailwind_hl(ctx) or ('BlinkCmpKind' .. ctx.kind),
397+
},
388398
' ',
389399
}
390400
end
@@ -402,7 +412,10 @@ function autocomplete.draw_item_minimal(ctx)
402412
max_width = 50,
403413
},
404414
' ',
405-
{ ctx.kind, hl_group = 'BlinkCmpKind' .. ctx.kind },
415+
{
416+
ctx.kind,
417+
hl_group = utils.try_get_tailwind_hl(ctx) or ('BlinkCmpKind' .. ctx.kind),
418+
},
406419
' ',
407420
}
408421
end

0 commit comments

Comments
 (0)