fix(api): set script context when setting usercmd or option (#22624)

This commit is contained in:
zeertzjq 2023-03-11 21:09:11 +08:00 committed by GitHub
parent 8065fc9aae
commit 6d0c61d90d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 16 deletions

View File

@ -938,10 +938,11 @@ static void build_cmdline_str(char **cmdlinep, exarg_T *eap, CmdParseInfo *cmdin
/// - force: (boolean, default true) Override any previous definition.
/// - preview: (function) Preview callback for 'inccommand' |:command-preview|
/// @param[out] err Error details, if any.
void nvim_create_user_command(String name, Object command, Dict(user_command) *opts, Error *err)
void nvim_create_user_command(uint64_t channel_id, String name, Object command,
Dict(user_command) *opts, Error *err)
FUNC_API_SINCE(9)
{
create_user_command(name, command, opts, 0, err);
create_user_command(channel_id, name, command, opts, 0, err);
}
/// Delete a user-defined command.
@ -959,7 +960,7 @@ void nvim_del_user_command(String name, Error *err)
/// @param buffer Buffer handle, or 0 for current buffer.
/// @param[out] err Error details, if any.
/// @see nvim_create_user_command
void nvim_buf_create_user_command(Buffer buffer, String name, Object command,
void nvim_buf_create_user_command(uint64_t channel_id, Buffer buffer, String name, Object command,
Dict(user_command) *opts, Error *err)
FUNC_API_SINCE(9)
{
@ -970,7 +971,7 @@ void nvim_buf_create_user_command(Buffer buffer, String name, Object command,
buf_T *save_curbuf = curbuf;
curbuf = target_buf;
create_user_command(name, command, opts, UC_BUFFER, err);
create_user_command(channel_id, name, command, opts, UC_BUFFER, err);
curbuf = save_curbuf;
}
@ -1011,8 +1012,8 @@ void nvim_buf_del_user_command(Buffer buffer, String name, Error *err)
api_set_error(err, kErrorTypeException, "Invalid command (not found): %s", name.data);
}
void create_user_command(String name, Object command, Dict(user_command) *opts, int flags,
Error *err)
void create_user_command(uint64_t channel_id, String name, Object command, Dict(user_command) *opts,
int flags, Error *err)
{
uint32_t argt = 0;
int64_t def = -1;
@ -1205,11 +1206,13 @@ void create_user_command(String name, Object command, Dict(user_command) *opts,
});
}
if (uc_add_command(name.data, name.size, rep, argt, def, flags, compl, compl_arg, compl_luaref,
preview_luaref, addr_type_arg, luaref, force) != OK) {
api_set_error(err, kErrorTypeException, "Failed to create user command");
// Do not goto err, since uc_add_command now owns luaref, compl_luaref, and compl_arg
}
WITH_SCRIPT_CONTEXT(channel_id, {
if (uc_add_command(name.data, name.size, rep, argt, def, flags, compl, compl_arg, compl_luaref,
preview_luaref, addr_type_arg, luaref, force) != OK) {
api_set_error(err, kErrorTypeException, "Failed to create user command");
// Do not goto err, since uc_add_command now owns luaref, compl_luaref, and compl_arg
}
});
return;

View File

@ -157,7 +157,8 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
/// - win: |window-ID|. Used for setting window local option.
/// - buf: Buffer number. Used for setting buffer local option.
/// @param[out] err Error details, if any
void nvim_set_option_value(String name, Object value, Dict(option) *opts, Error *err)
void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(option) *opts,
Error *err)
FUNC_API_SINCE(9)
{
int scope = 0;
@ -202,7 +203,9 @@ void nvim_set_option_value(String name, Object value, Dict(option) *opts, Error
});
}
access_option_value_for(name.data, &numval, &stringval, scope, opt_type, to, false, err);
WITH_SCRIPT_CONTEXT(channel_id, {
access_option_value_for(name.data, &numval, &stringval, scope, opt_type, to, false, err);
});
}
/// Gets the option information for all options.

View File

@ -2832,6 +2832,24 @@ describe('API', function()
type = "boolean",
was_set = true
}, meths.get_option_info'showcmd')
meths.set_option_value('showcmd', true, {})
eq({
allows_duplicates = true,
commalist = false,
default = true,
flaglist = false,
global_local = false,
last_set_chan = 1,
last_set_linenr = 0,
last_set_sid = -9,
name = "showcmd",
scope = "global",
shortname = "sc",
type = "boolean",
was_set = true
}, meths.get_option_info'showcmd')
end)
end)

View File

@ -7,7 +7,7 @@ local exec_capture = helpers.exec_capture
local write_file = helpers.write_file
local call_viml_function = helpers.meths.call_function
describe('lua :verbose', function()
local function last_set_tests(cmd)
local script_location, script_file
-- All test cases below use the same nvim instance.
setup(function()
@ -46,7 +46,7 @@ endfunction\
let &tw = s:return80()\
", true)
]])
exec(':source '..script_file)
exec(cmd .. ' ' .. script_file)
end)
teardown(function()
@ -106,6 +106,9 @@ test_group FileType
end)
it('"Last set" for command defined by nvim_command', function()
if cmd == 'luafile' then
pending('nvim_command does not set the script context')
end
local result = exec_capture(':verbose command Bdelete')
eq(string.format([[
Name Args Address Complete Definition
@ -123,7 +126,7 @@ test_group FileType
script_location), result)
end)
it('"Last set for function', function()
it('"Last set" for function', function()
local result = exec_capture(':verbose function Close_Window')
eq(string.format([[
function Close_Window() abort
@ -140,6 +143,14 @@ test_group FileType
Last set from %s line 22]],
script_location), result)
end)
end
describe('lua :verbose when using :source', function()
last_set_tests('source')
end)
describe('lua :verbose when using :luafile', function()
last_set_tests('luafile')
end)
describe('lua verbose:', function()