feat(lua): allow passing handles to vim.b/w/t

vim.bo can target a specific buffer by indexing with a number, e.g:
`vim.bo[2].filetype` can get/set the filetype for buffer 2. This change
replicates that behaviour for the variable namespace.
This commit is contained in:
Lewis Russell 2021-09-23 15:00:25 +01:00
parent 064411ea7f
commit 6c5e7bde9a
3 changed files with 44 additions and 8 deletions

View File

@ -928,6 +928,7 @@ Example: >
vim.g.foo = 5 -- Set the g:foo Vimscript variable.
print(vim.g.foo) -- Get and print the g:foo Vimscript variable.
vim.g.foo = nil -- Delete (:unlet) the Vimscript variable.
vim.b[2].foo = 6 -- Set b:foo for buffer 2
vim.g *vim.g*
Global (|g:|) editor variables.
@ -935,15 +936,18 @@ vim.g *vim.g*
vim.b *vim.b*
Buffer-scoped (|b:|) variables for the current buffer.
Invalid or unset key returns `nil`.
Invalid or unset key returns `nil`. Can be indexed with
an integer to access variables for a specific buffer.
vim.w *vim.w*
Window-scoped (|w:|) variables for the current window.
Invalid or unset key returns `nil`.
Invalid or unset key returns `nil`. Can be indexed with
an integer to access variables for a specific window.
vim.t *vim.t*
Tabpage-scoped (|t:|) variables for the current tabpage.
Invalid or unset key returns `nil`.
Invalid or unset key returns `nil`. Can be indexed with
an integer to access variables for a specific tabpage.
vim.v *vim.v*
|v:| variables.

View File

@ -323,22 +323,25 @@ end
do
local validate = vim.validate
local function make_dict_accessor(scope)
local function make_dict_accessor(scope, handle)
validate {
scope = {scope, 's'};
}
local mt = {}
function mt:__newindex(k, v)
return vim._setvar(scope, 0, k, v)
return vim._setvar(scope, handle or 0, k, v)
end
function mt:__index(k)
return vim._getvar(scope, 0, k)
if handle == nil and type(k) == 'number' then
return make_dict_accessor(scope, k)
end
return vim._getvar(scope, handle or 0, k)
end
return setmetatable({}, mt)
end
vim.g = make_dict_accessor('g')
vim.v = make_dict_accessor('v')
vim.g = make_dict_accessor('g', false)
vim.v = make_dict_accessor('v', false)
vim.b = make_dict_accessor('b')
vim.w = make_dict_accessor('w')
vim.t = make_dict_accessor('t')

View File

@ -996,6 +996,9 @@ describe('lua stdlib', function()
vim.g.to_delete = nil
]]
eq(NIL, funcs.luaeval "vim.g.to_delete")
matches([[^Error executing lua: .*: attempt to index .* nil value]],
pcall_err(exec_lua, 'return vim.g[0].testing'))
end)
it('vim.b', function()
@ -1005,18 +1008,25 @@ describe('lua stdlib', function()
vim.api.nvim_buf_set_var(0, "floaty", 5120.1)
vim.api.nvim_buf_set_var(0, "nullvar", vim.NIL)
vim.api.nvim_buf_set_var(0, "to_delete", {hello="world"})
BUF = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_var(BUF, "testing", "bye")
]]
eq('hi', funcs.luaeval "vim.b.testing")
eq('bye', funcs.luaeval "vim.b[BUF].testing")
eq(123, funcs.luaeval "vim.b.other")
eq(5120.1, funcs.luaeval "vim.b.floaty")
eq(NIL, funcs.luaeval "vim.b.nonexistant")
eq(NIL, funcs.luaeval "vim.b[BUF].nonexistant")
eq(NIL, funcs.luaeval "vim.b.nullvar")
-- lost over RPC, so test locally:
eq({false, true}, exec_lua [[
return {vim.b.nonexistant == vim.NIL, vim.b.nullvar == vim.NIL}
]])
matches([[^Error executing lua: .*: attempt to index .* nil value]],
pcall_err(exec_lua, 'return vim.b[BUF][0].testing'))
eq({hello="world"}, funcs.luaeval "vim.b.to_delete")
exec_lua [[
vim.b.to_delete = nil
@ -1037,11 +1047,22 @@ describe('lua stdlib', function()
vim.api.nvim_win_set_var(0, "testing", "hi")
vim.api.nvim_win_set_var(0, "other", 123)
vim.api.nvim_win_set_var(0, "to_delete", {hello="world"})
BUF = vim.api.nvim_create_buf(false, true)
WIN = vim.api.nvim_open_win(BUF, false, {
width=10, height=10,
relative='win', row=0, col=0
})
vim.api.nvim_win_set_var(WIN, "testing", "bye")
]]
eq('hi', funcs.luaeval "vim.w.testing")
eq('bye', funcs.luaeval "vim.w[WIN].testing")
eq(123, funcs.luaeval "vim.w.other")
eq(NIL, funcs.luaeval "vim.w.nonexistant")
eq(NIL, funcs.luaeval "vim.w[WIN].nonexistant")
matches([[^Error executing lua: .*: attempt to index .* nil value]],
pcall_err(exec_lua, 'return vim.w[WIN][0].testing'))
eq({hello="world"}, funcs.luaeval "vim.w.to_delete")
exec_lua [[
@ -1068,6 +1089,12 @@ describe('lua stdlib', function()
eq('hi', funcs.luaeval "vim.t.testing")
eq(123, funcs.luaeval "vim.t.other")
eq(NIL, funcs.luaeval "vim.t.nonexistant")
eq('hi', funcs.luaeval "vim.t[0].testing")
eq(123, funcs.luaeval "vim.t[0].other")
eq(NIL, funcs.luaeval "vim.t[0].nonexistant")
matches([[^Error executing lua: .*: attempt to index .* nil value]],
pcall_err(exec_lua, 'return vim.t[0][0].testing'))
eq({hello="world"}, funcs.luaeval "vim.t.to_delete")
exec_lua [[
@ -1096,6 +1123,8 @@ describe('lua stdlib', function()
eq(funcs.luaeval "vim.api.nvim_get_vvar('progpath')", funcs.luaeval "vim.v.progpath")
eq(false, funcs.luaeval "vim.v['false']")
eq(NIL, funcs.luaeval "vim.v.null")
matches([[^Error executing lua: .*: attempt to index .* nil value]],
pcall_err(exec_lua, 'return vim.v[0].progpath'))
end)
it('vim.bo', function()