lua: add vim.fn.{func} for direct access to vimL function

compared to vim.api.|nvim_call_function|, this fixes some typing issues
due to the indirect conversion via the API. float values are preserved
as such (fixes #9389) as well as empty dicts/arrays.

Ref https://github.com/norcalli/nvim.lua for the call syntax
This commit is contained in:
Björn Linse
2019-08-25 22:01:35 +02:00
parent 19ba36d0e1
commit 8ee7c94a92
5 changed files with 130 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ local feed = helpers.feed
local pcall_err = helpers.pcall_err
local exec_lua = helpers.exec_lua
local matches = helpers.matches
local source = helpers.source
before_each(clear)
@@ -299,4 +300,31 @@ describe('lua stdlib', function()
eq("Error executing lua: .../shared.lua: Expected string, got number",
pcall_err(exec_lua, [[return vim.pesc(2)]]))
end)
it('vim.call and vim.fn', function()
eq(true, exec_lua([[return vim.call('sin', 0.0) == 0.0 ]]))
eq(true, exec_lua([[return vim.fn.sin(0.0) == 0.0 ]]))
-- compat: nvim_call_function uses "special" value for vimL float
eq(false, exec_lua([[return vim.api.nvim_call_function('sin', {0.0}) == 0.0 ]]))
source([[
func! FooFunc(test)
let g:test = a:test
return {}
endfunc
func! VarArg(...)
return a:000
endfunc
]])
eq(true, exec_lua([[return next(vim.fn.FooFunc(3)) == nil ]]))
eq(3, eval("g:test"))
-- compat: nvim_call_function uses "special" value for empty dict
eq(true, exec_lua([[return next(vim.api.nvim_call_function("FooFunc", {5})) == true ]]))
eq(5, eval("g:test"))
eq({2, "foo", true}, exec_lua([[return vim.fn.VarArg(2, "foo", true)]]))
-- error handling
eq({false, 'Vim:E714: List required'}, exec_lua([[return {pcall(vim.fn.add, "aa", "bb")}]]))
end)
end)