aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-03-29 18:37:07 +0800
committerGitHub <noreply@github.com>2024-03-29 18:37:07 +0800
commitf29c41d665fde8e03848db12093219466deceda2 (patch)
treee7b8c7f926e6f3097bd3992fbae507ddf3c0eb46
parentfc19ee01acc30bac371f5a56a97a5cdfe8094014 (diff)
downloadrneovim-f29c41d665fde8e03848db12093219466deceda2.tar.gz
rneovim-f29c41d665fde8e03848db12093219466deceda2.tar.bz2
rneovim-f29c41d665fde8e03848db12093219466deceda2.zip
test: add a bit more testing for vim.on_key() (#28095)
Also: - Don't use NUMBUFLEN as buffer length as its unrelated. - Restore accidentally removed comment from last commit.
-rw-r--r--src/nvim/getchar.c2
-rw-r--r--src/nvim/highlight_group.c2
-rw-r--r--src/nvim/lua/executor.c2
-rw-r--r--src/nvim/os/input.c4
-rw-r--r--test/functional/lua/vim_spec.lua20
5 files changed, 23 insertions, 7 deletions
diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c
index 2a5b8b00d4..0a848c4676 100644
--- a/src/nvim/getchar.c
+++ b/src/nvim/getchar.c
@@ -1122,6 +1122,8 @@ static void gotchars(const uint8_t *chars, size_t len)
pending--;
}
+ // When receiving a special key sequence, store it until we have all
+ // the bytes and we can decide what to do with it.
if ((pending == 0 || in_mbyte) && c == K_SPECIAL) {
pending += 2;
if (!in_mbyte) {
diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c
index f4800deb90..ca8c2999e1 100644
--- a/src/nvim/highlight_group.c
+++ b/src/nvim/highlight_group.c
@@ -1914,7 +1914,7 @@ static void set_hl_attr(int idx)
at_en.cterm_bg_color = (int16_t)sgp->sg_cterm_bg;
at_en.rgb_ae_attr = (int16_t)sgp->sg_gui;
// FIXME(tarruda): The "unset value" for rgb is -1, but since hlgroup is
- // initialized with 0(by garray functions), check for sg_rgb_{f,b}g_name
+ // initialized with 0 (by garray functions), check for sg_rgb_{f,b}g_name
// before setting attr_entry->{f,g}g_color to a other than -1
at_en.rgb_fg_color = sgp->sg_rgb_fg_idx != kColorIdxNone ? sgp->sg_rgb_fg : -1;
at_en.rgb_bg_color = sgp->sg_rgb_bg_idx != kColorIdxNone ? sgp->sg_rgb_bg : -1;
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index d5d35c5295..9e4b698b69 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -2066,7 +2066,7 @@ char *nlua_register_table_as_callable(const typval_T *const arg)
void nlua_execute_on_key(int c)
{
- char buf[NUMBUFLEN];
+ char buf[MB_MAXBYTES * 3 + 4];
size_t buf_len = special_to_buf(c, mod_mask, false, buf);
lua_State *const lstate = global_lstate;
diff --git a/src/nvim/os/input.c b/src/nvim/os/input.c
index baa99fca65..5218e50df6 100644
--- a/src/nvim/os/input.c
+++ b/src/nvim/os/input.c
@@ -255,9 +255,9 @@ size_t input_enqueue(String keys)
while (rbuffer_space(input_buffer) >= 19 && ptr < end) {
// A "<x>" form occupies at least 1 characters, and produces up
// to 19 characters (1 + 5 * 3 for the char and 3 for a modifier).
- // In the case of K_SPECIAL(0x80), 3 bytes are escaped and needed,
+ // In the case of K_SPECIAL (0x80), 3 bytes are escaped and needed,
// but since the keys are UTF-8, so the first byte cannot be
- // K_SPECIAL(0x80).
+ // K_SPECIAL (0x80).
uint8_t buf[19] = { 0 };
// Do not simplify the keys here. Simplification will be done later.
unsigned new_size
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index 7ab009659b..62ca20d599 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -3007,7 +3007,7 @@ describe('lua stdlib', function()
end)
describe('vim.on_key', function()
- it('tracks keystrokes', function()
+ it('tracks Unicode input', function()
insert([[hello world ]])
exec_lua [[
@@ -3022,10 +3022,24 @@ describe('lua stdlib', function()
end)
]]
- insert([[next 🤦 lines å ]])
+ insert([[next 🤦 lines å …]])
-- 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(keys, '')]])
+ end)
+
+ it('tracks input with modifiers', function()
+ exec_lua [[
+ keys = {}
+
+ vim.on_key(function(buf)
+ table.insert(keys, vim.fn.keytrans(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, '')]])
end)
it('allows removing on_key listeners', function()