fix(lua): preserve argument lists which are not lists

This commit is contained in:
Björn Linse 2021-08-07 21:17:45 +02:00
parent 636ecd0c3b
commit 6896d22b63
3 changed files with 30 additions and 2 deletions

View File

@ -27,5 +27,14 @@ function F.nil_wrap(fn)
end end
end end
--- like {...} except preserve the lenght explicitly
function F.pack_len(...)
return {n=select('#', ...), ...}
end
--- like unpack() but use the length set by F.pack_len if present
function F.unpack_len(t)
return unpack(t, 1, t.n)
end
return F return F

View File

@ -273,8 +273,8 @@ end
---@see |vim.in_fast_event()| ---@see |vim.in_fast_event()|
function vim.schedule_wrap(cb) function vim.schedule_wrap(cb)
return (function (...) return (function (...)
local args = {...} local args = vim.F.pack_len(...)
vim.schedule(function() cb(unpack(args)) end) vim.schedule(function() cb(vim.F.unpack_len(args)) end)
end) end)
end end

View File

@ -17,6 +17,7 @@ local matches = helpers.matches
local source = helpers.source local source = helpers.source
local NIL = helpers.NIL local NIL = helpers.NIL
local retry = helpers.retry local retry = helpers.retry
local next_msg = helpers.next_msg
before_each(clear) before_each(clear)
@ -2178,6 +2179,24 @@ describe('lua stdlib', function()
end) end)
end) end)
describe('vim.schedule_wrap', function()
it('preserves argument lists', function()
exec_lua [[
local fun = vim.schedule_wrap(function(kling, klang, klonk)
vim.rpcnotify(1, 'mayday_mayday', {a=kling, b=klang, c=klonk})
end)
fun("BOB", nil, "MIKE")
]]
eq({'notification', 'mayday_mayday', {{a='BOB', c='MIKE'}}}, next_msg())
-- let's gooooo
exec_lua [[
vim.schedule_wrap(function(...) vim.rpcnotify(1, 'boogalo', select('#', ...)) end)(nil,nil,nil,nil)
]]
eq({'notification', 'boogalo', {4}}, next_msg())
end)
end)
describe('vim.api.nvim_buf_call', function() describe('vim.api.nvim_buf_call', function()
it('can access buf options', function() it('can access buf options', function()
local buf1 = meths.get_current_buf() local buf1 = meths.get_current_buf()