mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge #9395 from pqzx/api-set-vvar
This commit is contained in:
commit
989fbad502
@ -184,35 +184,28 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del,
|
|||||||
bool retval, Error *err)
|
bool retval, Error *err)
|
||||||
{
|
{
|
||||||
Object rv = OBJECT_INIT;
|
Object rv = OBJECT_INIT;
|
||||||
|
|
||||||
if (dict->dv_lock) {
|
|
||||||
api_set_error(err, kErrorTypeException, "Dictionary is locked");
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key.size == 0) {
|
|
||||||
api_set_error(err, kErrorTypeValidation, "Key name is empty");
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key.size > INT_MAX) {
|
|
||||||
api_set_error(err, kErrorTypeValidation, "Key name is too long");
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
dictitem_T *di = tv_dict_find(dict, key.data, (ptrdiff_t)key.size);
|
dictitem_T *di = tv_dict_find(dict, key.data, (ptrdiff_t)key.size);
|
||||||
|
|
||||||
if (di != NULL) {
|
if (di != NULL) {
|
||||||
if (di->di_flags & DI_FLAGS_RO) {
|
if (di->di_flags & DI_FLAGS_RO) {
|
||||||
api_set_error(err, kErrorTypeException, "Key is read-only: %s", key.data);
|
api_set_error(err, kErrorTypeException, "Key is read-only: %s", key.data);
|
||||||
return rv;
|
return rv;
|
||||||
} else if (di->di_flags & DI_FLAGS_FIX) {
|
|
||||||
api_set_error(err, kErrorTypeException, "Key is fixed: %s", key.data);
|
|
||||||
return rv;
|
|
||||||
} else if (di->di_flags & DI_FLAGS_LOCK) {
|
} else if (di->di_flags & DI_FLAGS_LOCK) {
|
||||||
api_set_error(err, kErrorTypeException, "Key is locked: %s", key.data);
|
api_set_error(err, kErrorTypeException, "Key is locked: %s", key.data);
|
||||||
return rv;
|
return rv;
|
||||||
|
} else if (del && (di->di_flags & DI_FLAGS_FIX)) {
|
||||||
|
api_set_error(err, kErrorTypeException, "Key is fixed: %s", key.data);
|
||||||
|
return rv;
|
||||||
}
|
}
|
||||||
|
} else if (dict->dv_lock) {
|
||||||
|
api_set_error(err, kErrorTypeException, "Dictionary is locked");
|
||||||
|
return rv;
|
||||||
|
} else if (key.size == 0) {
|
||||||
|
api_set_error(err, kErrorTypeValidation, "Key name is empty");
|
||||||
|
return rv;
|
||||||
|
} else if (key.size > INT_MAX) {
|
||||||
|
api_set_error(err, kErrorTypeValidation, "Key name is too long");
|
||||||
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (del) {
|
if (del) {
|
||||||
|
@ -687,6 +687,17 @@ Object nvim_get_vvar(String name, Error *err)
|
|||||||
return dict_get_value(&vimvardict, name, err);
|
return dict_get_value(&vimvardict, name, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets a v: variable, if it is not readonly
|
||||||
|
///
|
||||||
|
/// @param name Variable name
|
||||||
|
/// @param value Variable value
|
||||||
|
/// @param[out] err Error details, if any
|
||||||
|
void nvim_set_vvar(String name, Object value, Error *err)
|
||||||
|
FUNC_API_SINCE(6)
|
||||||
|
{
|
||||||
|
dict_set_var(&vimvardict, name, value, false, false, err);
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets an option value string
|
/// Gets an option value string
|
||||||
///
|
///
|
||||||
/// @param name Option name
|
/// @param name Option name
|
||||||
|
@ -351,8 +351,8 @@ describe('API', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('nvim_get_var, nvim_set_var, nvim_del_var', function()
|
describe('set/get/del variables', function()
|
||||||
it('works', function()
|
it('nvim_get_var, nvim_set_var, nvim_del_var', function()
|
||||||
nvim('set_var', 'lua', {1, 2, {['3'] = 1}})
|
nvim('set_var', 'lua', {1, 2, {['3'] = 1}})
|
||||||
eq({1, 2, {['3'] = 1}}, nvim('get_var', 'lua'))
|
eq({1, 2, {['3'] = 1}}, nvim('get_var', 'lua'))
|
||||||
eq({1, 2, {['3'] = 1}}, nvim('eval', 'g:lua'))
|
eq({1, 2, {['3'] = 1}}, nvim('eval', 'g:lua'))
|
||||||
@ -361,11 +361,22 @@ describe('API', function()
|
|||||||
eq(0, funcs.exists('g:lua'))
|
eq(0, funcs.exists('g:lua'))
|
||||||
eq({false, "Key not found: lua"}, meth_pcall(meths.del_var, 'lua'))
|
eq({false, "Key not found: lua"}, meth_pcall(meths.del_var, 'lua'))
|
||||||
meths.set_var('lua', 1)
|
meths.set_var('lua', 1)
|
||||||
|
|
||||||
|
-- Set locked g: var.
|
||||||
command('lockvar lua')
|
command('lockvar lua')
|
||||||
eq({false, 'Key is locked: lua'}, meth_pcall(meths.del_var, 'lua'))
|
eq({false, 'Key is locked: lua'}, meth_pcall(meths.del_var, 'lua'))
|
||||||
eq({false, 'Key is locked: lua'}, meth_pcall(meths.set_var, 'lua', 1))
|
eq({false, 'Key is locked: lua'}, meth_pcall(meths.set_var, 'lua', 1))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('nvim_get_vvar, nvim_set_vvar', function()
|
||||||
|
-- Set readonly v: var.
|
||||||
|
expect_err('Key is read%-only: count$', request,
|
||||||
|
'nvim_set_vvar', 'count', 42)
|
||||||
|
-- Set writable v: var.
|
||||||
|
meths.set_vvar('errmsg', 'set by API')
|
||||||
|
eq('set by API', meths.get_vvar('errmsg'))
|
||||||
|
end)
|
||||||
|
|
||||||
it('vim_set_var returns the old value', function()
|
it('vim_set_var returns the old value', function()
|
||||||
local val1 = {1, 2, {['3'] = 1}}
|
local val1 = {1, 2, {['3'] = 1}}
|
||||||
local val2 = {4, 7}
|
local val2 = {4, 7}
|
||||||
|
Loading…
Reference in New Issue
Block a user