Skip to content

Commit

Permalink
feat: nvim cmp as default highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Sep 18, 2024
1 parent 2190ee5 commit b93a5e3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
11 changes: 9 additions & 2 deletions lua/blink/cmp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
--- @field autocomplete blink.cmp.AutocompleteConfig
--- @field documentation blink.cmp.DocumentationConfig

--- @class blink.cmp.HighlightConfig
--- @field ns number
--- @field use_nvim_cmp_as_default boolean

--- @class blink.cmp.AutocompleteConfig
--- @field min_width number
--- @field max_width number
Expand All @@ -78,7 +82,7 @@
--- @field fuzzy blink.cmp.FuzzyConfig
--- @field sources blink.cmp.SourceConfig
--- @field windows blink.cmp.WindowConfig
--- @field highlight_ns number
--- @field highlight blink.cmp.HighlightConfig
--- @field kind_icons table<string, string>

--- @type blink.cmp.CmpConfig
Expand Down Expand Up @@ -138,7 +142,10 @@ local config = {
},
},

highlight_ns = vim.api.nvim_create_namespace('blink_cmp'),
highlight = {
ns = vim.api.nvim_create_namespace('blink_cmp'),
use_nvim_cmp_as_default = false,
},
kind_icons = {
Text = '󰉿',
Method = '󰊕',
Expand Down
17 changes: 12 additions & 5 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ cmp.setup = function(opts)
end

cmp.add_default_highlights = function()
vim.api.nvim_set_hl(0, 'BlinkCmpLabel', { link = 'Pmenu', default = true })
vim.api.nvim_set_hl(0, 'BlinkCmpLabelDeprecated', { link = 'Comment', default = true })
vim.api.nvim_set_hl(0, 'BlinkCmpLabelMatch', { link = 'Pmenu', default = true })
vim.api.nvim_set_hl(0, 'BlinkCmpKind', { link = 'Special', default = true })
local use_nvim_cmp = require('blink.cmp.config').highlight.use_nvim_cmp_as_default

local set_hl = function(hl_group, opts)
opts.default = true
vim.api.nvim_set_hl(0, hl_group, opts)
end

set_hl('BlinkCmpLabel', { link = use_nvim_cmp and 'CmpItemAbbr' or 'Pmenu' })
set_hl('BlinkCmpLabelDeprecated', { link = use_nvim_cmp and 'CmpItemAbbrDeprecated' or 'Comment' })
set_hl('BlinkCmpLabelMatch', { link = use_nvim_cmp and 'CmpItemAbbrMatch' or 'Pmenu' })
set_hl('BlinkCmpKind', { link = use_nvim_cmp and 'CmpItemKind' or 'Special' })
for _, kind in pairs(vim.lsp.protocol.CompletionItemKind) do
vim.api.nvim_set_hl(0, 'BlinkCmpKind' .. kind, { link = 'BlinkCmpItemKind', default = true })
set_hl('BlinkCmpKind' .. kind, { link = use_nvim_cmp and 'CmpItemKind' .. kind or 'BlinkCmpItemKind' })
end
end

Expand Down
6 changes: 3 additions & 3 deletions lua/blink/cmp/windows/autocomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function autocomplete.setup()
-- Setting highlights is slow and we update on every keystroke so we instead use a decoration provider
-- which will only render highlights of the visible lines. This also avoids having to do virtual scroll
-- like nvim-cmp does, which breaks on UIs like neovide
vim.api.nvim_set_decoration_provider(config.highlight_ns, {
vim.api.nvim_set_decoration_provider(config.highlight.ns, {
on_win = function(_, winnr, bufnr)
return autocomplete.win:get_win() == winnr and bufnr == autocomplete.win:get_buf()
end,
Expand All @@ -33,7 +33,7 @@ function autocomplete.setup()
local kind_hl = 'BlinkCmpKind' .. kind

-- todo: handle .labelDetails and others
vim.api.nvim_buf_set_extmark(bufnr, config.highlight_ns, line_number, 0, {
vim.api.nvim_buf_set_extmark(bufnr, config.highlight.ns, line_number, 0, {
end_col = 4,
hl_group = kind_hl,
hl_mode = 'combine',
Expand All @@ -44,7 +44,7 @@ function autocomplete.setup()
-- todo: use vim.lsp.protocol.CompletionItemTag
if item.deprecated or (item.tags and vim.tbl_contains(item.tags, 1)) then
-- todo: why 7?
vim.api.nvim_buf_set_extmark(bufnr, config.highlight_ns, line_number, 7, {
vim.api.nvim_buf_set_extmark(bufnr, config.highlight.ns, line_number, 7, {
end_col = #line_text - 1,
hl_group = 'BlinkCmpLabelDeprecated',
hl_mode = 'combine',
Expand Down

0 comments on commit b93a5e3

Please sign in to comment.