Skip to content

Commit e355cc8

Browse files
authored
Merge pull request neovim#13875 from smolck/vim_fn_error_on_api
vim.fn: throw error when trying to use API function
2 parents 7c204af + d1899bb commit e355cc8

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/nvim/lua/vim.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,15 @@ end
263263
-- vim.fn.{func}(...)
264264
vim.fn = setmetatable({}, {
265265
__index = function(t, key)
266-
local function _fn(...)
267-
return vim.call(key, ...)
266+
local _fn
267+
if vim.api[key] ~= nil then
268+
_fn = function()
269+
error(string.format("Tried to call API function with vim.fn: use vim.api.%s instead", key))
270+
end
271+
else
272+
_fn = function(...)
273+
return vim.call(key, ...)
274+
end
268275
end
269276
t[key] = _fn
270277
return _fn

test/functional/lua/vim_spec.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,11 @@ describe('lua stdlib', function()
715715
eq({false, 'Vim:E714: List required'}, exec_lua([[return {pcall(vim.fn.add, "aa", "bb")}]]))
716716
end)
717717

718+
it('vim.fn should error when calling API function', function()
719+
eq('Error executing lua: vim.lua:0: Tried to call API function with vim.fn: use vim.api.nvim_get_current_line instead',
720+
pcall_err(exec_lua, "vim.fn.nvim_get_current_line()"))
721+
end)
722+
718723
it('vim.rpcrequest and vim.rpcnotify', function()
719724
exec_lua([[
720725
chan = vim.fn.jobstart({'cat'}, {rpc=true})

0 commit comments

Comments
 (0)