Merge pull request #20103 from lewis6991/refactor/vim_opt

This commit is contained in:
Lewis Russell
2022-09-22 13:59:04 +01:00
committed by GitHub
2 changed files with 412 additions and 589 deletions

View File

@@ -2,197 +2,6 @@
local vim = assert(vim) local vim = assert(vim)
local a = vim.api local a = vim.api
local validate = vim.validate
local SET_TYPES = setmetatable({
SET = 0,
LOCAL = 1,
GLOBAL = 2,
}, { __index = error })
local options_info = nil
local buf_options = nil
local glb_options = nil
local win_options = nil
local function _setup()
if options_info ~= nil then
return
end
options_info = {}
for _, v in pairs(a.nvim_get_all_options_info()) do
options_info[v.name] = v
if v.shortname ~= '' then
options_info[v.shortname] = v
end
end
local function get_scoped_options(scope)
local result = {}
for name, option_info in pairs(options_info) do
if option_info.scope == scope then
result[name] = true
end
end
return result
end
buf_options = get_scoped_options('buf')
glb_options = get_scoped_options('global')
win_options = get_scoped_options('win')
end
local function make_meta_accessor(get, set, del, validator)
validator = validator or function()
return true
end
validate({
get = { get, 'f' },
set = { set, 'f' },
del = { del, 'f', true },
validator = { validator, 'f' },
})
local mt = {}
function mt:__newindex(k, v)
if not validator(k) then
return
end
if del and v == nil then
return del(k)
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)
if v == vim.NIL then
return nil
end
return v
end, vim.fn.setenv)
do -- buffer option accessor
local function new_buf_opt_accessor(bufnr)
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, nil, function(k)
if type(k) == 'string' then
_setup()
if win_options[k] then
error(
string.format([['%s' is a window option, not a buffer option. See ":help %s"]], k, k)
)
elseif glb_options[k] then
error(
string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k)
)
end
end
return true
end)
end
vim.bo = new_buf_opt_accessor(nil)
end
do -- window option accessor
local function new_win_opt_accessor(winnr)
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, nil, function(k)
if type(k) == 'string' then
_setup()
if buf_options[k] then
error(
string.format([['%s' is a buffer option, not a window option. See ":help %s"]], k, k)
)
elseif glb_options[k] then
error(
string.format([['%s' is a global option, not a window option. See ":help %s"]], k, k)
)
end
end
return true
end)
end
vim.wo = new_win_opt_accessor(nil)
end
-- vim global option
-- this ONLY sets the global option. like `setglobal`
vim.go = make_meta_accessor(function(k)
return a.nvim_get_option_value(k, { scope = 'global' })
end, function(k, v)
return a.nvim_set_option_value(k, v, { scope = 'global' })
end)
-- vim `set` style options.
-- it has no additional metamethod magic.
vim.o = make_meta_accessor(function(k)
return a.nvim_get_option_value(k, {})
end, function(k, v)
return a.nvim_set_option_value(k, v, {})
end)
---@brief [[
--- vim.opt, vim.opt_local and vim.opt_global implementation
---
--- To be used as helpers for working with options within neovim.
--- For information on how to use, see :help vim.opt
---
---@brief ]]
--- Preserves the order and does not mutate the original list
local remove_duplicate_values = function(t)
local result, seen = {}, {}
if type(t) == 'function' then
error(debug.traceback('asdf'))
end
for _, v in ipairs(t) do
if not seen[v] then
table.insert(result, v)
end
seen[v] = true
end
return result
end
-- 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.
@@ -205,51 +14,124 @@ local key_value_options = {
winhl = true, winhl = true,
} }
---@class OptionTypes --- Convert a vimoption_T style dictionary to the correct OptionType associated with it.
--- Option Type Enum ---@return string
local OptionTypes = setmetatable({ local function get_option_metatype(name, info)
BOOLEAN = 0, if info.type == 'string' then
NUMBER = 1, if info.flaglist then
STRING = 2, return 'set'
ARRAY = 3, elseif info.commalist then
MAP = 4, if key_value_options[name] then
SET = 5, return 'map'
}, { end
__index = function(_, k) return 'array'
error('Not a valid OptionType: ' .. k) end
end, return 'string'
__newindex = function(_, k) end
error('Cannot set a new OptionType: ' .. k) return info.type
end
local options_info = setmetatable({}, {
__index = function(t, k)
local info = a.nvim_get_option_info(k)
info.metatype = get_option_metatype(k, info)
rawset(t, k, info)
return rawget(t, k)
end, end,
}) })
--- Convert a vimoption_T style dictionary to the correct OptionType associated with it. vim.env = setmetatable({}, {
---@return OptionType __index = function(_, k)
local get_option_type = function(name, info) local v = vim.fn.getenv(k)
if info.type == 'boolean' then if v == vim.NIL then
return OptionTypes.BOOLEAN return nil
elseif info.type == 'number' then end
return OptionTypes.NUMBER return v
elseif info.type == 'string' then end,
if not info.commalist and not info.flaglist then
return OptionTypes.STRING __newindex = function(_, k, v)
vim.fn.setenv(k, v)
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 end
if key_value_options[name] then local function new_opt_accessor(handle, scope)
assert(info.commalist, 'Must be a comma list to use key:value style') return setmetatable({}, {
return OptionTypes.MAP __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,
__newindex = function(_, k, v)
opt_validate(k, scope)
return a.nvim_set_option_value(k, v, { [scope] = handle or 0 })
end,
})
end end
if info.flaglist then vim.bo = new_opt_accessor(nil, 'buf')
return OptionTypes.SET vim.wo = new_opt_accessor(nil, 'win')
elseif info.commalist then
return OptionTypes.ARRAY -- vim global option
-- this ONLY sets the global option. like `setglobal`
vim.go = setmetatable({}, {
__index = function(_, k)
return a.nvim_get_option_value(k, { scope = 'global' })
end,
__newindex = function(_, k, v)
return a.nvim_set_option_value(k, v, { scope = 'global' })
end,
})
-- vim `set` style options.
-- it has no additional metamethod magic.
vim.o = setmetatable({}, {
__index = function(_, k)
return a.nvim_get_option_value(k, {})
end,
__newindex = function(_, k, v)
return a.nvim_set_option_value(k, v, {})
end,
})
---@brief [[
--- vim.opt, vim.opt_local and vim.opt_global implementation
---
--- To be used as helpers for working with options within neovim.
--- For information on how to use, see :help vim.opt
---
---@brief ]]
--- Preserves the order and does not mutate the original list
local function remove_duplicate_values(t)
local result, seen = {}, {}
for _, v in ipairs(t) do
if not seen[v] then
table.insert(result, v)
end end
error('Fallthrough in OptionTypes') seen[v] = true
else
error('Not a known info.type:' .. info.type)
end end
return result
end end
-- Check whether the OptionTypes is allowed for vim.opt -- Check whether the OptionTypes is allowed for vim.opt
@@ -272,31 +154,43 @@ local function assert_valid_value(name, value, types)
) )
end end
local function passthrough(_, x)
return x
end
local function tbl_merge(left, right)
return vim.tbl_extend('force', left, right)
end
local function tbl_remove(t, value)
if type(value) == 'string' then
t[value] = nil
else
for _, v in ipairs(value) do
t[v] = nil
end
end
return t
end
local valid_types = { local valid_types = {
[OptionTypes.BOOLEAN] = { 'boolean' }, boolean = { 'boolean' },
[OptionTypes.NUMBER] = { 'number' }, number = { 'number' },
[OptionTypes.STRING] = { 'string' }, string = { 'string' },
[OptionTypes.SET] = { 'string', 'table' }, set = { 'string', 'table' },
[OptionTypes.ARRAY] = { 'string', 'table' }, array = { 'string', 'table' },
[OptionTypes.MAP] = { 'string', 'table' }, map = { 'string', 'table' },
} }
--- Convert a lua value to a vimoption_T value
local convert_value_to_vim = (function()
-- Map of functions to take a Lua style value and convert to vimoption_T style value. -- Map of functions to take a Lua style value and convert to vimoption_T style value.
-- Each function takes (info, lua_value) -> vim_value -- Each function takes (info, lua_value) -> vim_value
local to_vim_value = { local to_vim_value = {
[OptionTypes.BOOLEAN] = function(_, value) boolean = passthrough,
return value number = passthrough,
end, string = passthrough,
[OptionTypes.NUMBER] = function(_, value)
return value
end,
[OptionTypes.STRING] = function(_, value)
return value
end,
[OptionTypes.SET] = function(info, value) set = function(info, value)
if type(value) == 'string' then if type(value) == 'string' then
return value return value
end end
@@ -323,7 +217,7 @@ local convert_value_to_vim = (function()
end end
end, end,
[OptionTypes.ARRAY] = function(info, value) array = function(info, value)
if type(value) == 'string' then if type(value) == 'string' then
return value return value
end end
@@ -333,7 +227,7 @@ local convert_value_to_vim = (function()
return table.concat(value, ',') return table.concat(value, ',')
end, end,
[OptionTypes.MAP] = function(_, value) map = function(_, value)
if type(value) == 'string' then if type(value) == 'string' then
return value return value
end end
@@ -348,34 +242,25 @@ local convert_value_to_vim = (function()
end, end,
} }
return function(name, info, value) --- Convert a lua value to a vimoption_T value
local function convert_value_to_vim(name, info, value)
if value == nil then if value == nil then
return vim.NIL return vim.NIL
end end
local option_type = get_option_type(name, info) assert_valid_value(name, value, valid_types[info.metatype])
assert_valid_value(name, value, valid_types[option_type])
return to_vim_value[option_type](info, value) return to_vim_value[info.metatype](info, value)
end end
end)()
--- Converts a vimoption_T style value to a Lua value
local convert_value_to_lua = (function()
-- Map of OptionType to functions that take vimoption_T values and convert to lua values. -- Map of OptionType to functions that take vimoption_T values and convert to lua values.
-- Each function takes (info, vim_value) -> lua_value -- Each function takes (info, vim_value) -> lua_value
local to_lua_value = { local to_lua_value = {
[OptionTypes.BOOLEAN] = function(_, value) boolean = passthrough,
return value number = passthrough,
end, string = passthrough,
[OptionTypes.NUMBER] = function(_, value)
return value
end,
[OptionTypes.STRING] = function(_, value)
return value
end,
[OptionTypes.ARRAY] = function(info, value) array = function(info, value)
if type(value) == 'table' then if type(value) == 'table' then
if not info.allows_duplicates then if not info.allows_duplicates then
value = remove_duplicate_values(value) value = remove_duplicate_values(value)
@@ -392,9 +277,7 @@ local convert_value_to_lua = (function()
-- Handles unescaped commas in a list. -- Handles unescaped commas in a list.
if string.find(value, ',,,') then if string.find(value, ',,,') then
local comma_split = vim.split(value, ',,,') local left, right = unpack(vim.split(value, ',,,'))
local left = comma_split[1]
local right = comma_split[2]
local result = {} local result = {}
vim.list_extend(result, vim.split(left, ',')) vim.list_extend(result, vim.split(left, ','))
@@ -407,9 +290,7 @@ local convert_value_to_lua = (function()
end end
if string.find(value, ',^,,', 1, true) then if string.find(value, ',^,,', 1, true) then
local comma_split = vim.split(value, ',^,,', true) local left, right = unpack(vim.split(value, ',^,,', true))
local left = comma_split[1]
local right = comma_split[2]
local result = {} local result = {}
vim.list_extend(result, vim.split(left, ',')) vim.list_extend(result, vim.split(left, ','))
@@ -424,7 +305,7 @@ local convert_value_to_lua = (function()
return vim.split(value, ',') return vim.split(value, ',')
end, end,
[OptionTypes.SET] = function(info, value) set = function(info, value)
if type(value) == 'table' then if type(value) == 'table' then
return value return value
end end
@@ -455,7 +336,7 @@ local convert_value_to_lua = (function()
end end
end, end,
[OptionTypes.MAP] = function(info, raw_value) map = function(info, raw_value)
if type(raw_value) == 'table' then if type(raw_value) == 'table' then
return raw_value return raw_value
end end
@@ -476,28 +357,21 @@ local convert_value_to_lua = (function()
end, end,
} }
return function(name, info, option_value) --- Converts a vimoption_T style value to a Lua value
return to_lua_value[get_option_type(name, info)](info, option_value) local function convert_value_to_lua(info, option_value)
end return to_lua_value[info.metatype](info, option_value)
end)()
--- Handles the mutation of various different values.
local value_mutator = function(name, info, current, new, mutator)
return mutator[get_option_type(name, info)](current, new)
end end
--- Handles the '^' operator local prepend_methods = {
local prepend_value = (function() number = function()
local methods = {
[OptionTypes.NUMBER] = function()
error("The '^' operator is not currently supported for") error("The '^' operator is not currently supported for")
end, end,
[OptionTypes.STRING] = function(left, right) string = function(left, right)
return right .. left return right .. left
end, end,
[OptionTypes.ARRAY] = function(left, right) array = function(left, right)
for i = #right, 1, -1 do for i = #right, 1, -1 do
table.insert(left, 1, right[i]) table.insert(left, 1, right[i])
end end
@@ -505,38 +379,28 @@ local prepend_value = (function()
return left return left
end, end,
[OptionTypes.MAP] = function(left, right) map = tbl_merge,
return vim.tbl_extend('force', left, right) set = tbl_merge,
end,
[OptionTypes.SET] = function(left, right)
return vim.tbl_extend('force', left, right)
end,
} }
return function(name, info, current, new) --- Handles the '^' operator
return value_mutator( local function prepend_value(info, current, new)
name, return prepend_methods[info.metatype](
info, convert_value_to_lua(info, current),
convert_value_to_lua(name, info, current), convert_value_to_lua(info, new)
convert_value_to_lua(name, info, new),
methods
) )
end end
end)()
--- Handles the '+' operator local add_methods = {
local add_value = (function() number = function(left, right)
local methods = {
[OptionTypes.NUMBER] = function(left, right)
return left + right return left + right
end, end,
[OptionTypes.STRING] = function(left, right) string = function(left, right)
return left .. right return left .. right
end, end,
[OptionTypes.ARRAY] = function(left, right) array = function(left, right)
for _, v in ipairs(right) do for _, v in ipairs(right) do
table.insert(left, v) table.insert(left, v)
end end
@@ -544,29 +408,19 @@ local add_value = (function()
return left return left
end, end,
[OptionTypes.MAP] = function(left, right) map = tbl_merge,
return vim.tbl_extend('force', left, right) set = tbl_merge,
end,
[OptionTypes.SET] = function(left, right)
return vim.tbl_extend('force', left, right)
end,
} }
return function(name, info, current, new) --- Handles the '+' operator
return value_mutator( local function add_value(info, current, new)
name, return add_methods[info.metatype](
info, convert_value_to_lua(info, current),
convert_value_to_lua(name, info, current), convert_value_to_lua(info, new)
convert_value_to_lua(name, info, new),
methods
) )
end end
end)()
--- Handles the '-' operator local function remove_one_item(t, val)
local remove_value = (function()
local remove_one_item = function(t, val)
if vim.tbl_islist(t) then if vim.tbl_islist(t) then
local remove_index = nil local remove_index = nil
for i, v in ipairs(t) do for i, v in ipairs(t) do
@@ -583,16 +437,16 @@ local remove_value = (function()
end end
end end
local methods = { local remove_methods = {
[OptionTypes.NUMBER] = function(left, right) number = function(left, right)
return left - right return left - right
end, end,
[OptionTypes.STRING] = function() string = function()
error('Subtraction not supported for strings.') error('Subtraction not supported for strings.')
end, end,
[OptionTypes.ARRAY] = function(left, right) array = function(left, right)
if type(right) == 'string' then if type(right) == 'string' then
remove_one_item(left, right) remove_one_item(left, right)
else else
@@ -604,41 +458,19 @@ local remove_value = (function()
return left return left
end, end,
[OptionTypes.MAP] = function(left, right) map = tbl_remove,
if type(right) == 'string' then set = tbl_remove,
left[right] = nil
else
for _, v in ipairs(right) do
left[v] = nil
end
end
return left
end,
[OptionTypes.SET] = function(left, right)
if type(right) == 'string' then
left[right] = nil
else
for _, v in ipairs(right) do
left[v] = nil
end
end
return left
end,
} }
return function(name, info, current, new) --- Handles the '-' operator
return value_mutator(name, info, convert_value_to_lua(name, info, current), new, methods) local function remove_value(info, current, new)
return remove_methods[info.metatype](convert_value_to_lua(info, current), new)
end end
end)()
local create_option_metatable = function(set_type) local function create_option_accessor(scope)
local set_mt, option_mt local option_mt
local make_option = function(name, value) local function make_option(name, value)
_setup()
local info = assert(options_info[name], 'Not a valid option name: ' .. name) local info = assert(options_info[name], 'Not a valid option name: ' .. name)
if type(value) == 'table' and getmetatable(value) == option_mt then if type(value) == 'table' and getmetatable(value) == option_mt then
@@ -654,67 +486,58 @@ local create_option_metatable = function(set_type)
}, option_mt) }, option_mt)
end end
local scope
if set_type == SET_TYPES.GLOBAL then
scope = 'global'
elseif set_type == SET_TYPES.LOCAL then
scope = 'local'
end
option_mt = { option_mt = {
-- To set a value, instead use: -- To set a value, instead use:
-- opt[my_option] = value -- opt[my_option] = value
_set = function(self) _set = function(self)
local value = convert_value_to_vim(self._name, self._info, self._value) local value = convert_value_to_vim(self._name, self._info, self._value)
a.nvim_set_option_value(self._name, value, { scope = scope }) a.nvim_set_option_value(self._name, value, { scope = scope })
return self
end, end,
get = function(self) get = function(self)
return convert_value_to_lua(self._name, self._info, self._value) return convert_value_to_lua(self._info, self._value)
end, end,
append = function(self, right) append = function(self, right)
return self:__add(right):_set() self._value = add_value(self._info, self._value, right)
self:_set()
end, end,
__add = function(self, right) __add = function(self, right)
return make_option(self._name, add_value(self._name, self._info, self._value, right)) return make_option(self._name, add_value(self._info, self._value, right))
end, end,
prepend = function(self, right) prepend = function(self, right)
return self:__pow(right):_set() self._value = prepend_value(self._info, self._value, right)
self:_set()
end, end,
__pow = function(self, right) __pow = function(self, right)
return make_option(self._name, prepend_value(self._name, self._info, self._value, right)) return make_option(self._name, prepend_value(self._info, self._value, right))
end, end,
remove = function(self, right) remove = function(self, right)
return self:__sub(right):_set() self._value = remove_value(self._info, self._value, right)
self:_set()
end, end,
__sub = function(self, right) __sub = function(self, right)
return make_option(self._name, remove_value(self._name, self._info, self._value, right)) return make_option(self._name, remove_value(self._info, self._value, right))
end, end,
} }
option_mt.__index = option_mt option_mt.__index = option_mt
set_mt = { return setmetatable({}, {
__index = function(_, k) __index = function(_, k)
return make_option(k, a.nvim_get_option_value(k, { scope = scope })) return make_option(k, a.nvim_get_option_value(k, { scope = scope }))
end, end,
__newindex = function(_, k, v) __newindex = function(_, k, v)
local opt = make_option(k, v) make_option(k, v):_set()
opt:_set()
end, end,
} })
return set_mt
end end
vim.opt = setmetatable({}, create_option_metatable(SET_TYPES.SET)) vim.opt = create_option_accessor()
vim.opt_local = setmetatable({}, create_option_metatable(SET_TYPES.LOCAL)) vim.opt_local = create_option_accessor('local')
vim.opt_global = setmetatable({}, create_option_metatable(SET_TYPES.GLOBAL)) vim.opt_global = create_option_accessor('global')

View File

@@ -1421,7 +1421,7 @@ describe('lua stdlib', function()
]] ]]
eq('', funcs.luaeval "vim.bo.filetype") eq('', funcs.luaeval "vim.bo.filetype")
eq(true, funcs.luaeval "vim.bo[BUF].modifiable") 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')) pcall_err(exec_lua, 'return vim.bo.nosuchopt'))
matches("Expected lua string$", matches("Expected lua string$",
pcall_err(exec_lua, 'return vim.bo[0][0].autoread')) pcall_err(exec_lua, 'return vim.bo[0][0].autoread'))
@@ -1442,7 +1442,7 @@ describe('lua stdlib', function()
eq(0, funcs.luaeval "vim.wo.cole") eq(0, funcs.luaeval "vim.wo.cole")
eq(0, funcs.luaeval "vim.wo[0].cole") eq(0, funcs.luaeval "vim.wo[0].cole")
eq(0, funcs.luaeval "vim.wo[1001].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')) pcall_err(exec_lua, 'return vim.wo.notanopt'))
matches("Expected lua string$", matches("Expected lua string$",
pcall_err(exec_lua, 'return vim.wo[0][0].list')) pcall_err(exec_lua, 'return vim.wo[0][0].list'))