Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: nvim_ex_call_luaref #13280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/nvim/api/vim.c
Expand Up @@ -634,6 +634,62 @@ Object nvim_call_function(String fn, Array args, Error *err)
return _call_function(fn, args, NULL, err);
}

/// nvim_ex_call_function({noautocmd = true}, my_lua_cb, x, y, z)
///
/// local callable = vim.ex_wrap({autocmd = false}, my_lua_cb)
/// callable(x, y, z)
/// local wrapped = vim.
//
// nvim_ex_call_function({bufdo = {1, 3, 7, 8}}, my_lua_cb, ...)
Object nvim_ex_call_luaref(Dictionary opts, LuaRef cb, Array args, Error *err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can just be called nvim_ex_call or something without "lua" in the name.. The documentation will automatically indicate that it is "lua only" for now, and we can extend it later to rpc and/or vimscript.

FUNC_API_SINCE(7) FUNC_API_LUA_ONLY
{
bool do_autocmds = true;
bool silent = false;

// TODO(tjdevries): Use the cool macro I made in the other PR
for (size_t i = 0; i < opts.size; i++) {
String k = opts.items[i].key;
Object v = opts.items[i].value;

if (strequal("autocmd", k.data)) {
do_autocmds = api_object_to_bool(v, "autocmd", true, err);
} else if (strequal("silent", k.data)) {
silent = api_object_to_bool(v, "silent", false, err);
} else {
api_set_error(err, kErrorTypeValidation, "Invalid key: %s", k.data);
}
}

char_u *save_eventignore;
if (!do_autocmds) {
save_eventignore = vim_strsave(p_ei);
set_string_option_direct(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really how ex commands do it?

(char_u *)"eventignore", -1, (char_u *)"all", OPT_FREE, SID_NONE);
}

bool save_silent;
if (silent) {
save_silent = emsg_silent;
emsg_silent = true;
}

// Undefined behavior if you call vim.wait inside of here?...
Copy link
Member

@bfredl bfredl Nov 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why Undefined behavior? the same situation can already arise in vimscript with ex command modiifers

Object rv = nlua_call_ref(cb, NULL, args, true, err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self, add a flag to nlua_call_ref to preserve retval as a luaref (and also use it for nvim_buf_call.


if (!do_autocmds) {
set_string_option_direct(
(char_u *)"eventignore", -1, save_eventignore, OPT_FREE, SID_NONE);
free_string_option(save_eventignore);
}

if (silent) {
emsg_silent = save_silent;
}

return rv;
}

/// Calls a VimL |Dictionary-function| with the given arguments.
///
/// On execution error: fails with VimL error, does not update v:errmsg.
Expand Down
33 changes: 32 additions & 1 deletion test/functional/api/vim_spec.lua
Expand Up @@ -168,7 +168,7 @@ describe('API', function()
os.remove(fname)
end)

it('traceback', function()
pending('traceback', function()
local fname = tmpname()
write_file(fname, 'echo "hello"\n')
local sourcing_fname = tmpname()
Expand Down Expand Up @@ -1906,4 +1906,35 @@ describe('API', function()
or endswith(val[1], p("autoload/remote/host.vim")))
end)
end)

describe('nvim_ex_call_luaref', function()
it('should return values when no configuration is passed', function()
eq(true, meths.exec_lua("return vim.api.nvim_ex_call_luaref({}, function() return true end, {})", {}))
end)

it('should disable autocmds when passed', function()
eq(true, meths.exec_lua("return vim.api.nvim_ex_call_luaref({autocmd = false}, function() return true end, {})", {}))

eq(1, meths.exec_lua([[
vim.g.autocmd_count = 0
vim.cmd [=[autocmd FileType * let g:autocmd_count = g:autocmd_count + 1]=]

local autocmd_incrementer = function() vim.cmd [=[doautocmd FileType]=] end

vim.api.nvim_ex_call_luaref({autocmd = true}, autocmd_incrementer, {})
vim.api.nvim_ex_call_luaref({autocmd = false}, autocmd_incrementer, {})

return vim.g.autocmd_count
]], {}))
end)

it('should ignore errors when silent is passed', function()
eq({'Begin', nil}, meths.exec_lua([[
return {
vim.api.nvim_ex_call_luaref({silent = true}, function() return 'Begin' end, {}),
vim.api.nvim_ex_call_luaref({silent = true}, function() error("HELLO?") end, {})
}
]], {}))
end)
end)
end)