Skip to content

Commit

Permalink
feat: use block icon for tailwind items (#544)
Browse files Browse the repository at this point in the history
* style(tailwind): use block icon for Color items, cleanup

* feat: hard code kind icon to block for tailwind

* refactor: clean up highlight.lua

---------

Co-authored-by: Liam Dyer <liamcdyer@gmail.com>
  • Loading branch information
minusfive and Saghen authored Dec 16, 2024
1 parent 0046d0c commit 1502c75
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
16 changes: 12 additions & 4 deletions lua/blink/cmp/completion/windows/render/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,34 @@ function draw_context.get_from_items(context, draw, items)
return ctxs
end

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

--- @param draw blink.cmp.Draw
--- @param item_idx number
--- @param item blink.cmp.CompletionItem
--- @param matched_indices number[]
--- @return blink.cmp.DrawItemContext
function draw_context.new(draw, item_idx, item, matched_indices)
local config = require('blink.cmp.config').appearance
local kind = require('blink.cmp.types').CompletionItemKind[item.kind] or 'Unknown'
local kind_icon = config.kind_icons[kind] or config.kind_icons.Field

local kind = kinds[item.kind] or 'Unknown'
local kind_icon = require('blink.cmp.completion.windows.render.tailwind').get_kind_icon(item)
or config.kind_icons[kind]
or config.kind_icons.Field
local icon_spacing = config.nerd_font_variant == 'mono' and '' or ' '

-- Some LSPs can return labels with newlines
-- Escape them to avoid errors in nvim_buf_set_lines when rendering the completion menu
local newline_char = '' .. icon_spacing

local label = item.label:gsub('\n', newline_char) .. (kind == 'Snippet' and '~' or '')
if config.nerd_font_variant == 'normal' then label = label:gsub('', '') end

local label_detail = (item.labelDetails and item.labelDetails.detail or ''):gsub('\n', newline_char)
if config.nerd_font_variant == 'normal' then label_detail = label_detail:gsub('', '') end

local label_description = (item.labelDetails and item.labelDetails.description or ''):gsub('\n', newline_char)
if config.nerd_font_variant == 'normal' then label_description = label_description:gsub('', '') end

local source_id = item.source_id
local source_name = item.source_name

Expand Down
30 changes: 21 additions & 9 deletions lua/blink/cmp/completion/windows/render/tailwind.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
local tailwind = {}

--- @param item blink.cmp.CompletionItem
--- @return string|nil
function tailwind.get_hex_color(item)
local doc = item.documentation
if item.kind ~= '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 doc = ctx.item.documentation
if ctx.kind == 'Color' and doc then
local content = type(doc) == 'string' and doc or doc.value
if content and content:match('^#%x%x%x%x%x%x$') then
local hl_name = 'HexColor' .. content:sub(2)
if #vim.api.nvim_get_hl(0, { name = hl_name }) == 0 then vim.api.nvim_set_hl(0, hl_name, { fg = content }) end
return hl_name
end
end
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
8 changes: 3 additions & 5 deletions lua/blink/cmp/config/completion/menu.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local validate = require('blink.cmp.config.utils').validate

--- @class (exact) blink.cmp.CompletionMenuConfig
--- @field enabled boolean
--- @field min_width number
Expand All @@ -17,7 +19,6 @@
--- @field n 'top_down' | 'bottom_up'
--- @field s 'top_down' | 'bottom_up'

local validate = require('blink.cmp.config.utils').validate
local window = {
--- @type blink.cmp.CompletionMenuConfig
default = {
Expand Down Expand Up @@ -87,10 +88,7 @@ local window = {

label = {
width = { fill = true, max = 60 },
text = function(ctx)
local label = (ctx.label .. ctx.label_detail):gsub('', '' .. ctx.icon_gap)
return label
end,
text = function(ctx) return ctx.label .. ctx.label_detail end,
highlight = function(ctx)
-- label and label details
local label = ctx.label
Expand Down
5 changes: 4 additions & 1 deletion lua/blink/cmp/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ local highlights = {}
function highlights.setup()
local use_nvim_cmp = require('blink.cmp.config').appearance.use_nvim_cmp_as_default

--- @param hl_group string Highlight group name, e.g. 'ErrorMsg'
--- @param opts vim.api.keyset.highlight Highlight definition map
--- @return nil
local set_hl = function(hl_group, opts)
opts.default = true
opts.default = true -- Prevents overriding existing definitions
vim.api.nvim_set_hl(0, hl_group, opts)
end

Expand Down

0 comments on commit 1502c75

Please sign in to comment.