diff options
author | Lewis Russell <lewis6991@gmail.com> | 2023-07-22 09:52:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-22 09:52:13 +0100 |
commit | 24e3ee9d07e1433cb13b4d96ec20999f5f02b204 (patch) | |
tree | b8cd26fc0e9413939b547ba0b63be99551e5d422 /runtime/lua/vim/_options.lua | |
parent | cfcda91827d73dda740e372b237383c19e63dab9 (diff) | |
download | rneovim-24e3ee9d07e1433cb13b4d96ec20999f5f02b204.tar.gz rneovim-24e3ee9d07e1433cb13b4d96ec20999f5f02b204.tar.bz2 rneovim-24e3ee9d07e1433cb13b4d96ec20999f5f02b204.zip |
fix(api/options): validate buf and win
Fixes #24398
Diffstat (limited to 'runtime/lua/vim/_options.lua')
-rw-r--r-- | runtime/lua/vim/_options.lua | 36 |
1 files changed, 7 insertions, 29 deletions
diff --git a/runtime/lua/vim/_options.lua b/runtime/lua/vim/_options.lua index d54e8b447c..e1c125baf2 100644 --- a/runtime/lua/vim/_options.lua +++ b/runtime/lua/vim/_options.lua @@ -124,14 +124,12 @@ local function get_option_metatype(name, info) return info.type end -local options_info = setmetatable({}, { - __index = function(t, k) - local info = api.nvim_get_option_info(k) - info.metatype = get_option_metatype(k, info) - rawset(t, k, info) - return rawget(t, k) - end, -}) +--- @param name string +local function get_options_info(name) + local info = api.nvim_get_option_info(name) + info.metatype = get_option_metatype(name, info) + return info +end ---Environment variables defined in the editor session. ---See |expand-env| and |:let-environment| for the Vimscript behavior. @@ -155,34 +153,16 @@ vim.env = setmetatable({}, { end, }) -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 - ) - ) - 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 - opt_validate(k, 'buf') return api.nvim_get_option_value(k, { buf = bufnr or 0 }) end, __newindex = function(_, k, v) - opt_validate(k, 'buf') return api.nvim_set_option_value(k, v, { buf = bufnr or 0 }) end, }) @@ -203,7 +183,6 @@ local function new_win_opt_accessor(winid, bufnr) error('only bufnr=0 is supported') end - opt_validate(k, 'win') -- TODO(lewis6991): allow passing both buf and win to nvim_get_option_value return api.nvim_get_option_value(k, { scope = bufnr and 'local' or nil, @@ -212,7 +191,6 @@ local function new_win_opt_accessor(winid, bufnr) end, __newindex = function(_, k, v) - opt_validate(k, 'win') -- TODO(lewis6991): allow passing both buf and win to nvim_set_option_value return api.nvim_set_option_value(k, v, { scope = bufnr and 'local' or nil, @@ -680,7 +658,7 @@ local function create_option_accessor(scope) local option_mt local function make_option(name, value) - local info = assert(options_info[name], 'Not a valid option name: ' .. name) + local info = assert(get_options_info(name), 'Not a valid option name: ' .. name) if type(value) == 'table' and getmetatable(value) == option_mt then assert(name == value._name, "must be the same value, otherwise that's weird.") |