diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-09-08 15:56:35 +0100 |
---|---|---|
committer | Lewis Russell <lewis6991@gmail.com> | 2022-09-09 09:54:53 +0100 |
commit | 7533ceec13a1dd9a1e46a523975bddf52f533a93 (patch) | |
tree | b78bd810101d5060fb8d30b480508a5273a6409f /runtime/lua/vim/_meta.lua | |
parent | 164752b38074e9d71493c304dc3b8c672aac397d (diff) | |
download | rneovim-7533ceec13a1dd9a1e46a523975bddf52f533a93.tar.gz rneovim-7533ceec13a1dd9a1e46a523975bddf52f533a93.tar.bz2 rneovim-7533ceec13a1dd9a1e46a523975bddf52f533a93.zip |
refactor(vim.opt): unify vim.bo/wo building
Diffstat (limited to 'runtime/lua/vim/_meta.lua')
-rw-r--r-- | runtime/lua/vim/_meta.lua | 92 |
1 files changed, 29 insertions, 63 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({}, { |