commit 4e4914ab2e523f100c06fc5fb253f8625cc67232
parent 116a3f4683de501228b422f67cf1030bee78759c
Author: Andrey Mishchenko <mishchea@gmail.com>
Date: Fri, 22 Apr 2022 20:01:08 -0400
fix(lua): don't mutate opts parameter of vim.keymap.del (#18227)
`vim.keymap.del` takes an `opts` parameter that lets caller refer to and
delete buffer-local mappings. For some reason the implementation of
`vim.keymap.del` mutates the table that is passed in, setting
`opts.buffer` to `nil`. This is wrong and also undocumented.
Diffstat:
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/runtime/lua/vim/keymap.lua b/runtime/lua/vim/keymap.lua
@@ -130,7 +130,6 @@ function keymap.del(modes, lhs, opts)
local buffer = false
if opts.buffer ~= nil then
buffer = opts.buffer == true and 0 or opts.buffer
- opts.buffer = nil
end
if buffer == false then
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
@@ -2755,6 +2755,39 @@ describe('vim.keymap', function()
eq('\nNo mapping found', helpers.exec_capture('nmap asdf'))
end)
+ it('works with buffer-local mappings', function()
+ eq(0, exec_lua [[
+ GlobalCount = 0
+ vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end, {buffer=true})
+ return GlobalCount
+ ]])
+
+ feed('asdf\n')
+
+ eq(1, exec_lua[[return GlobalCount]])
+
+ exec_lua [[
+ vim.keymap.del('n', 'asdf', {buffer=true})
+ ]]
+
+ feed('asdf\n')
+
+ eq(1, exec_lua[[return GlobalCount]])
+ eq('\nNo mapping found', helpers.exec_capture('nmap asdf'))
+ end)
+
+ it('does not mutate the opts parameter', function()
+ eq(true, exec_lua [[
+ opts = {buffer=true}
+ vim.keymap.set('n', 'asdf', function() end, opts)
+ return opts.buffer
+ ]])
+ eq(true, exec_lua [[
+ vim.keymap.del('n', 'asdf', opts)
+ return opts.buffer
+ ]])
+ end)
+
it('can do <Plug> mappings', function()
eq(0, exec_lua [[
GlobalCount = 0