refactor(vim.opt): remove make_meta_accessor()

This commit is contained in:
Lewis Russell
2022-09-08 15:52:39 +01:00
parent 514a1679dc
commit 164752b380

View File

@@ -2,7 +2,6 @@
local vim = assert(vim) local vim = assert(vim)
local a = vim.api local a = vim.api
local validate = vim.validate
-- TODO(tjdevries): Improve option metadata so that this doesn't have to be hardcoded. -- TODO(tjdevries): Improve option metadata so that this doesn't have to be hardcoded.
-- Can be done in a separate PR. -- Can be done in a separate PR.
@@ -41,107 +40,85 @@ local options_info = setmetatable({}, {
end, end,
}) })
local function make_meta_accessor(get, set, validator) vim.env = setmetatable({}, {
validator = validator or function() __index = function(_, k)
return true local v = vim.fn.getenv(k)
end if v == vim.NIL then
return nil
validate({
get = { get, 'f' },
set = { set, 'f' },
validator = { validator, 'f' },
})
local mt = {}
function mt:__newindex(k, v)
if not validator(k) then
return
end end
return v
end,
return set(k, v) __newindex = function(_, k, v)
end vim.fn.setenv(k, v)
function mt:__index(k) end,
if not validator(k) then })
return
end
return get(k)
end
return setmetatable({}, mt)
end
vim.env = make_meta_accessor(function(k)
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
end
return v
end, vim.fn.setenv)
do -- buffer option accessor 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)
)
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) local function new_buf_opt_accessor(bufnr)
local function get(k) return setmetatable({},{
if bufnr == nil and type(k) == 'number' then __index = function(_, k)
return new_buf_opt_accessor(k) if bufnr == nil and type(k) == 'number' then
end return new_buf_opt_accessor(k)
return a.nvim_get_option_value(k, { buf = bufnr or 0 })
end
local function set(k, v)
return a.nvim_set_option_value(k, v, { buf = bufnr or 0 })
end
return make_meta_accessor(get, set, function(k)
if type(k) == 'string' then
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)
)
elseif scope == 'global' then
error(
string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k)
)
end end
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,
})
return true
end)
end end
vim.bo = new_buf_opt_accessor(nil) vim.bo = new_buf_opt_accessor(nil)
end end
do -- window option accessor 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) local function new_win_opt_accessor(winnr)
local function get(k) return setmetatable({}, {
if winnr == nil and type(k) == 'number' then __index = function(_, k)
return new_win_opt_accessor(k) if winnr == nil and type(k) == 'number' then
end return new_win_opt_accessor(k)
return a.nvim_get_option_value(k, { win = winnr or 0 })
end
local function set(k, v)
return a.nvim_set_option_value(k, v, { win = winnr or 0 })
end
return make_meta_accessor(get, set, function(k)
if type(k) == 'string' then
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
end win_opt_validate(k)
return a.nvim_get_option_value(k, { win = winnr or 0 })
end,
return true __newindex = function(_, k, v)
end) win_opt_validate(k)
return a.nvim_set_option_value(k, v, { win = winnr or 0 })
end,
})
end end
vim.wo = new_win_opt_accessor(nil) vim.wo = new_win_opt_accessor(nil)
@@ -149,19 +126,25 @@ end
-- vim global option -- vim global option
-- this ONLY sets the global option. like `setglobal` -- this ONLY sets the global option. like `setglobal`
vim.go = make_meta_accessor(function(k) vim.go = setmetatable({}, {
return a.nvim_get_option_value(k, { scope = 'global' }) __index = function(_, k)
end, function(k, v) return a.nvim_get_option_value(k, { scope = 'global' })
return a.nvim_set_option_value(k, v, { scope = 'global' }) end,
end) __newindex = function(_, k, v)
return a.nvim_set_option_value(k, v, { scope = 'global' })
end,
})
-- vim `set` style options. -- vim `set` style options.
-- it has no additional metamethod magic. -- it has no additional metamethod magic.
vim.o = make_meta_accessor(function(k) vim.o = setmetatable({}, {
return a.nvim_get_option_value(k, {}) __index = function(_, k)
end, function(k, v) return a.nvim_get_option_value(k, {})
return a.nvim_set_option_value(k, v, {}) end,
end) __newindex = function(_, k, v)
return a.nvim_set_option_value(k, v, {})
end,
})
---@brief [[ ---@brief [[
--- vim.opt, vim.opt_local and vim.opt_global implementation --- vim.opt, vim.opt_local and vim.opt_global implementation