diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 6ec11a0..efc85ba 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -125,7 +125,7 @@ core.on_keymap = function(self, keys, fallback) }, function() local ctx = self:get_context() local word = e:get_word() - if string.sub(ctx.cursor_before_line, - #word, ctx.cursor.col - 1) == word and is_printable then + if string.sub(ctx.cursor_before_line, -#word, ctx.cursor.col - 1) == word and is_printable then fallback() else self:reset() @@ -381,7 +381,7 @@ core.confirm = function(self, e, option, callback) -- This logic must be used nvim_buf_set_text. -- If not used, the snippet engine's placeholder wil be broken. vim.api.nvim_buf_set_text(0, e.context.cursor.row - 1, e:get_offset() - 1, ctx.cursor.row - 1, ctx.cursor.col - 1, { - e.context.cursor_before_line:sub(e:get_offset()) + e.context.cursor_before_line:sub(e:get_offset()), }) vim.api.nvim_win_set_cursor(0, { e.context.cursor.row, e.context.cursor.col - 1 }) end @@ -444,7 +444,7 @@ core.confirm = function(self, e, option, callback) if api.is_insert_mode() then if false then --To use complex expansion debug. - vim.pretty_print({ + vim.pretty_print({ -- luacheck: ignore diff_before = diff_before, diff_after = diff_after, new_text = new_text, diff --git a/lua/cmp/utils/api.lua b/lua/cmp/utils/api.lua index 8153c9e..f304a78 100644 --- a/lua/cmp/utils/api.lua +++ b/lua/cmp/utils/api.lua @@ -55,7 +55,7 @@ end api.get_screen_cursor = function() if api.is_cmdline_mode() then local cursor = api.get_cursor() - return { cursor[1], cursor[2] + 1 } + return { cursor[1], vim.fn.strdisplaywidth(string.sub(vim.fn.getcmdline(), 1, cursor[2] + 1)) } end local cursor = api.get_cursor() local pos = vim.fn.screenpos(0, cursor[1], cursor[2] + 1) diff --git a/lua/cmp/utils/api_spec.lua b/lua/cmp/utils/api_spec.lua index 5363b48..31bbee2 100644 --- a/lua/cmp/utils/api_spec.lua +++ b/lua/cmp/utils/api_spec.lua @@ -4,8 +4,8 @@ local feedkeys = require('cmp.utils.feedkeys') local api = require('cmp.utils.api') describe('api', function() + before_each(spec.before) describe('get_cursor', function() - before_each(spec.before) it('insert-mode', function() local cursor feedkeys.call(keymap.t('i\t1234567890'), 'nx', function() @@ -24,8 +24,26 @@ describe('api', function() end) end) + describe('get_screen_cursor', function() + it('insert-mode', function() + local screen_cursor + feedkeys.call(keymap.t('iあいうえお'), 'nx', function() + screen_cursor = api.get_screen_cursor() + end) + assert.are.equal(10, screen_cursor[2]) + end) + it('cmdline-mode', function() + local screen_cursor + keymap.set_map(0, 'c', '(cmp-spec-spy)', function() + screen_cursor = api.get_screen_cursor() + end, { expr = true, noremap = true }) + feedkeys.call(keymap.t(':あいうえお'), 'n') + feedkeys.call(keymap.t('(cmp-spec-spy)'), 'x') + assert.are.equal(10, screen_cursor[2]) + end) + end) + describe('get_cursor_before_line', function() - before_each(spec.before) it('insert-mode', function() local cursor_before_line feedkeys.call(keymap.t('i\t1234567890'), 'nx', function() diff --git a/plugin/cmp.lua b/plugin/cmp.lua index acffc43..eaacad8 100644 --- a/plugin/cmp.lua +++ b/plugin/cmp.lua @@ -57,4 +57,3 @@ vim.api.nvim_create_user_command('CmpStatus', function() end, { desc = 'Check status of cmp sources' }) vim.cmd([[doautocmd User CmpReady]]) -