Skip to content

Commit

Permalink
Fix cmdline confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed Nov 5, 2021
1 parent d0231d0 commit 1774ff0
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 27 deletions.
41 changes: 26 additions & 15 deletions lua/cmp/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -372,23 +372,34 @@ core.confirm = function(self, e, option, callback)
completion_item.textEdit.range = e:get_insert_range()
end

local is_snippet = completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet
local diff_before = e.context.cursor.character - completion_item.textEdit.range.start.character
local diff_after = completion_item.textEdit.range['end'].character - e.context.cursor.character
local new_text = completion_item.textEdit.newText
completion_item.textEdit.range.start.line = ctx.cursor.line
completion_item.textEdit.range.start.character = ctx.cursor.character - (e.context.cursor.character - completion_item.textEdit.range.start.character)
completion_item.textEdit.range['end'].line = ctx.cursor.line
completion_item.textEdit.range['end'].character = ctx.cursor.character + (completion_item.textEdit.range['end'].character - e.context.cursor.character)
if is_snippet then
completion_item.textEdit.newText = ''
end
vim.fn['cmp#apply_text_edits'](ctx.bufnr, { completion_item.textEdit })
if is_snippet then
config.get().snippet.expand({
body = new_text,
insert_text_mode = completion_item.insertTextMode,
})

if api.is_insert_mode() then
local is_snippet = completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet
completion_item.textEdit.range.start.line = ctx.cursor.line
completion_item.textEdit.range.start.character = ctx.cursor.character - diff_before
completion_item.textEdit.range['end'].line = ctx.cursor.line
completion_item.textEdit.range['end'].character = ctx.cursor.character + diff_after
if is_snippet then
completion_item.textEdit.newText = ''
end
vim.fn['cmp#apply_text_edits'](ctx.bufnr, { completion_item.textEdit })
if is_snippet then
config.get().snippet.expand({
body = new_text,
insert_text_mode = completion_item.insertTextMode,
})
end
next()
else
local keys = {}
table.insert(keys, string.rep(keymap.t('<BS>'), diff_before))
table.insert(keys, string.rep(keymap.t('<Del>'), diff_after))
table.insert(keys, new_text)
feedkeys.call(table.concat(keys, ''), 'n', next)
end
next()

-- Finalize
end, function()
Expand Down
39 changes: 39 additions & 0 deletions lua/cmp/utils/api_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local spec = require('cmp.utils.spec')
local keymap = require('cmp.utils.keymap')
local feedkeys = require('cmp.utils.feedkeys')
local api = require('cmp.utils.api')

describe('api', function()
describe('get_cursor', function()
before_each(spec.before)
it('insert-mode', function()
feedkeys.call(keymap.t('i\t1234567890'), 'n', function()
assert.are.same(api.get_cursor()[2], 10)
end)
feedkeys.call('', 'nx')
end)
it('cmdline-mode', function()
feedkeys.call(keymap.t(':\t1234567890'), 'n', function()
assert.are.same(api.get_cursor()[2], 10)
end)
feedkeys.call('', 'nx')
end)
end)

describe('get_cursor_before_line', function()
before_each(spec.before)
it('insert-mode', function()
feedkeys.call(keymap.t(':\t1234567890<Left><Left>'), 'n', function()
assert.are.same(api.get_cursor_before_line(), '\t12345678')
end)
feedkeys.call('', 'nx')
end)
it('cmdline-mode', function()
feedkeys.call(keymap.t(':\t1234567890<Left><Left>'), 'n', function()
assert.are.same(api.get_cursor_before_line(), '\t12345678')
end)
feedkeys.call('', 'nx')
end)
end)
end)

Empty file removed lua/cmp/utils/cache_spec.lua
Empty file.
2 changes: 1 addition & 1 deletion lua/cmp/utils/feedkeys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ feedkeys.call = setmetatable({
table.insert(queue, { keymap.t('<Cmd>set %slazyredraw<CR>'):format(vim.o.lazyredraw and '' or 'no'), 'n' })
table.insert(queue, { keymap.t('<Cmd>set eventignore=%s<CR>'):format(vim.o.eventignore or ''), 'n' })
end
if #keys > 0 or callback then
if callback then
local id = misc.id('cmp.utils.feedkeys.call')
self.callbacks[id] = function()
if callback then
Expand Down
14 changes: 3 additions & 11 deletions lua/cmp/utils/feedkeys_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@ describe('feedkeys', function()
before_each(spec.before)

it('dot-repeat', function()
feedkeys.call(keymap.t('iaiueo<Esc>'), 'nx')
assert.are.equal(vim.fn.getreg('.'), keymap.t('aiueo'))
end)
it('macro', function()
vim.fn.setreg('q', '')
vim.cmd([[normal! qq]])
feedkeys.call(keymap.t('iaiueo'), 'nt')
feedkeys.call(keymap.t('<Esc>'), 'nt', function()
vim.cmd([[normal! q]])
assert.are.equal(vim.fn.getreg('q'), keymap.t('iaiueo<Esc>'))
print(vim.fn.getreg('q'))
feedkeys.call(keymap.t('iaiueo<Esc>'), 'n', function()
assert.are.equal(vim.fn.getreg('.'), keymap.t('aiueo'))
end)
feedkeys.call('', 'nx')
end)
end)
1 change: 1 addition & 0 deletions lua/cmp/utils/keymap_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('keymap', function()
end)
end)
describe('realworld', function()
before_each(spec.before)
it('#226', function()
keymap.listen('i', '<c-n>', function(_, fallback)
fallback()
Expand Down
12 changes: 12 additions & 0 deletions lua/cmp/utils/spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ spec.before = function()
vim.cmd([[
bdelete!
enew!
imapclear
imapclear <buffer>
cmapclear
cmapclear <buffer>
smapclear
smapclear <buffer>
xmapclear
xmapclear <buffer>
tmapclear
tmapclear <buffer>
setlocal noswapfile
setlocal virtualedit=all
setlocal completeopt=menu,menuone,noselect
]])
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, true, true), 'x', true)
end

spec.state = function(text, row, col)
Expand Down

0 comments on commit 1774ff0

Please sign in to comment.