Fix multibyte character problem

This commit is contained in:
hrsh7th
2021-08-26 18:08:04 +09:00
parent f5c0772562
commit 600c6c9cc2
3 changed files with 17 additions and 5 deletions

View File

@@ -64,3 +64,4 @@ return function()
sources = {}, sources = {},
} }
end end

View File

@@ -1,5 +1,6 @@
local debug = require('cmp.utils.debug') local debug = require('cmp.utils.debug')
local char = require('cmp.utils.char') local char = require('cmp.utils.char')
local str = require('cmp.utils.str')
local async = require('cmp.utils.async') local async = require('cmp.utils.async')
local keymap = require('cmp.utils.keymap') local keymap = require('cmp.utils.keymap')
local context = require('cmp.context') local context = require('cmp.context')
@@ -196,7 +197,7 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
debug.log('entry.confirm', e) debug.log('entry.confirm', e)
local ctx = context.new() local ctx = context.new()
keymap.feedkeys(keymap.t('<C-g>U' .. string.rep('<BS>', ctx.cursor.col - e.context.cursor.col)), 'n', function() keymap.feedkeys(keymap.t('<C-g>U' .. string.rep('<BS>', 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 --@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 if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
local pre = context.new() 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 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 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 = {} local keys = {}
if completion_item.textEdit.range['end'].character > e.context.cursor.character then if e.context.cursor.col < range['end'].col then
table.insert(keys, keymap.t(string.rep('<C-g>U<Right><BS>', completion_item.textEdit.range['end'].character - e.context.cursor.character))) table.insert(keys, keymap.t(string.rep('<C-g>U<Right><BS>', str.chars(ctx.cursor_line, e.context.cursor.col, range['end'].col - 1))))
end end
if e.context.cursor.character > completion_item.textEdit.range.start.character then if range.start.col < e.context.cursor.col then
table.insert(keys, keymap.t(string.rep('<BS>', e.context.cursor.character - completion_item.textEdit.range.start.character))) table.insert(keys, keymap.t(string.rep('<BS>', str.chars(ctx.cursor_line, range.start.col, e.context.cursor.col - 1))))
end end
if is_snippet then if is_snippet then

View File

@@ -135,6 +135,15 @@ str.get_word = function(text, stop_char)
return text return text
end 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 ---Oneline
---@param text string ---@param text string
---@return string ---@return string