From 6896d22b63ad12de4c4990d0eece4a9b6f9cd88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 7 Aug 2021 21:17:45 +0200 Subject: [PATCH] fix(lua): preserve argument lists which are not lists --- runtime/lua/vim/F.lua | 9 +++++++++ src/nvim/lua/vim.lua | 4 ++-- test/functional/lua/vim_spec.lua | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/runtime/lua/vim/F.lua b/runtime/lua/vim/F.lua index 09467eb8c6..1a258546a5 100644 --- a/runtime/lua/vim/F.lua +++ b/runtime/lua/vim/F.lua @@ -27,5 +27,14 @@ function F.nil_wrap(fn) 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 diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index ed435439a4..801226ab7b 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -273,8 +273,8 @@ end ---@see |vim.in_fast_event()| function vim.schedule_wrap(cb) return (function (...) - local args = {...} - vim.schedule(function() cb(unpack(args)) end) + local args = vim.F.pack_len(...) + vim.schedule(function() cb(vim.F.unpack_len(args)) end) end) end diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 4e2bed4deb..0ea914880f 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -17,6 +17,7 @@ local matches = helpers.matches local source = helpers.source local NIL = helpers.NIL local retry = helpers.retry +local next_msg = helpers.next_msg before_each(clear) @@ -2178,6 +2179,24 @@ describe('lua stdlib', function() 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() it('can access buf options', function() local buf1 = meths.get_current_buf()