Files
nvim-cmp/lua/cmp/utils/feedkeys.lua
hrsh7th 5054c14490 dev (#956)
* Adjust empty line count

* Implement vim.api.nvim_create_autocmd (#844)

* Implement vim.api.nvim_create_autocmd

* Only use vim.api.nvim_create_autocmd on nighly

* Cleanup

* cleanup

* Rename autos.lua to autocmds.lua

* Fix forgot to rename autos to autocmds

* Remove legacy autocmd

* Add descriptions on autocmds

* Update descriptions on autocmds

* Update CmpStatus command to lua API (#922)

Signed-off-by: Micah Halter <micah@balena.io>

* Move highlights to nvim_set_hl lua API (#925)

Signed-off-by: Micah Halter <micah@balena.io>

* Add default to highlight

* Refactor autocmds

* fmt

* Improve performance

* Fix bug

* Improve matching logic
Fixes https://github.com/hrsh7th/nvim-cmp/discussions/954

* Fix format

* Improve performance
Fix #825

* Fix cmdline redraw bug

* Fix event

Co-authored-by: hrsh7th <>
Co-authored-by: zer09 <zer09@users.noreply.github.com>
Co-authored-by: Micah Halter <micah@mehalter.com>
2022-05-04 01:47:01 +09:00

54 lines
1.8 KiB
Lua

local keymap = require('cmp.utils.keymap')
local misc = require('cmp.utils.misc')
local feedkeys = {}
feedkeys.call = setmetatable({
callbacks = {},
}, {
__call = function(self, keys, mode, callback)
local is_insert = string.match(mode, 'i') ~= nil
local is_immediate = string.match(mode, 'x') ~= nil
local queue = {}
if #keys > 0 then
table.insert(queue, { keymap.t('<Cmd>setlocal lazyredraw<CR>'), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal textwidth=0<CR>'), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal backspace=2<CR>'), 'n' })
table.insert(queue, { keys, string.gsub(mode, '[itx]', ''), true })
table.insert(queue, { keymap.t('<Cmd>setlocal %slazyredraw<CR>'):format(vim.o.lazyredraw and '' or 'no'), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal textwidth=%s<CR>'):format(vim.bo.textwidth or 0), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal backspace=%s<CR>'):format(vim.go.backspace or 2), 'n' })
end
if callback then
local id = misc.id('cmp.utils.feedkeys.call')
self.callbacks[id] = callback
table.insert(queue, { keymap.t('<Cmd>call v:lua.cmp.utils.feedkeys.call.run(%s)<CR>'):format(id), 'n', true })
end
if is_insert then
for i = #queue, 1, -1 do
vim.api.nvim_feedkeys(queue[i][1], queue[i][2] .. 'i', queue[i][3])
end
else
for i = 1, #queue do
vim.api.nvim_feedkeys(queue[i][1], queue[i][2], queue[i][3])
end
end
if is_immediate then
vim.api.nvim_feedkeys('', 'x', true)
end
end,
})
misc.set(_G, { 'cmp', 'utils', 'feedkeys', 'call', 'run' }, function(id)
if feedkeys.call.callbacks[id] then
feedkeys.call.callbacks[id]()
feedkeys.call.callbacks[id] = nil
end
return ''
end)
return feedkeys