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,59 +40,22 @@ 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
end
validate({
get = { get, 'f' },
set = { set, 'f' },
validator = { validator, 'f' },
})
local mt = {}
function mt:__newindex(k, v)
if not validator(k) then
return
end
return set(k, v)
end
function mt:__index(k)
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) local v = vim.fn.getenv(k)
if v == vim.NIL then if v == vim.NIL then
return nil return nil
end end
return v return v
end, vim.fn.setenv) end,
__newindex = function(_, k, v)
vim.fn.setenv(k, v)
end,
})
do -- buffer option accessor do -- buffer option accessor
local function new_buf_opt_accessor(bufnr) local function buf_opt_validate(k)
local function get(k)
if bufnr == nil and type(k) == 'number' then
return new_buf_opt_accessor(k)
end
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 local scope = options_info[k].scope
if scope == 'win' then if scope == 'win' then
error( error(
@@ -106,28 +68,30 @@ do -- buffer option accessor
end end
end end
return true local function new_buf_opt_accessor(bufnr)
end) 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 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 new_win_opt_accessor(winnr) local function win_opt_validate(k)
local function get(k)
if winnr == nil and type(k) == 'number' then
return new_win_opt_accessor(k)
end
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 local scope = options_info[k].scope
if scope == 'buf' then if scope == 'buf' then
error( error(
@@ -140,8 +104,21 @@ do -- window option accessor
end end
end end
return true local function new_win_opt_accessor(winnr)
end) 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 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({}, {
__index = function(_, k)
return a.nvim_get_option_value(k, { scope = 'global' }) return a.nvim_get_option_value(k, { scope = 'global' })
end, function(k, v) end,
__newindex = function(_, k, v)
return a.nvim_set_option_value(k, v, { scope = 'global' }) return a.nvim_set_option_value(k, v, { scope = 'global' })
end) 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({}, {
__index = function(_, k)
return a.nvim_get_option_value(k, {}) return a.nvim_get_option_value(k, {})
end, function(k, v) end,
__newindex = function(_, k, v)
return a.nvim_set_option_value(k, v, {}) return a.nvim_set_option_value(k, v, {})
end) end,
})
---@brief [[ ---@brief [[
--- vim.opt, vim.opt_local and vim.opt_global implementation --- vim.opt, vim.opt_local and vim.opt_global implementation