This commit is contained in:
hrsh7th
2021-11-23 19:37:25 +09:00
parent 876dd1a427
commit 9b5d64d906
2 changed files with 8 additions and 7 deletions

View File

@@ -15,7 +15,6 @@ local event = require('cmp.utils.event')
local SOURCE_TIMEOUT = 500
local THROTTLE_TIME = 100
local DEBOUNCE_TIME = 50
---@class cmp.Core
---@field public suspending boolean
@@ -295,7 +294,7 @@ core.confirm = function(self, e, option, callback)
feedkeys.call('', 'n', function()
local ctx = context.new()
local keys = {}
table.insert(keys, keymap.backspace(ctx.cursor.character - vim.str_utfindex(ctx.cursor_line, e:get_offset() - 1)))
table.insert(keys, keymap.backspace(ctx.cursor.character - misc.to_utfindex(ctx.cursor_line, e:get_offset())))
table.insert(keys, e:get_word())
table.insert(keys, keymap.undobreak())
feedkeys.call(table.concat(keys, ''), 'int')
@@ -304,7 +303,7 @@ core.confirm = function(self, e, option, callback)
local ctx = context.new()
if api.is_cmdline_mode() then
local keys = {}
table.insert(keys, keymap.backspace(ctx.cursor.character - vim.str_utfindex(ctx.cursor_line, e:get_offset() - 1)))
table.insert(keys, keymap.backspace(ctx.cursor.character - misc.to_utfindex(ctx.cursor_line, e:get_offset())))
table.insert(keys, string.sub(e.context.cursor_before_line, e:get_offset()))
feedkeys.call(table.concat(keys, ''), 'int')
else
@@ -377,9 +376,9 @@ core.confirm = function(self, e, option, callback)
local position = completion_item.textEdit.range.start
position.line = position.line + (#texts - 1)
if #texts == 1 then
position.character = position.character + vim.str_utfindex(texts[1])
position.character = position.character + misc.to_utfindex(texts[1])
else
position.character = vim.str_utfindex(texts[#texts])
position.character = misc.to_utfindex(texts[#texts])
end
local pos = types.lsp.Position.to_vim(0, position)
vim.api.nvim_win_set_cursor(0, { pos.row, pos.col - 1 })

View File

@@ -144,9 +144,10 @@ end
---Safe version of vim.str_utfindex
---@param text string
---@param vimindex number
---@param vimindex number|nil
---@return number
misc.to_utfindex = function(text, vimindex)
vimindex = vimindex or #text + 1
return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text)))
end
@@ -155,6 +156,7 @@ end
---@param utfindex number
---@return number
misc.to_vimindex = function(text, utfindex)
utfindex = utfindex or #text
for i = utfindex, 1, -1 do
local s, v = pcall(function()
return vim.str_byteindex(text, i) + 1