diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index de1de23..af10309 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -64,3 +64,4 @@ return function() sources = {}, } end + diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 1bd490b..ba1c2f3 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -1,5 +1,6 @@ local debug = require('cmp.utils.debug') local char = require('cmp.utils.char') +local str = require('cmp.utils.str') local async = require('cmp.utils.async') local keymap = require('cmp.utils.keymap') local context = require('cmp.context') @@ -196,7 +197,7 @@ core.confirm = vim.schedule_wrap(function(e, option, callback) debug.log('entry.confirm', e) local ctx = context.new() - keymap.feedkeys(keymap.t('U' .. string.rep('', ctx.cursor.col - e.context.cursor.col)), 'n', function() + keymap.feedkeys(keymap.t('U' .. string.rep('', str.chars(ctx.cursor_line, e.context.cursor.col, ctx.cursor.col - 1))), 'n', function() --@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334 if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then local pre = context.new() @@ -245,12 +246,13 @@ core.confirm = vim.schedule_wrap(function(e, option, callback) is_snippet = is_snippet and completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet is_snippet = is_snippet and vim.lsp.util.parse_snippet(completion_item.textEdit.newText) ~= completion_item.textEdit.newText + local range = types.lsp.Range.to_vim(ctx.bufnr, completion_item.textEdit.range) local keys = {} - if completion_item.textEdit.range['end'].character > e.context.cursor.character then - table.insert(keys, keymap.t(string.rep('U', completion_item.textEdit.range['end'].character - e.context.cursor.character))) + if e.context.cursor.col < range['end'].col then + table.insert(keys, keymap.t(string.rep('U', str.chars(ctx.cursor_line, e.context.cursor.col, range['end'].col - 1)))) end - if e.context.cursor.character > completion_item.textEdit.range.start.character then - table.insert(keys, keymap.t(string.rep('', e.context.cursor.character - completion_item.textEdit.range.start.character))) + if range.start.col < e.context.cursor.col then + table.insert(keys, keymap.t(string.rep('', str.chars(ctx.cursor_line, range.start.col, e.context.cursor.col - 1)))) end if is_snippet then diff --git a/lua/cmp/utils/str.lua b/lua/cmp/utils/str.lua index 2ad353a..2572ca6 100644 --- a/lua/cmp/utils/str.lua +++ b/lua/cmp/utils/str.lua @@ -135,6 +135,15 @@ str.get_word = function(text, stop_char) return text end +---Get character length. +---@param text string +---@param s number +---@param e number +---@return number +str.chars = function(text, s, e) + return vim.fn.strchars(string.sub(text, s, e)) +end + ---Oneline ---@param text string ---@return string