mirror of
https://github.com/neovim/neovim.git
synced 2025-09-29 14:38:32 +00:00
api: Replace set_var(name, NIL) with del_var(name)
This commit is contained in:
@@ -423,7 +423,7 @@ Object buffer_get_var(Buffer buffer, String name, Error *err)
|
|||||||
return dict_get_value(buf->b_vars, name, err);
|
return dict_get_value(buf->b_vars, name, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a buffer-scoped (b:) variable. 'nil' value deletes the variable.
|
/// Sets a buffer-scoped (b:) variable
|
||||||
///
|
///
|
||||||
/// @param buffer The buffer handle
|
/// @param buffer The buffer handle
|
||||||
/// @param name The variable name
|
/// @param name The variable name
|
||||||
@@ -438,7 +438,24 @@ Object buffer_set_var(Buffer buffer, String name, Object value, Error *err)
|
|||||||
return (Object) OBJECT_INIT;
|
return (Object) OBJECT_INIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dict_set_value(buf->b_vars, name, value, err);
|
return dict_set_value(buf->b_vars, name, value, false, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes a buffer-scoped (b:) variable
|
||||||
|
///
|
||||||
|
/// @param buffer The buffer handle
|
||||||
|
/// @param name The variable name
|
||||||
|
/// @param[out] err Details of an error that may have occurred
|
||||||
|
/// @return The old value
|
||||||
|
Object buffer_del_var(Buffer buffer, String name, Error *err)
|
||||||
|
{
|
||||||
|
buf_T *buf = find_buffer_by_handle(buffer, err);
|
||||||
|
|
||||||
|
if (!buf) {
|
||||||
|
return (Object) OBJECT_INIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dict_set_value(buf->b_vars, name, NIL, true, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a buffer option value
|
/// Gets a buffer option value
|
||||||
|
@@ -90,14 +90,17 @@ Object dict_get_value(dict_T *dict, String key, Error *err)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set a value in a dict. Objects are recursively expanded into their
|
/// Set a value in a dict. Objects are recursively expanded into their
|
||||||
/// vimscript equivalents. Passing 'nil' as value deletes the key.
|
/// vimscript equivalents.
|
||||||
///
|
///
|
||||||
/// @param dict The vimscript dict
|
/// @param dict The vimscript dict
|
||||||
/// @param key The key
|
/// @param key The key
|
||||||
/// @param value The new value
|
/// @param value The new value
|
||||||
|
/// @param del Delete key in place of setting it. Argument `value` is ignored in
|
||||||
|
/// this case.
|
||||||
/// @param[out] err Details of an error that may have occurred
|
/// @param[out] err Details of an error that may have occurred
|
||||||
/// @return the old value, if any
|
/// @return the old value, if any
|
||||||
Object dict_set_value(dict_T *dict, String key, Object value, Error *err)
|
Object dict_set_value(dict_T *dict, String key, Object value, bool del,
|
||||||
|
Error *err)
|
||||||
{
|
{
|
||||||
Object rv = OBJECT_INIT;
|
Object rv = OBJECT_INIT;
|
||||||
|
|
||||||
@@ -118,7 +121,7 @@ Object dict_set_value(dict_T *dict, String key, Object value, Error *err)
|
|||||||
|
|
||||||
dictitem_T *di = dict_find(dict, (uint8_t *)key.data, (int)key.size);
|
dictitem_T *di = dict_find(dict, (uint8_t *)key.data, (int)key.size);
|
||||||
|
|
||||||
if (value.type == kObjectTypeNil) {
|
if (del) {
|
||||||
// Delete the key
|
// Delete the key
|
||||||
if (di == NULL) {
|
if (di == NULL) {
|
||||||
// Doesn't exist, fail
|
// Doesn't exist, fail
|
||||||
|
@@ -54,7 +54,7 @@ Object tabpage_get_var(Tabpage tabpage, String name, Error *err)
|
|||||||
return dict_get_value(tab->tp_vars, name, err);
|
return dict_get_value(tab->tp_vars, name, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a tab-scoped (t:) variable. 'nil' value deletes the variable.
|
/// Sets a tab-scoped (t:) variable
|
||||||
///
|
///
|
||||||
/// @param tabpage handle
|
/// @param tabpage handle
|
||||||
/// @param name The variable name
|
/// @param name The variable name
|
||||||
@@ -69,7 +69,24 @@ Object tabpage_set_var(Tabpage tabpage, String name, Object value, Error *err)
|
|||||||
return (Object) OBJECT_INIT;
|
return (Object) OBJECT_INIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dict_set_value(tab->tp_vars, name, value, err);
|
return dict_set_value(tab->tp_vars, name, value, false, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes a tab-scoped (t:) variable
|
||||||
|
///
|
||||||
|
/// @param tabpage handle
|
||||||
|
/// @param name The variable name
|
||||||
|
/// @param[out] err Details of an error that may have occurred
|
||||||
|
/// @return The tab page handle
|
||||||
|
Object tabpage_del_var(Tabpage tabpage, String name, Error *err)
|
||||||
|
{
|
||||||
|
tabpage_T *tab = find_tab_by_handle(tabpage, err);
|
||||||
|
|
||||||
|
if (!tab) {
|
||||||
|
return (Object) OBJECT_INIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dict_set_value(tab->tp_vars, name, NIL, true, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the current window in a tab page
|
/// Gets the current window in a tab page
|
||||||
|
@@ -331,7 +331,7 @@ Object vim_get_var(String name, Error *err)
|
|||||||
return dict_get_value(&globvardict, name, err);
|
return dict_get_value(&globvardict, name, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a global variable. Passing 'nil' as value deletes the variable.
|
/// Sets a global variable
|
||||||
///
|
///
|
||||||
/// @param name The variable name
|
/// @param name The variable name
|
||||||
/// @param value The variable value
|
/// @param value The variable value
|
||||||
@@ -339,7 +339,17 @@ Object vim_get_var(String name, Error *err)
|
|||||||
/// @return the old value if any
|
/// @return the old value if any
|
||||||
Object vim_set_var(String name, Object value, Error *err)
|
Object vim_set_var(String name, Object value, Error *err)
|
||||||
{
|
{
|
||||||
return dict_set_value(&globvardict, name, value, err);
|
return dict_set_value(&globvardict, name, value, false, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes a global variable
|
||||||
|
///
|
||||||
|
/// @param name The variable name
|
||||||
|
/// @param[out] err Details of an error that may have occurred
|
||||||
|
/// @return the old value if any
|
||||||
|
Object vim_del_var(String name, Error *err)
|
||||||
|
{
|
||||||
|
return dict_set_value(&globvardict, name, NIL, true, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a vim variable
|
/// Gets a vim variable
|
||||||
|
@@ -197,7 +197,7 @@ Object window_get_var(Window window, String name, Error *err)
|
|||||||
return dict_get_value(win->w_vars, name, err);
|
return dict_get_value(win->w_vars, name, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a window-scoped (w:) variable. 'nil' value deletes the variable.
|
/// Sets a window-scoped (w:) variable
|
||||||
///
|
///
|
||||||
/// @param window The window handle
|
/// @param window The window handle
|
||||||
/// @param name The variable name
|
/// @param name The variable name
|
||||||
@@ -212,7 +212,24 @@ Object window_set_var(Window window, String name, Object value, Error *err)
|
|||||||
return (Object) OBJECT_INIT;
|
return (Object) OBJECT_INIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dict_set_value(win->w_vars, name, value, err);
|
return dict_set_value(win->w_vars, name, value, false, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes a window-scoped (w:) variable
|
||||||
|
///
|
||||||
|
/// @param window The window handle
|
||||||
|
/// @param name The variable name
|
||||||
|
/// @param[out] err Details of an error that may have occurred
|
||||||
|
/// @return The old value
|
||||||
|
Object window_del_var(Window window, String name, Error *err)
|
||||||
|
{
|
||||||
|
win_T *win = find_window_by_handle(window, err);
|
||||||
|
|
||||||
|
if (!win) {
|
||||||
|
return (Object) OBJECT_INIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dict_set_value(win->w_vars, name, NIL, true, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a window option value
|
/// Gets a window option value
|
||||||
|
@@ -16028,9 +16028,9 @@ static void f_termopen(typval_T *argvars, typval_T *rettv)
|
|||||||
// Save the job id and pid in b:terminal_job_{id,pid}
|
// Save the job id and pid in b:terminal_job_{id,pid}
|
||||||
Error err;
|
Error err;
|
||||||
dict_set_value(curbuf->b_vars, cstr_as_string("terminal_job_id"),
|
dict_set_value(curbuf->b_vars, cstr_as_string("terminal_job_id"),
|
||||||
INTEGER_OBJ(rettv->vval.v_number), &err);
|
INTEGER_OBJ(rettv->vval.v_number), false, &err);
|
||||||
dict_set_value(curbuf->b_vars, cstr_as_string("terminal_job_pid"),
|
dict_set_value(curbuf->b_vars, cstr_as_string("terminal_job_pid"),
|
||||||
INTEGER_OBJ(pid), &err);
|
INTEGER_OBJ(pid), false, &err);
|
||||||
|
|
||||||
Terminal *term = terminal_open(topts);
|
Terminal *term = terminal_open(topts);
|
||||||
data->term = term;
|
data->term = term;
|
||||||
|
@@ -627,6 +627,7 @@ static int term_settermprop(VTermProp prop, VTermValue *val, void *data)
|
|||||||
api_free_object(dict_set_value(buf->b_vars,
|
api_free_object(dict_set_value(buf->b_vars,
|
||||||
cstr_as_string("term_title"),
|
cstr_as_string("term_title"),
|
||||||
STRING_OBJ(cstr_as_string(val->string)),
|
STRING_OBJ(cstr_as_string(val->string)),
|
||||||
|
false,
|
||||||
&err));
|
&err));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ local helpers = require('test.functional.helpers')
|
|||||||
local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer
|
local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer
|
||||||
local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq
|
local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq
|
||||||
local curbufmeths, ok = helpers.curbufmeths, helpers.ok
|
local curbufmeths, ok = helpers.curbufmeths, helpers.ok
|
||||||
|
local funcs = helpers.funcs
|
||||||
|
|
||||||
describe('buffer_* functions', function()
|
describe('buffer_* functions', function()
|
||||||
before_each(clear)
|
before_each(clear)
|
||||||
@@ -234,11 +235,14 @@ describe('buffer_* functions', function()
|
|||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('{get,set}_var', function()
|
describe('{get,set,del}_var', function()
|
||||||
it('works', function()
|
it('works', function()
|
||||||
curbuf('set_var', 'lua', {1, 2, {['3'] = 1}})
|
curbuf('set_var', 'lua', {1, 2, {['3'] = 1}})
|
||||||
eq({1, 2, {['3'] = 1}}, curbuf('get_var', 'lua'))
|
eq({1, 2, {['3'] = 1}}, curbuf('get_var', 'lua'))
|
||||||
eq({1, 2, {['3'] = 1}}, nvim('eval', 'b:lua'))
|
eq({1, 2, {['3'] = 1}}, nvim('eval', 'b:lua'))
|
||||||
|
eq(1, funcs.exists('b:lua'))
|
||||||
|
curbufmeths.del_var('lua')
|
||||||
|
eq(0, funcs.exists('b:lua'))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@@ -3,6 +3,9 @@ local helpers = require('test.functional.helpers')
|
|||||||
local clear, nvim, tabpage, curtab, eq, ok =
|
local clear, nvim, tabpage, curtab, eq, ok =
|
||||||
helpers.clear, helpers.nvim, helpers.tabpage, helpers.curtab, helpers.eq,
|
helpers.clear, helpers.nvim, helpers.tabpage, helpers.curtab, helpers.eq,
|
||||||
helpers.ok
|
helpers.ok
|
||||||
|
local wait = helpers.wait
|
||||||
|
local curtabmeths = helpers.curtabmeths
|
||||||
|
local funcs = helpers.funcs
|
||||||
|
|
||||||
describe('tabpage_* functions', function()
|
describe('tabpage_* functions', function()
|
||||||
before_each(clear)
|
before_each(clear)
|
||||||
@@ -21,11 +24,14 @@ describe('tabpage_* functions', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('{get,set}_var', function()
|
describe('{get,set,del}_var', function()
|
||||||
it('works', function()
|
it('works', function()
|
||||||
curtab('set_var', 'lua', {1, 2, {['3'] = 1}})
|
curtab('set_var', 'lua', {1, 2, {['3'] = 1}})
|
||||||
eq({1, 2, {['3'] = 1}}, curtab('get_var', 'lua'))
|
eq({1, 2, {['3'] = 1}}, curtab('get_var', 'lua'))
|
||||||
eq({1, 2, {['3'] = 1}}, nvim('eval', 't:lua'))
|
eq({1, 2, {['3'] = 1}}, nvim('eval', 't:lua'))
|
||||||
|
eq(1, funcs.exists('t:lua'))
|
||||||
|
curtabmeths.del_var('lua')
|
||||||
|
eq(0, funcs.exists('t:lua'))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@@ -4,6 +4,8 @@ local Screen = require('test.functional.ui.screen')
|
|||||||
local clear, nvim, eq, neq = helpers.clear, helpers.nvim, helpers.eq, helpers.neq
|
local clear, nvim, eq, neq = helpers.clear, helpers.nvim, helpers.eq, helpers.neq
|
||||||
local ok, nvim_async, feed = helpers.ok, helpers.nvim_async, helpers.feed
|
local ok, nvim_async, feed = helpers.ok, helpers.nvim_async, helpers.feed
|
||||||
local os_name = helpers.os_name
|
local os_name = helpers.os_name
|
||||||
|
local meths = helpers.meths
|
||||||
|
local funcs = helpers.funcs
|
||||||
|
|
||||||
describe('vim_* functions', function()
|
describe('vim_* functions', function()
|
||||||
before_each(clear)
|
before_each(clear)
|
||||||
@@ -70,11 +72,14 @@ describe('vim_* functions', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('{get,set}_var', function()
|
describe('{get,set,del}_var', function()
|
||||||
it('works', function()
|
it('works', 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'))
|
||||||
|
eq(1, funcs.exists('g:lua'))
|
||||||
|
meths.del_var('lua')
|
||||||
|
eq(0, funcs.exists('g:lua'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('set_var returns the old value', function()
|
it('set_var returns the old value', function()
|
||||||
@@ -84,6 +89,14 @@ describe('vim_* functions', function()
|
|||||||
eq(val1, nvim('set_var', 'lua', val2))
|
eq(val1, nvim('set_var', 'lua', val2))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('del_var returns the old value', function()
|
||||||
|
local val1 = {1, 2, {['3'] = 1}}
|
||||||
|
local val2 = {4, 7}
|
||||||
|
eq(nil, meths.set_var('lua', val1))
|
||||||
|
eq(val1, meths.set_var('lua', val2))
|
||||||
|
eq(val2, meths.del_var('lua'))
|
||||||
|
end)
|
||||||
|
|
||||||
it('truncates values with NULs in them', function()
|
it('truncates values with NULs in them', function()
|
||||||
nvim('set_var', 'xxx', 'ab\0cd')
|
nvim('set_var', 'xxx', 'ab\0cd')
|
||||||
eq('ab', nvim('get_var', 'xxx'))
|
eq('ab', nvim('get_var', 'xxx'))
|
||||||
|
@@ -5,6 +5,8 @@ local clear, nvim, curbuf, curbuf_contents, window, curwin, eq, neq,
|
|||||||
helpers.curbuf_contents, helpers.window, helpers.curwin, helpers.eq,
|
helpers.curbuf_contents, helpers.window, helpers.curwin, helpers.eq,
|
||||||
helpers.neq, helpers.ok, helpers.feed, helpers.insert, helpers.eval
|
helpers.neq, helpers.ok, helpers.feed, helpers.insert, helpers.eval
|
||||||
local wait = helpers.wait
|
local wait = helpers.wait
|
||||||
|
local curwinmeths = helpers.curwinmeths
|
||||||
|
local funcs = helpers.funcs
|
||||||
|
|
||||||
-- check if str is visible at the beginning of some line
|
-- check if str is visible at the beginning of some line
|
||||||
local function is_visible(str)
|
local function is_visible(str)
|
||||||
@@ -126,11 +128,14 @@ describe('window_* functions', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('{get,set}_var', function()
|
describe('{get,set,del}_var', function()
|
||||||
it('works', function()
|
it('works', function()
|
||||||
curwin('set_var', 'lua', {1, 2, {['3'] = 1}})
|
curwin('set_var', 'lua', {1, 2, {['3'] = 1}})
|
||||||
eq({1, 2, {['3'] = 1}}, curwin('get_var', 'lua'))
|
eq({1, 2, {['3'] = 1}}, curwin('get_var', 'lua'))
|
||||||
eq({1, 2, {['3'] = 1}}, nvim('eval', 'w:lua'))
|
eq({1, 2, {['3'] = 1}}, nvim('eval', 'w:lua'))
|
||||||
|
eq(1, funcs.exists('w:lua'))
|
||||||
|
curwinmeths.del_var('lua')
|
||||||
|
eq(0, funcs.exists('w:lua'))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user