mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	Merge pull request #15767 from lewis6991/lua_var_index
feat(lua): allow passing handles to `vim.b/w/t`
This commit is contained in:
		| @@ -928,6 +928,7 @@ Example: > | |||||||
|     vim.g.foo = 5     -- Set the g:foo Vimscript variable. |     vim.g.foo = 5     -- Set the g:foo Vimscript variable. | ||||||
|     print(vim.g.foo)  -- Get and print 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.g.foo = nil   -- Delete (:unlet) the Vimscript variable. | ||||||
|  |     vim.b[2].foo = 6  -- Set b:foo for buffer 2 | ||||||
|  |  | ||||||
| vim.g                                                   *vim.g* | vim.g                                                   *vim.g* | ||||||
|         Global (|g:|) editor variables. |         Global (|g:|) editor variables. | ||||||
| @@ -935,15 +936,18 @@ vim.g                                                   *vim.g* | |||||||
|  |  | ||||||
| vim.b                                                   *vim.b* | vim.b                                                   *vim.b* | ||||||
|         Buffer-scoped (|b:|) variables for the current buffer. |         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* | vim.w                                                   *vim.w* | ||||||
|         Window-scoped (|w:|) variables for the current window. |         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* | vim.t                                                   *vim.t* | ||||||
|         Tabpage-scoped (|t:|) variables for the current tabpage. |         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* | vim.v                                                   *vim.v* | ||||||
|         |v:| variables. |         |v:| variables. | ||||||
|   | |||||||
| @@ -323,22 +323,25 @@ end | |||||||
| do | do | ||||||
|   local validate = vim.validate |   local validate = vim.validate | ||||||
|  |  | ||||||
|   local function make_dict_accessor(scope) |   local function make_dict_accessor(scope, handle) | ||||||
|     validate { |     validate { | ||||||
|       scope = {scope, 's'}; |       scope = {scope, 's'}; | ||||||
|     } |     } | ||||||
|     local mt = {} |     local mt = {} | ||||||
|     function mt:__newindex(k, v) |     function mt:__newindex(k, v) | ||||||
|       return vim._setvar(scope, 0, k, v) |       return vim._setvar(scope, handle or 0, k, v) | ||||||
|     end |     end | ||||||
|     function mt:__index(k) |     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 |     end | ||||||
|     return setmetatable({}, mt) |     return setmetatable({}, mt) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   vim.g = make_dict_accessor('g') |   vim.g = make_dict_accessor('g', false) | ||||||
|   vim.v = make_dict_accessor('v') |   vim.v = make_dict_accessor('v', false) | ||||||
|   vim.b = make_dict_accessor('b') |   vim.b = make_dict_accessor('b') | ||||||
|   vim.w = make_dict_accessor('w') |   vim.w = make_dict_accessor('w') | ||||||
|   vim.t = make_dict_accessor('t') |   vim.t = make_dict_accessor('t') | ||||||
|   | |||||||
| @@ -996,6 +996,9 @@ describe('lua stdlib', function() | |||||||
|     vim.g.to_delete = nil |     vim.g.to_delete = nil | ||||||
|     ]] |     ]] | ||||||
|     eq(NIL, funcs.luaeval "vim.g.to_delete") |     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) |   end) | ||||||
|  |  | ||||||
|   it('vim.b', function() |   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, "floaty", 5120.1) | ||||||
|     vim.api.nvim_buf_set_var(0, "nullvar", vim.NIL) |     vim.api.nvim_buf_set_var(0, "nullvar", vim.NIL) | ||||||
|     vim.api.nvim_buf_set_var(0, "to_delete", {hello="world"}) |     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('hi', funcs.luaeval "vim.b.testing") | ||||||
|  |     eq('bye', funcs.luaeval "vim.b[BUF].testing") | ||||||
|     eq(123, funcs.luaeval "vim.b.other") |     eq(123, funcs.luaeval "vim.b.other") | ||||||
|     eq(5120.1, funcs.luaeval "vim.b.floaty") |     eq(5120.1, funcs.luaeval "vim.b.floaty") | ||||||
|     eq(NIL, funcs.luaeval "vim.b.nonexistant") |     eq(NIL, funcs.luaeval "vim.b.nonexistant") | ||||||
|  |     eq(NIL, funcs.luaeval "vim.b[BUF].nonexistant") | ||||||
|     eq(NIL, funcs.luaeval "vim.b.nullvar") |     eq(NIL, funcs.luaeval "vim.b.nullvar") | ||||||
|     -- lost over RPC, so test locally: |     -- lost over RPC, so test locally: | ||||||
|     eq({false, true}, exec_lua [[ |     eq({false, true}, exec_lua [[ | ||||||
|       return {vim.b.nonexistant == vim.NIL, vim.b.nullvar == vim.NIL} |       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") |     eq({hello="world"}, funcs.luaeval "vim.b.to_delete") | ||||||
|     exec_lua [[ |     exec_lua [[ | ||||||
|     vim.b.to_delete = nil |     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, "testing", "hi") | ||||||
|     vim.api.nvim_win_set_var(0, "other", 123) |     vim.api.nvim_win_set_var(0, "other", 123) | ||||||
|     vim.api.nvim_win_set_var(0, "to_delete", {hello="world"}) |     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('hi', funcs.luaeval "vim.w.testing") | ||||||
|  |     eq('bye', funcs.luaeval "vim.w[WIN].testing") | ||||||
|     eq(123, funcs.luaeval "vim.w.other") |     eq(123, funcs.luaeval "vim.w.other") | ||||||
|     eq(NIL, funcs.luaeval "vim.w.nonexistant") |     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") |     eq({hello="world"}, funcs.luaeval "vim.w.to_delete") | ||||||
|     exec_lua [[ |     exec_lua [[ | ||||||
| @@ -1068,6 +1089,12 @@ describe('lua stdlib', function() | |||||||
|     eq('hi', funcs.luaeval "vim.t.testing") |     eq('hi', funcs.luaeval "vim.t.testing") | ||||||
|     eq(123, funcs.luaeval "vim.t.other") |     eq(123, funcs.luaeval "vim.t.other") | ||||||
|     eq(NIL, funcs.luaeval "vim.t.nonexistant") |     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") |     eq({hello="world"}, funcs.luaeval "vim.t.to_delete") | ||||||
|     exec_lua [[ |     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(funcs.luaeval "vim.api.nvim_get_vvar('progpath')", funcs.luaeval "vim.v.progpath") | ||||||
|     eq(false, funcs.luaeval "vim.v['false']") |     eq(false, funcs.luaeval "vim.v['false']") | ||||||
|     eq(NIL, funcs.luaeval "vim.v.null") |     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) |   end) | ||||||
|  |  | ||||||
|   it('vim.bo', function() |   it('vim.bo', function() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Björn Linse
					Björn Linse