Merge pull request #11466 from bfredl/luaopt

lua: make vim.wo and vim.bo used nested indexing for specified handle
This commit is contained in:
Björn Linse 2019-11-26 21:08:16 +01:00 committed by GitHub
commit a40ae96a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 19 deletions

View File

@ -314,7 +314,7 @@ do
end end
vim.g = make_meta_accessor(nil_wrap(a.nvim_get_var), a.nvim_set_var, a.nvim_del_var) vim.g = make_meta_accessor(nil_wrap(a.nvim_get_var), a.nvim_set_var, a.nvim_del_var)
vim.v = make_meta_accessor(nil_wrap(a.nvim_get_vvar), a.nvim_set_vvar) vim.v = make_meta_accessor(nil_wrap(a.nvim_get_vvar), a.nvim_set_vvar)
vim.o = make_meta_accessor(nil_wrap(a.nvim_get_option), a.nvim_set_option) vim.o = make_meta_accessor(a.nvim_get_option, a.nvim_set_option)
vim.env = make_meta_accessor(vim.fn.getenv, vim.fn.setenv) vim.env = make_meta_accessor(vim.fn.getenv, vim.fn.setenv)
-- TODO(ashkan) if/when these are available from an API, generate them -- TODO(ashkan) if/when these are available from an API, generate them
-- instead of hardcoding. -- instead of hardcoding.
@ -344,29 +344,31 @@ do
if window_options[k] then if window_options[k] then
return a.nvim_err_writeln(k.." is a window option, not a buffer option") return a.nvim_err_writeln(k.." is a window option, not a buffer option")
end end
return a.nvim_buf_get_option(bufnr, k) if bufnr == nil and type(k) == "number" then
return new_buf_opt_accessor(k)
end
return a.nvim_buf_get_option(bufnr or 0, k)
end end
local function set(k, v) local function set(k, v)
if window_options[k] then if window_options[k] then
return a.nvim_err_writeln(k.." is a window option, not a buffer option") return a.nvim_err_writeln(k.." is a window option, not a buffer option")
end end
return a.nvim_buf_set_option(bufnr, k, v) return a.nvim_buf_set_option(bufnr or 0, k, v)
end end
return make_meta_accessor(nil_wrap(get), set) return make_meta_accessor(get, set)
end
vim.bo = new_buf_opt_accessor(0)
getmetatable(vim.bo).__call = function(_, bufnr)
return new_buf_opt_accessor(bufnr)
end end
vim.bo = new_buf_opt_accessor(nil)
local function new_win_opt_accessor(winnr) local function new_win_opt_accessor(winnr)
local function get(k) return a.nvim_win_get_option(winnr, k) end local function get(k)
local function set(k, v) return a.nvim_win_set_option(winnr, k, v) end if winnr == nil and type(k) == "number" then
return make_meta_accessor(nil_wrap(get), set) return new_win_opt_accessor(k)
end end
vim.wo = new_win_opt_accessor(0) return a.nvim_win_get_option(winnr or nil, k)
getmetatable(vim.wo).__call = function(_, winnr) end
return new_win_opt_accessor(winnr) local function set(k, v) return a.nvim_win_set_option(winnr or nil, k, v) end
return make_meta_accessor(get, set)
end end
vim.wo = new_win_opt_accessor(nil)
end end
return module return module

View File

@ -587,13 +587,17 @@ describe('lua stdlib', function()
]] ]]
eq(false, funcs.luaeval "vim.bo.modified") eq(false, funcs.luaeval "vim.bo.modified")
eq('markdown', funcs.luaeval "vim.bo.filetype") eq('markdown', funcs.luaeval "vim.bo.filetype")
eq(false, funcs.luaeval "vim.bo(BUF).modifiable") eq(false, funcs.luaeval "vim.bo[BUF].modifiable")
exec_lua [[ exec_lua [[
vim.bo.filetype = '' vim.bo.filetype = ''
vim.bo(BUF).modifiable = true vim.bo[BUF].modifiable = true
]] ]]
eq('', funcs.luaeval "vim.bo.filetype") eq('', funcs.luaeval "vim.bo.filetype")
eq(true, funcs.luaeval "vim.bo(BUF).modifiable") eq(true, funcs.luaeval "vim.bo[BUF].modifiable")
matches("^Error executing lua: .*: Invalid option name: 'nosuchopt'$",
pcall_err(exec_lua, 'return vim.bo.nosuchopt'))
matches("^Error executing lua: .*: Expected lua string$",
pcall_err(exec_lua, 'return vim.bo[0][0].autoread'))
end) end)
it('vim.wo', function() it('vim.wo', function()
@ -606,8 +610,12 @@ describe('lua stdlib', function()
eq(2, funcs.luaeval "vim.wo.cole") eq(2, funcs.luaeval "vim.wo.cole")
exec_lua [[ exec_lua [[
vim.wo.conceallevel = 0 vim.wo.conceallevel = 0
vim.bo(BUF).modifiable = true vim.bo[BUF].modifiable = true
]] ]]
eq(0, funcs.luaeval "vim.wo.cole") eq(0, funcs.luaeval "vim.wo.cole")
matches("^Error executing lua: .*: Invalid option name: 'notanopt'$",
pcall_err(exec_lua, 'return vim.wo.notanopt'))
matches("^Error executing lua: .*: Expected lua string$",
pcall_err(exec_lua, 'return vim.wo[0][0].list'))
end) end)
end) end)