Merge pull request #13875 from smolck/vim_fn_error_on_api

vim.fn: throw error when trying to use API function
This commit is contained in:
Björn Linse
2021-03-09 23:12:23 +01:00
committed by GitHub
2 changed files with 14 additions and 2 deletions

View File

@@ -263,8 +263,15 @@ end
-- vim.fn.{func}(...) -- vim.fn.{func}(...)
vim.fn = setmetatable({}, { vim.fn = setmetatable({}, {
__index = function(t, key) __index = function(t, key)
local function _fn(...) local _fn
return vim.call(key, ...) if vim.api[key] ~= nil then
_fn = function()
error(string.format("Tried to call API function with vim.fn: use vim.api.%s instead", key))
end
else
_fn = function(...)
return vim.call(key, ...)
end
end end
t[key] = _fn t[key] = _fn
return _fn return _fn

View File

@@ -715,6 +715,11 @@ describe('lua stdlib', function()
eq({false, 'Vim:E714: List required'}, exec_lua([[return {pcall(vim.fn.add, "aa", "bb")}]])) eq({false, 'Vim:E714: List required'}, exec_lua([[return {pcall(vim.fn.add, "aa", "bb")}]]))
end) end)
it('vim.fn should error when calling API function', function()
eq('Error executing lua: vim.lua:0: Tried to call API function with vim.fn: use vim.api.nvim_get_current_line instead',
pcall_err(exec_lua, "vim.fn.nvim_get_current_line()"))
end)
it('vim.rpcrequest and vim.rpcnotify', function() it('vim.rpcrequest and vim.rpcnotify', function()
exec_lua([[ exec_lua([[
chan = vim.fn.jobstart({'cat'}, {rpc=true}) chan = vim.fn.jobstart({'cat'}, {rpc=true})