aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/vim_spec.lua
diff options
context:
space:
mode:
authorTJ DeVries <devries.timothyj@gmail.com>2020-08-14 10:03:17 -0400
committerGitHub <noreply@github.com>2020-08-14 10:03:17 -0400
commit3ccdbc570d856ee3ff1f64204e352a40b9030ac2 (patch)
treee73d198bef4dce52bfd990dc57ea419e8b1fc703 /test/functional/lua/vim_spec.lua
parentaa48c1c724f7164485782a3a5a8ff7a94373f607 (diff)
downloadrneovim-3ccdbc570d856ee3ff1f64204e352a40b9030ac2.tar.gz
rneovim-3ccdbc570d856ee3ff1f64204e352a40b9030ac2.tar.bz2
rneovim-3ccdbc570d856ee3ff1f64204e352a40b9030ac2.zip
lua: add vim.register_keystroke_callback (#12536)
* feat: Add vim.register_keystroke_callback * fixup: Forgot to remove mention of old option * fixup: Answer jamessan comments * fixup: Answer norcalli comments * fixup: portability * Update runtime/doc/lua.txt Co-authored-by: Ashkan Kiani <ashkan.k.kiani@gmail.com>
Diffstat (limited to 'test/functional/lua/vim_spec.lua')
-rw-r--r--test/functional/lua/vim_spec.lua98
1 files changed, 98 insertions, 0 deletions
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index 9b2697b3c2..a9e8ca9686 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -1068,6 +1068,104 @@ describe('lua stdlib', function()
eq({5,15}, exec_lua[[ return vim.region(0,{1,5},{1,14},'v',true)[1] ]])
end)
+ describe('vim.execute_on_keystroke', function()
+ it('should keep track of keystrokes', function()
+ helpers.insert([[hello world ]])
+
+ exec_lua [[
+ KeysPressed = {}
+
+ vim.register_keystroke_callback(function(buf)
+ if buf:byte() == 27 then
+ buf = "<ESC>"
+ end
+
+ table.insert(KeysPressed, buf)
+ end)
+ ]]
+
+ helpers.insert([[next 🤦 lines å ]])
+
+ -- It has escape in the keys pressed
+ eq('inext 🤦 lines å <ESC>', exec_lua [[return table.concat(KeysPressed, '')]])
+ end)
+
+ it('should allow removing trackers.', function()
+ helpers.insert([[hello world]])
+
+ exec_lua [[
+ KeysPressed = {}
+
+ return vim.register_keystroke_callback(function(buf)
+ if buf:byte() == 27 then
+ buf = "<ESC>"
+ end
+
+ table.insert(KeysPressed, buf)
+ end, vim.api.nvim_create_namespace("logger"))
+ ]]
+
+ helpers.insert([[next lines]])
+
+ exec_lua("vim.register_keystroke_callback(nil, vim.api.nvim_create_namespace('logger'))")
+
+ helpers.insert([[more lines]])
+
+ -- It has escape in the keys pressed
+ eq('inext lines<ESC>', exec_lua [[return table.concat(KeysPressed, '')]])
+ end)
+
+ it('should not call functions that error again.', function()
+ helpers.insert([[hello world]])
+
+ exec_lua [[
+ KeysPressed = {}
+
+ return vim.register_keystroke_callback(function(buf)
+ if buf:byte() == 27 then
+ buf = "<ESC>"
+ end
+
+ table.insert(KeysPressed, buf)
+
+ if buf == 'l' then
+ error("Dumb Error")
+ end
+ end)
+ ]]
+
+ helpers.insert([[next lines]])
+ helpers.insert([[more lines]])
+
+ -- Only the first letter gets added. After that we remove the callback
+ eq('inext l', exec_lua [[ return table.concat(KeysPressed, '') ]])
+ end)
+
+ it('should process mapped keys, not unmapped keys', function()
+ exec_lua [[
+ KeysPressed = {}
+
+ vim.cmd("inoremap hello world")
+
+ vim.register_keystroke_callback(function(buf)
+ if buf:byte() == 27 then
+ buf = "<ESC>"
+ end
+
+ table.insert(KeysPressed, buf)
+ end)
+ ]]
+
+ helpers.insert("hello")
+
+ local next_status = exec_lua [[
+ return table.concat(KeysPressed, '')
+ ]]
+
+ eq("iworld<ESC>", next_status)
+ end)
+ end)
+
describe('vim.wait', function()
before_each(function()
exec_lua[[