Skip to content

Commit

Permalink
fix: implementation of newer vim.str_utfindex not being adapted for s…
Browse files Browse the repository at this point in the history
…table neovim
  • Loading branch information
user committed Nov 19, 2024
1 parent 573b2b3 commit edd7cd1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
49 changes: 35 additions & 14 deletions lua/cmp/core_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,41 @@ describe('cmp.core', function()
end)

local char = '🗿'
for _, case in ipairs({
{
encoding = types.lsp.PositionEncodingKind.UTF8,
char_size = #char,
},
{
encoding = types.lsp.PositionEncodingKind.UTF16,
char_size = vim.str_utfindex(char, types.lsp.PositionEncodingKind.UTF16),
},
{
encoding = types.lsp.PositionEncodingKind.UTF32,
char_size = vim.str_utfindex(char, types.lsp.PositionEncodingKind.UTF32),
},
}) do
local encoding_table

if vim.fn.has('nvim-0.11') == 1 then
encoding_table = {
{
encoding = types.lsp.PositionEncodingKind.UTF8,
char_size = #char,
},
{
encoding = types.lsp.PositionEncodingKind.UTF16,
char_size = vim.str_utfindex(char, types.lsp.PositionEncodingKind.UTF16),
},
{
encoding = types.lsp.PositionEncodingKind.UTF32,
char_size = vim.str_utfindex(char, types.lsp.PositionEncodingKind.UTF32),
},
}
else
encoding_table = {
{
encoding = types.lsp.PositionEncodingKind.UTF8,
char_size = #char,
},
{
encoding = types.lsp.PositionEncodingKind.UTF16,
char_size = select(2, vim.str_utfindex(char)),
},
{
encoding = types.lsp.PositionEncodingKind.UTF32,
char_size = select(1, vim.str_utfindex(char)),
},
}
end

for _, case in ipairs(encoding_table) do
it('textEdit & multibyte: ' .. case.encoding, function()
local state = confirm(keymap.t('i%s:%s%s:%s<Left><Left><Left>'):format(char, char, char, char), char, {
label = char .. char .. char,
Expand Down
16 changes: 14 additions & 2 deletions lua/cmp/types/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ lsp.Position = {
return position
end

local has_nvim11 = vim.fn.has("nvim-0.11") == 1

local utf8 = lsp.Position.to_utf8(text, position, from_encoding)
for index = utf8.character, 0, -1 do
local ok, utf16index = pcall(function()
return vim.str_utfindex(text, lsp.PositionEncodingKind.UTF16, index)
if has_nvim11 then
return vim.str_utfindex(text, lsp.PositionEncodingKind.UTF16, index)
else
return select(2, vim.str_utfindex(text, index))
end
end)
if ok then
return { line = utf8.line, character = utf16index }
Expand All @@ -106,10 +112,16 @@ lsp.Position = {
return position
end

local has_nvim11 = vim.fn.has("nvim-0.11") == 1

local utf8 = lsp.Position.to_utf8(text, position, from_encoding)
for index = utf8.character, 0, -1 do
local ok, utf32index = pcall(function()
return vim.str_utfindex(text, lsp.PositionEncodingKind.UTF32, index)
if has_nvim11 then
return vim.str_utfindex(text, lsp.PositionEncodingKind.UTF32, index)
else
return select(1, vim.str_utfindex(text, index))
end
end)
if ok then
return { line = utf8.line, character = utf32index }
Expand Down
6 changes: 5 additions & 1 deletion lua/cmp/utils/misc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ end
---@return integer
misc.to_utfindex = function(text, vimindex)
vimindex = vimindex or #text + 1
return vim.str_utfindex(text, 'utf-32', math.max(0, math.min(vimindex - 1, #text)))
if vim.fn.has("nvim-0.11") == 1 then
return vim.str_utfindex(text, 'utf-32', math.max(0, math.min(vimindex - 1, #text)))
else
return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text)))
end
end

---Safe version of vim.str_byteindex
Expand Down
19 changes: 15 additions & 4 deletions lua/cmp/view/ghost_text_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,26 @@ ghost_text_view.text_gen = function(self, line, cursor_col, entry)

-- Trim cursorline ghost text
local function trim_text(word)
local word_clen = vim.str_utfindex(word, types.lsp.PositionEncodingKind.UTF32)
local cword = string.sub(line, entry:get_offset(), cursor_col)
local cword_clen = vim.str_utfindex(cword, types.lsp.PositionEncodingKind.UTF32)
local word_clen
local cword
local cword_clen

if vim.fn.has('nvim-0.11') == 1 then
word_clen = vim.str_utfindex(word, types.lsp.PositionEncodingKind.UTF32)
cword = string.sub(line, entry:get_offset(), cursor_col)
cword_clen = vim.str_utfindex(cword, types.lsp.PositionEncodingKind.UTF32)
else
word_clen = vim.str_utfindex(word)
cword = string.sub(line, entry:get_offset(), cursor_col)
cword_clen = vim.str_utfindex(cword)
end

-- 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, types.lsp.PositionEncodingKind.UTF32, word_clen - nchars) + 1)
text = string.sub(word, vim.str_byteindex(word, word_clen - nchars) + 1)
else
text = ''
end
Expand Down

0 comments on commit edd7cd1

Please sign in to comment.