aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-03-31 11:20:05 +0800
committerGitHub <noreply@github.com>2024-03-31 11:20:05 +0800
commite1ff2c51cad755d0ddc04a23df23e317d77023ed (patch)
treea32389bcb708f2b0efed0134ffafc206b527db08 /test
parent12240600f5d2c992aa77bc4592edc16814abfafd (diff)
downloadrneovim-e1ff2c51cad755d0ddc04a23df23e317d77023ed.tar.gz
rneovim-e1ff2c51cad755d0ddc04a23df23e317d77023ed.tar.bz2
rneovim-e1ff2c51cad755d0ddc04a23df23e317d77023ed.zip
feat(lua): pass keys before mapping to vim.on_key() callback (#28098)
Keys before mapping (i.e. typed keys) are passed as the second argument.
Diffstat (limited to 'test')
-rw-r--r--test/functional/editor/meta_key_spec.lua25
-rw-r--r--test/functional/lua/vim_spec.lua40
2 files changed, 61 insertions, 4 deletions
diff --git a/test/functional/editor/meta_key_spec.lua b/test/functional/editor/meta_key_spec.lua
index b57f5c3c35..e2bdf6ba96 100644
--- a/test/functional/editor/meta_key_spec.lua
+++ b/test/functional/editor/meta_key_spec.lua
@@ -141,4 +141,29 @@ describe('meta-keys #8226 #13042', function()
// This is some text: bar
// This is some text: baz]])
end)
+
+ it('ALT/META with vim.on_key()', function()
+ feed('ifoo<CR>bar<CR>baz<Esc>gg0')
+
+ exec_lua [[
+ keys = {}
+ typed = {}
+
+ vim.on_key(function(buf, typed_buf)
+ table.insert(keys, vim.fn.keytrans(buf))
+ table.insert(typed, vim.fn.keytrans(typed_buf))
+ end)
+ ]]
+
+ -- <M-"> is reinterpreted as <Esc>"
+ feed('qrviw"ayc$FOO.<M-">apq')
+ expect([[
+ FOO.foo
+ bar
+ baz]])
+
+ -- vim.on_key() callback should only receive <Esc>"
+ eq('qrviw"ayc$FOO.<Esc>"apq', exec_lua [[return table.concat(keys, '')]])
+ eq('qrviw"ayc$FOO.<Esc>"apq', exec_lua [[return table.concat(typed, '')]])
+ end)
end)
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index 62ca20d599..8722c4dbb5 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -3012,13 +3012,18 @@ describe('lua stdlib', function()
exec_lua [[
keys = {}
+ typed = {}
- vim.on_key(function(buf)
+ vim.on_key(function(buf, typed_buf)
if buf:byte() == 27 then
buf = "<ESC>"
end
+ if typed_buf:byte() == 27 then
+ typed_buf = "<ESC>"
+ end
table.insert(keys, buf)
+ table.insert(typed, typed_buf)
end)
]]
@@ -3026,20 +3031,41 @@ describe('lua stdlib', function()
-- It has escape in the keys pressed
eq('inext 🤦 lines å …<ESC>', exec_lua [[return table.concat(keys, '')]])
+ eq('inext 🤦 lines å …<ESC>', exec_lua [[return table.concat(typed, '')]])
end)
it('tracks input with modifiers', function()
exec_lua [[
keys = {}
+ typed = {}
- vim.on_key(function(buf)
+ vim.on_key(function(buf, typed_buf)
table.insert(keys, vim.fn.keytrans(buf))
+ table.insert(typed, vim.fn.keytrans(typed_buf))
end)
]]
feed([[i<C-V><C-;><C-V><C-…><Esc>]])
eq('i<C-V><C-;><C-V><C-…><Esc>', exec_lua [[return table.concat(keys, '')]])
+ eq('i<C-V><C-;><C-V><C-…><Esc>', exec_lua [[return table.concat(typed, '')]])
+ end)
+
+ it('works with character find and Select mode', function()
+ insert('12345')
+
+ exec_lua [[
+ typed = {}
+
+ vim.cmd('snoremap # @')
+
+ vim.on_key(function(buf, typed_buf)
+ table.insert(typed, vim.fn.keytrans(typed_buf))
+ end)
+ ]]
+
+ feed('F3gHβγδεζ<Esc>gH…<Esc>gH#$%^')
+ eq('F3gHβγδεζ<Esc>gH…<Esc>gH#$%^', exec_lua [[return table.concat(typed, '')]])
end)
it('allows removing on_key listeners', function()
@@ -3101,23 +3127,29 @@ describe('lua stdlib', function()
eq('inext l', exec_lua [[ return table.concat(keys, '') ]])
end)
- it('processes mapped keys, not unmapped keys', function()
+ it('argument 1 is keys after mapping, argument 2 is typed keys', function()
exec_lua [[
keys = {}
+ typed = {}
vim.cmd("inoremap hello world")
- vim.on_key(function(buf)
+ vim.on_key(function(buf, typed_buf)
if buf:byte() == 27 then
buf = "<ESC>"
end
+ if typed_buf:byte() == 27 then
+ typed_buf = "<ESC>"
+ end
table.insert(keys, buf)
+ table.insert(typed, typed_buf)
end)
]]
insert('hello')
eq('iworld<ESC>', exec_lua [[return table.concat(keys, '')]])
+ eq('ihello<ESC>', exec_lua [[return table.concat(typed, '')]])
end)
it('can call vim.fn functions on Ctrl-C #17273', function()