From 3fce8d97007a08df9f3f816300247b148827cf43 Mon Sep 17 00:00:00 2001 From: hrsh7th <629908+hrsh7th@users.noreply.github.com> Date: Tue, 1 Apr 2025 16:47:02 +0900 Subject: [PATCH] improve backward compatibility --- lua/cmp/utils/misc.lua | 5 ++++- lua/cmp/view/ghost_text_view.lua | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lua/cmp/utils/misc.lua b/lua/cmp/utils/misc.lua index 77f78a9..76f1339 100644 --- a/lua/cmp/utils/misc.lua +++ b/lua/cmp/utils/misc.lua @@ -197,7 +197,10 @@ end ---@return integer misc.to_utfindex = function(text, vimindex) vimindex = vimindex or #text + 1 - return vim.str_utfindex(text, 'utf-16', math.max(0, math.min(vimindex - 1, #text))) + if vim.fn.has('nvim-0.11') then + return vim.str_utfindex(text, 'utf-16', math.max(0, math.min(vimindex - 1, #text))) + end + return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text))) end ---Safe version of vim.str_byteindex diff --git a/lua/cmp/view/ghost_text_view.lua b/lua/cmp/view/ghost_text_view.lua index a6457f1..35f456d 100644 --- a/lua/cmp/view/ghost_text_view.lua +++ b/lua/cmp/view/ghost_text_view.lua @@ -90,15 +90,15 @@ ghost_text_view.text_gen = function(self, line, cursor_col) if self.entry:get_completion_item().insertTextFormat == types.lsp.InsertTextFormat.Snippet then word = tostring(snippet.parse(word)) end - local word_clen = vim.str_utfindex(word) + local word_clen = vim.fn.strchars(word, true) local cword = string.sub(line, self.entry.offset, cursor_col) - local cword_clen = vim.str_utfindex(cword) + local cword_clen = vim.fn.strchars(cword, true) -- Number of characters from entry text (word) to be displayed as ghost thext local nchars = word_clen - cword_clen -- Missing characters to complete the entry text local text if nchars > 0 then - text = string.sub(word, vim.str_byteindex(word, word_clen - nchars) + 1) + text = string.sub(word, misc.to_vimindex(word, word_clen - nchars)) else text = '' end