From e65d0e53b1234f9173831a8a34c4e64d5542b0cb Mon Sep 17 00:00:00 2001 From: smolck <46855713+smolck@users.noreply.github.com> Date: Wed, 3 Feb 2021 18:13:04 -0600 Subject: [PATCH 1/5] vim.fn: throw error when trying to use API function --- src/nvim/lua/vim.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index dbf4f6014c..e154404836 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -253,9 +253,17 @@ end -- vim.fn.{func}(...) vim.fn = setmetatable({}, { __index = function(t, key) - local function _fn(...) - return vim.call(key, ...) + local _fn + 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 + t[key] = _fn return _fn end From 2d06538b5eb8513ad58a7d34eed8e38664627cd8 Mon Sep 17 00:00:00 2001 From: smolck <46855713+smolck@users.noreply.github.com> Date: Thu, 4 Feb 2021 08:27:38 -0600 Subject: [PATCH 2/5] remove extra line, remove () in error --- src/nvim/lua/vim.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index e154404836..da6c2c4678 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -256,14 +256,13 @@ vim.fn = setmetatable({}, { local _fn 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)) + 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 - t[key] = _fn return _fn end From ad1a437835dd78d668c2255892ee4dc3bfcebcfc Mon Sep 17 00:00:00 2001 From: smolck <46855713+smolck@users.noreply.github.com> Date: Thu, 4 Feb 2021 09:22:27 -0600 Subject: [PATCH 3/5] vim.fn: add test for error --- test/functional/lua/vim_spec.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index e253db5297..301dc503df 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -715,6 +715,11 @@ describe('lua stdlib', function() eq({false, 'Vim:E714: List required'}, exec_lua([[return {pcall(vim.fn.add, "aa", "bb")}]])) end) + it('vim.fn should error when calling API function', function() + eq({false, 'vim.lua:259: Tried to call API function with vim.fn: use vim.api.nvim_get_current_line instead'}, + exec_lua([[return {pcall(vim.fn.nvim_get_current_line)}]])) + end) + it('vim.rpcrequest and vim.rpcnotify', function() exec_lua([[ chan = vim.fn.jobstart({'cat'}, {rpc=true}) From 4526294848e5289a1f67cdb6c7e6fe5327f340fb Mon Sep 17 00:00:00 2001 From: smolck <46855713+smolck@users.noreply.github.com> Date: Sat, 6 Feb 2021 11:06:33 -0600 Subject: [PATCH 4/5] Fix unused vararg --- src/nvim/lua/vim.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index da6c2c4678..fd53bffe94 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -255,7 +255,7 @@ vim.fn = setmetatable({}, { __index = function(t, key) local _fn if vim.api[key] ~= nil then - _fn = function(...) + _fn = function() error(string.format("Tried to call API function with vim.fn: use vim.api.%s instead", key)) end else From d1899bb5f40d9f6f0e4426b6e87a44b69e38b29f Mon Sep 17 00:00:00 2001 From: smolck <46855713+smolck@users.noreply.github.com> Date: Tue, 9 Mar 2021 15:23:42 -0600 Subject: [PATCH 5/5] use pcall_err --- test/functional/lua/vim_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 301dc503df..9bf00b594b 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -716,8 +716,8 @@ describe('lua stdlib', function() end) it('vim.fn should error when calling API function', function() - eq({false, 'vim.lua:259: Tried to call API function with vim.fn: use vim.api.nvim_get_current_line instead'}, - exec_lua([[return {pcall(vim.fn.nvim_get_current_line)}]])) + 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()