This commit is contained in:
max397574 2024-09-15 14:40:24 +05:45 committed by GitHub
commit 4808220955
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 13 deletions

View File

@ -2809,7 +2809,7 @@ vim.keymap.del({modes}, {lhs}, {opts}) *vim.keymap.del()*
See also: ~
• |vim.keymap.set()|
vim.keymap.set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
vim.keymap.set({mode}, {lhs}, {opts}, {rhs}) *vim.keymap.set()*
Adds a new |mapping|. Examples: >lua
-- Map to a Lua function:
vim.keymap.set('n', 'lhs', function() print("real lua function") end)
@ -2828,9 +2828,9 @@ vim.keymap.set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
Parameters: ~
• {mode} (`string|string[]`) Mode short-name, see |nvim_set_keymap()|.
Can also be list of modes to create mapping on multiple modes.
• {lhs} (`string`) Left-hand side |{lhs}| of the mapping.
• {rhs} (`string|function`) Right-hand side |{rhs}| of the mapping,
can be a Lua function.
• {lhs} (`string|string[]`) Left-hand side |{lhs}| of the mapping. Can
also be list of strings to map different key sequences to the
same rhs.
• {opts} (`table?`) Table of |:map-arguments|. Same as
|nvim_set_keymap()| {opts}, except:
• {replace_keycodes} defaults to `true` if "expr" is `true`.
@ -2840,6 +2840,8 @@ vim.keymap.set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
`0` or `true` for current buffer.
• {remap}? (`boolean`, default: `false`) Make the mapping
recursive. Inverse of {noremap}.
• {rhs} (`string|function`) Right-hand side |{rhs}| of the mapping,
can be a Lua function.
See also: ~
• |nvim_set_keymap()|

View File

@ -148,7 +148,9 @@ LSP
LUA
• TODO
• |vim.keymap.set()| now allows passing the rhs as last parameter.
• |vim.keymap.set()| now allows passing a table of strings as lhs to map
multiple keys to the same rhs.
OPTIONS

View File

@ -35,27 +35,38 @@ local keymap = {}
---
---@param mode string|string[] Mode short-name, see |nvim_set_keymap()|.
--- Can also be list of modes to create mapping on multiple modes.
---@param lhs string Left-hand side |{lhs}| of the mapping.
---@param lhs string|string[] Left-hand side |{lhs}| of the mapping.
--- Can also be list of strings to map different key sequences to the
--- same rhs.
---@param opts? vim.keymap.set.Opts
---@param rhs string|function Right-hand side |{rhs}| of the mapping, can be a Lua function.
---
---@param opts? vim.keymap.set.Opts
---@see |nvim_set_keymap()|
---@see |maparg()|
---@see |mapcheck()|
---@see |mapset()|
function keymap.set(mode, lhs, rhs, opts)
---@overload fun(mode: string|string[], lhs: string|string[], rhs: string|function, opts: vim.keymap.set.Opts?)
function keymap.set(mode, lhs, opts, rhs)
vim.validate({
mode = { mode, { 's', 't' } },
lhs = { lhs, 's' },
rhs = { rhs, { 's', 'f' } },
opts = { opts, 't', true },
lhs = { lhs, { 's', 't' } },
opts = { opts, { 't', 'f', 's' }, true },
rhs = { rhs, { 's', 'f', 't' }, true },
})
if type(opts) == 'function' or type(opts) == 'string' then
rhs, opts = opts, rhs
end
---@type vim.keymap.set.Opts
opts = vim.deepcopy(opts or {}, true)
---@cast mode string[]
mode = type(mode) == 'string' and { mode } or mode
---@cast lhs string[]
lhs = type(lhs) == 'string' and { lhs } or lhs
if opts.expr and opts.replace_keycodes ~= false then
opts.replace_keycodes = true
end
@ -78,12 +89,16 @@ function keymap.set(mode, lhs, rhs, opts)
local bufnr = opts.buffer == true and 0 or opts.buffer --[[@as integer]]
opts.buffer = nil ---@type integer?
for _, m in ipairs(mode) do
vim.api.nvim_buf_set_keymap(bufnr, m, lhs, rhs, opts)
for _, l in ipairs(lhs) do
vim.api.nvim_buf_set_keymap(bufnr, m, l, rhs, opts)
end
end
else
opts.buffer = nil
for _, m in ipairs(mode) do
vim.api.nvim_set_keymap(m, lhs, rhs, opts)
for _, l in ipairs(lhs) do
vim.api.nvim_set_keymap(m, l, rhs, opts)
end
end
end
end

View File

@ -4058,6 +4058,37 @@ describe('vim.keymap', function()
eq(1, exec_lua [[return GlobalCount]])
end)
it('can make a mapping with multiple lhs at once', function()
eq(
0,
exec_lua [[
GlobalCount = 0
vim.keymap.set('n', { 'asdf', 'ghjk' }, function() GlobalCount = GlobalCount + 1 end)
return GlobalCount
]]
)
feed('asdf\n')
feed('ghjk\n')
eq(2, exec_lua [[return GlobalCount]])
end)
it('can make a mapping with rhs as last parameter', function()
eq(
0,
exec_lua [[
GlobalCount = 0
vim.keymap.set('n', 'asdf',, {}, function() GlobalCount = GlobalCount + 1 end)
return GlobalCount
]]
)
feed('asdf\n')
eq(1, exec_lua [[return GlobalCount]])
end)
it('can make an expr mapping', function()
exec_lua [[
vim.keymap.set('n', 'aa', function() return '<Insert>π<C-V><M-π>foo<lt><Esc>' end, {expr = true})