diff options
-rw-r--r-- | runtime/lua/vim/_meta.lua | 92 | ||||
-rw-r--r-- | test/functional/lua/vim_spec.lua | 4 |
2 files changed, 31 insertions, 65 deletions
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 93077c89d6..f78c2da581 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -54,76 +54,42 @@ vim.env = setmetatable({}, { end, }) -do -- buffer option accessor - local function buf_opt_validate(k) - local scope = options_info[k].scope - if scope == 'win' then - error( - string.format([['%s' is a window option, not a buffer option. See ":help %s"]], k, k) +local function opt_validate(option_name, target_scope) + local scope = options_info[option_name].scope + if scope ~= target_scope then + local scope_to_string = { buf = 'buffer', win = 'window' } + error( + string.format( + [['%s' is a %s option, not a %s option. See ":help %s"]], + option_name, + scope_to_string[scope] or scope, + scope_to_string[target_scope] or target_scope, + option_name ) - elseif scope == 'global' then - error( - string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k) - ) - end - end - - local function new_buf_opt_accessor(bufnr) - return setmetatable({},{ - __index = function(_, k) - if bufnr == nil and type(k) == 'number' then - return new_buf_opt_accessor(k) - end - buf_opt_validate(k) - - return a.nvim_get_option_value(k, { buf = bufnr or 0 }) - end, - - __newindex = function(_, k, v) - buf_opt_validate(k) - return a.nvim_set_option_value(k, v, { buf = bufnr or 0 }) - end, - }) - + ) end - - vim.bo = new_buf_opt_accessor(nil) end -do -- window option accessor - local function win_opt_validate(k) - local scope = options_info[k].scope - if scope == 'buf' then - error( - string.format([['%s' is a buffer option, not a window option. See ":help %s"]], k, k) - ) - elseif scope == 'global' then - error( - string.format([['%s' is a global option, not a window option. See ":help %s"]], k, k) - ) - end - end - - local function new_win_opt_accessor(winnr) - return setmetatable({}, { - __index = function(_, k) - if winnr == nil and type(k) == 'number' then - return new_win_opt_accessor(k) - end - win_opt_validate(k) - return a.nvim_get_option_value(k, { win = winnr or 0 }) - end, - - __newindex = function(_, k, v) - win_opt_validate(k) - return a.nvim_set_option_value(k, v, { win = winnr or 0 }) - end, - }) - end +local function new_opt_accessor(handle, scope) + return setmetatable({}, { + __index = function(_, k) + if handle == nil and type(k) == 'number' then + return new_opt_accessor(k, scope) + end + opt_validate(k, scope) + return a.nvim_get_option_value(k, { [scope] = handle or 0 }) + end, - vim.wo = new_win_opt_accessor(nil) + __newindex = function(_, k, v) + opt_validate(k, scope) + return a.nvim_set_option_value(k, v, { [scope] = handle or 0 }) + end, + }) end +vim.bo = new_opt_accessor(nil, 'buf') +vim.wo = new_opt_accessor(nil, 'win') + -- vim global option -- this ONLY sets the global option. like `setglobal` vim.go = setmetatable({}, { diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index cd3240cd30..2466e9cc31 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -1396,7 +1396,7 @@ describe('lua stdlib', function() ]] eq('', funcs.luaeval "vim.bo.filetype") eq(true, funcs.luaeval "vim.bo[BUF].modifiable") - matches("unknown option 'nosuchopt'$", + matches("no such option: 'nosuchopt'$", pcall_err(exec_lua, 'return vim.bo.nosuchopt')) matches("Expected lua string$", pcall_err(exec_lua, 'return vim.bo[0][0].autoread')) @@ -1417,7 +1417,7 @@ describe('lua stdlib', function() eq(0, funcs.luaeval "vim.wo.cole") eq(0, funcs.luaeval "vim.wo[0].cole") eq(0, funcs.luaeval "vim.wo[1001].cole") - matches("unknown option 'notanopt'$", + matches("no such option: 'notanopt'$", pcall_err(exec_lua, 'return vim.wo.notanopt')) matches("Expected lua string$", pcall_err(exec_lua, 'return vim.wo[0][0].list')) |