mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #17375 from shadmansaleh/fix/vim.g/autoload
fix: autoload variables not loaded with vim.g & nvim_get_var
This commit is contained in:
commit
f378df846c
@ -601,7 +601,19 @@ void nvim_del_current_line(Error *err)
|
|||||||
Object nvim_get_var(String name, Error *err)
|
Object nvim_get_var(String name, Error *err)
|
||||||
FUNC_API_SINCE(1)
|
FUNC_API_SINCE(1)
|
||||||
{
|
{
|
||||||
return dict_get_value(&globvardict, name, err);
|
dictitem_T *di = tv_dict_find(&globvardict, name.data, (ptrdiff_t)name.size);
|
||||||
|
if (di == NULL) { // try to autoload script
|
||||||
|
if (!script_autoload(name.data, name.size, false) || aborting()) {
|
||||||
|
api_set_error(err, kErrorTypeValidation, "Key not found: %s", name.data);
|
||||||
|
return (Object)OBJECT_INIT;
|
||||||
|
}
|
||||||
|
di = tv_dict_find(&globvardict, name.data, (ptrdiff_t)name.size);
|
||||||
|
}
|
||||||
|
if (di == NULL) {
|
||||||
|
api_set_error(err, kErrorTypeValidation, "Key not found: %s", name.data);
|
||||||
|
return (Object)OBJECT_INIT;
|
||||||
|
}
|
||||||
|
return vim_to_object(&di->di_tv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a global (g:) variable.
|
/// Sets a global (g:) variable.
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include "nvim/func_attr.h"
|
#include "nvim/func_attr.h"
|
||||||
#include "nvim/garray.h"
|
#include "nvim/garray.h"
|
||||||
#include "nvim/getchar.h"
|
#include "nvim/getchar.h"
|
||||||
|
#include "nvim/globals.h"
|
||||||
#include "nvim/lua/converter.h"
|
#include "nvim/lua/converter.h"
|
||||||
#include "nvim/lua/executor.h"
|
#include "nvim/lua/executor.h"
|
||||||
#include "nvim/lua/stdlib.h"
|
#include "nvim/lua/stdlib.h"
|
||||||
@ -408,6 +409,12 @@ int nlua_getvar(lua_State *lstate)
|
|||||||
const char *name = luaL_checklstring(lstate, 3, &len);
|
const char *name = luaL_checklstring(lstate, 3, &len);
|
||||||
|
|
||||||
dictitem_T *di = tv_dict_find(dict, name, (ptrdiff_t)len);
|
dictitem_T *di = tv_dict_find(dict, name, (ptrdiff_t)len);
|
||||||
|
if (di == NULL && dict == &globvardict) { // try to autoload script
|
||||||
|
if (!script_autoload(name, len, false) || aborting()) {
|
||||||
|
return 0; // nil
|
||||||
|
}
|
||||||
|
di = tv_dict_find(dict, name, (ptrdiff_t)len);
|
||||||
|
}
|
||||||
if (di == NULL) {
|
if (di == NULL) {
|
||||||
return 0; // nil
|
return 0; // nil
|
||||||
}
|
}
|
||||||
|
@ -898,6 +898,19 @@ describe('API', function()
|
|||||||
command('lockvar lua')
|
command('lockvar lua')
|
||||||
eq('Key is locked: lua', pcall_err(meths.del_var, 'lua'))
|
eq('Key is locked: lua', pcall_err(meths.del_var, 'lua'))
|
||||||
eq('Key is locked: lua', pcall_err(meths.set_var, 'lua', 1))
|
eq('Key is locked: lua', pcall_err(meths.set_var, 'lua', 1))
|
||||||
|
|
||||||
|
-- Check if autoload works properly
|
||||||
|
local pathsep = helpers.get_pathsep()
|
||||||
|
local xconfig = 'Xhome' .. pathsep .. 'Xconfig'
|
||||||
|
local xdata = 'Xhome' .. pathsep .. 'Xdata'
|
||||||
|
local autoload_folder = table.concat({xconfig, 'nvim', 'autoload'}, pathsep)
|
||||||
|
local autoload_file = table.concat({autoload_folder , 'testload.vim'}, pathsep)
|
||||||
|
mkdir_p(autoload_folder)
|
||||||
|
write_file(autoload_file , [[let testload#value = 2]])
|
||||||
|
|
||||||
|
clear{ args_rm={'-u'}, env={ XDG_CONFIG_HOME=xconfig, XDG_DATA_HOME=xdata } }
|
||||||
|
eq(2, meths.get_var('testload#value'))
|
||||||
|
rmdir('Xhome')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('nvim_get_vvar, nvim_set_vvar', function()
|
it('nvim_get_vvar, nvim_set_vvar', function()
|
||||||
|
@ -19,6 +19,9 @@ local NIL = helpers.NIL
|
|||||||
local retry = helpers.retry
|
local retry = helpers.retry
|
||||||
local next_msg = helpers.next_msg
|
local next_msg = helpers.next_msg
|
||||||
local remove_trace = helpers.remove_trace
|
local remove_trace = helpers.remove_trace
|
||||||
|
local mkdir_p = helpers.mkdir_p
|
||||||
|
local rmdir = helpers.rmdir
|
||||||
|
local write_file = helpers.write_file
|
||||||
|
|
||||||
before_each(clear)
|
before_each(clear)
|
||||||
|
|
||||||
@ -1019,6 +1022,20 @@ describe('lua stdlib', function()
|
|||||||
eq(3, exec_lua([[return vim.g.GetCounter()]]))
|
eq(3, exec_lua([[return vim.g.GetCounter()]]))
|
||||||
exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
|
exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
|
||||||
eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
|
eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
|
||||||
|
|
||||||
|
-- Check if autoload works properly
|
||||||
|
local pathsep = helpers.get_pathsep()
|
||||||
|
local xconfig = 'Xhome' .. pathsep .. 'Xconfig'
|
||||||
|
local xdata = 'Xhome' .. pathsep .. 'Xdata'
|
||||||
|
local autoload_folder = table.concat({xconfig, 'nvim', 'autoload'}, pathsep)
|
||||||
|
local autoload_file = table.concat({autoload_folder , 'testload.vim'}, pathsep)
|
||||||
|
mkdir_p(autoload_folder)
|
||||||
|
write_file(autoload_file , [[let testload#value = 2]])
|
||||||
|
|
||||||
|
clear{ args_rm={'-u'}, env={ XDG_CONFIG_HOME=xconfig, XDG_DATA_HOME=xdata } }
|
||||||
|
|
||||||
|
eq(2, exec_lua("return vim.g['testload#value']"))
|
||||||
|
rmdir('Xhome')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('vim.b', function()
|
it('vim.b', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user