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

@@ -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()