Skip to content

Commit 3fce8d9

Browse files
committed
improve backward compatibility
1 parent c4c784a commit 3fce8d9

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lua/cmp/utils/misc.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ end
197197
---@return integer
198198
misc.to_utfindex = function(text, vimindex)
199199
vimindex = vimindex or #text + 1
200-
return vim.str_utfindex(text, 'utf-16', math.max(0, math.min(vimindex - 1, #text)))
200+
if vim.fn.has('nvim-0.11') then
201+
return vim.str_utfindex(text, 'utf-16', math.max(0, math.min(vimindex - 1, #text)))
202+
end
203+
return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text)))
201204
end
202205

203206
---Safe version of vim.str_byteindex

lua/cmp/view/ghost_text_view.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ ghost_text_view.text_gen = function(self, line, cursor_col)
9090
if self.entry:get_completion_item().insertTextFormat == types.lsp.InsertTextFormat.Snippet then
9191
word = tostring(snippet.parse(word))
9292
end
93-
local word_clen = vim.str_utfindex(word)
93+
local word_clen = vim.fn.strchars(word, true)
9494
local cword = string.sub(line, self.entry.offset, cursor_col)
95-
local cword_clen = vim.str_utfindex(cword)
95+
local cword_clen = vim.fn.strchars(cword, true)
9696
-- Number of characters from entry text (word) to be displayed as ghost thext
9797
local nchars = word_clen - cword_clen
9898
-- Missing characters to complete the entry text
9999
local text
100100
if nchars > 0 then
101-
text = string.sub(word, vim.str_byteindex(word, word_clen - nchars) + 1)
101+
text = string.sub(word, misc.to_vimindex(word, word_clen - nchars))
102102
else
103103
text = ''
104104
end

0 commit comments

Comments
 (0)