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.
This commit is contained in:
zeertzjq
2024-03-31 11:20:05 +08:00
committed by GitHub
parent 12240600f5
commit e1ff2c51ca
11 changed files with 139 additions and 26 deletions

View File

@@ -654,11 +654,14 @@ local on_key_cbs = {} --- @type table<integer,function>
---
---@note {fn} will be removed on error.
---@note {fn} will not be cleared by |nvim_buf_clear_namespace()|
---@note {fn} will receive the keys after mappings have been evaluated
---
---@param fn fun(key: string)? Function invoked on every key press. |i_CTRL-V|
--- Passing in nil when {ns_id} is specified removes the
--- callback associated with namespace {ns_id}.
---@param fn fun(key: string, typed: string)?
--- Function invoked on every key press. |i_CTRL-V|
--- {key} is the key after mappings have been applied, and
--- {typed} is the key(s) before mappings are applied, which
--- may be empty if {key} is produced by non-typed keys.
--- When {fn} is nil and {ns_id} is specified, the callback
--- associated with namespace {ns_id} is removed.
---@param ns_id integer? Namespace ID. If nil or 0, generates and returns a
--- new |nvim_create_namespace()| id.
---
@@ -684,11 +687,11 @@ end
--- Executes the on_key callbacks.
---@private
function vim._on_key(char)
function vim._on_key(buf, typed_buf)
local failed_ns_ids = {}
local failed_messages = {}
for k, v in pairs(on_key_cbs) do
local ok, err_msg = pcall(v, char)
local ok, err_msg = pcall(v, buf, typed_buf)
if not ok then
vim.on_key(nil, k)
table.insert(failed_ns_ids, k)