refactor(vim.opt): de-nest code

This commit is contained in:
Lewis Russell
2022-09-08 17:09:44 +01:00
parent 9272d20ea4
commit 038c711539

View File

@@ -183,8 +183,6 @@ local valid_types = {
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.
-- Each function takes (info, lua_value) -> vim_value
local to_vim_value = {
@@ -244,7 +242,8 @@ local convert_value_to_vim = (function()
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
return vim.NIL
end
@@ -253,10 +252,7 @@ local convert_value_to_vim = (function()
return to_vim_value[info.metatype](info, value)
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.
-- Each function takes (info, vim_value) -> lua_value
local to_lua_value = {
@@ -361,14 +357,12 @@ local convert_value_to_lua = (function()
end,
}
return function(info, option_value)
--- Converts a vimoption_T style value to a Lua value
local function convert_value_to_lua(info, option_value)
return to_lua_value[info.metatype](info, option_value)
end
end)()
--- Handles the '^' operator
local prepend_value = (function()
local methods = {
local prepend_methods = {
number = function()
error("The '^' operator is not currently supported for")
end,
@@ -389,17 +383,15 @@ local prepend_value = (function()
set = tbl_merge,
}
return function(info, current, new)
methods[info.metatype](
--- Handles the '^' operator
local function prepend_value(info, current, new)
return prepend_methods[info.metatype](
convert_value_to_lua(info, current),
convert_value_to_lua(info, new)
)
end
end)()
--- Handles the '+' operator
local add_value = (function()
local methods = {
local add_methods = {
number = function(left, right)
return left + right
end,
@@ -420,16 +412,14 @@ local add_value = (function()
set = tbl_merge,
}
return function(info, current, new)
methods[info.metatype](
--- Handles the '+' operator
local function add_value(info, current, new)
return add_methods[info.metatype](
convert_value_to_lua(info, current),
convert_value_to_lua(info, new)
)
end
end)()
--- Handles the '-' operator
local remove_value = (function()
local function remove_one_item(t, val)
if vim.tbl_islist(t) then
local remove_index = nil
@@ -447,7 +437,7 @@ local remove_value = (function()
end
end
local methods = {
local remove_methods = {
number = function(left, right)
return left - right
end,
@@ -472,10 +462,10 @@ local remove_value = (function()
set = tbl_remove,
}
return function(info, current, new)
return methods[info.metatype](convert_value_to_lua(info, current), new)
--- Handles the '-' operator
local function remove_value(info, current, new)
return remove_methods[info.metatype](convert_value_to_lua(info, current), new)
end
end)()
local function create_option_accessor(scope)
local option_mt