From 67dcaf0c586bd682de9bb2b40b67dab75bc33248 Mon Sep 17 00:00:00 2001 From: Suryansh-Dey Date: Wed, 26 Mar 2025 22:31:42 +0530 Subject: [PATCH] Supporting different ghost_text highlight when selected --- doc/cmp.txt | 2 +- lua/cmp/types/cmp.lua | 1 + lua/cmp/view/ghost_text_view.lua | 8 ++++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/doc/cmp.txt b/doc/cmp.txt index da2a7038f..041a629c6 100644 --- a/doc/cmp.txt +++ b/doc/cmp.txt @@ -787,7 +787,7 @@ window.documentation.max_height~ *cmp-config.experimental.ghost_text* experimental.ghost_text~ - `boolean | { hl_group = string }` + `boolean | { hl_group = string, hl_group_selected = string | nil}` Whether to enable the ghost_text feature. ============================================================================== diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 982261df1..3e692c57b 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -164,6 +164,7 @@ cmp.ItemField = { ---@class cmp.GhostTextConfig ---@field hl_group string +---@field hl_group_selected string|nil ---@class cmp.SourceConfig ---@field public name string diff --git a/lua/cmp/view/ghost_text_view.lua b/lua/cmp/view/ghost_text_view.lua index a6457f16e..3b1eadb70 100644 --- a/lua/cmp/view/ghost_text_view.lua +++ b/lua/cmp/view/ghost_text_view.lua @@ -62,9 +62,13 @@ ghost_text_view.new = function() local text = self.text_gen(self, line, col) if #text > 0 then + local text_hl = 'Comment' + if type(c) == 'table' then + text_hl = require('cmp').core.view:get_selected_entry() and (c.hl_group_selected or c.hl_group) or c.hl_group + end local virt_lines = {} for _, l in ipairs(vim.fn.split(text, '\n')) do - table.insert(virt_lines, { { l, type(c) == 'table' and c.hl_group or 'Comment' } }) + table.insert(virt_lines, { { l, text_hl} }) end local first_line = table.remove(virt_lines, 1) self.extmark_buf = vim.api.nvim_get_current_buf() @@ -87,7 +91,7 @@ end --- of character differences instead of just byte difference. ghost_text_view.text_gen = function(self, line, cursor_col) local word = self.entry:get_insert_text() - if self.entry:get_completion_item().insertTextFormat == types.lsp.InsertTextFormat.Snippet then + if self.entry.completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet then word = tostring(snippet.parse(word)) end local word_clen = vim.str_utfindex(word)