refactor(vim.opt): remove enums

This commit is contained in:
Lewis Russell
2022-09-06 22:56:55 +01:00
parent bc88691dbd
commit f21e2a51ba

View File

@@ -4,12 +4,6 @@ local vim = assert(vim)
local a = vim.api local a = vim.api
local validate = vim.validate local validate = vim.validate
local SET_TYPES = setmetatable({
SET = 0,
LOCAL = 1,
GLOBAL = 2,
}, { __index = error })
local options_info = nil local options_info = nil
local buf_options = nil local buf_options = nil
local glb_options = nil local glb_options = nil
@@ -201,52 +195,31 @@ local key_value_options = {
winhl = true, winhl = true,
} }
---@class OptionTypes
--- Option Type Enum
local OptionTypes = setmetatable({
BOOLEAN = 0,
NUMBER = 1,
STRING = 2,
ARRAY = 3,
MAP = 4,
SET = 5,
}, {
__index = function(_, k)
error('Not a valid OptionType: ' .. k)
end,
__newindex = function(_, k)
error('Cannot set a new OptionType: ' .. k)
end,
})
--- Convert a vimoption_T style dictionary to the correct OptionType associated with it. --- Convert a vimoption_T style dictionary to the correct OptionType associated with it.
---@return OptionType ---@return string
local get_option_type = function(name, info) local get_option_type = function(name, info)
if info.type == 'boolean' then if info.type == 'boolean' then
return OptionTypes.BOOLEAN return 'boolean'
elseif info.type == 'number' then elseif info.type == 'number' then
return OptionTypes.NUMBER return 'number'
elseif info.type == 'string' then elseif info.type == 'string' then
if not info.commalist and not info.flaglist then if not info.commalist and not info.flaglist then
return OptionTypes.STRING return 'string'
end end
if key_value_options[name] then if key_value_options[name] then
assert(info.commalist, 'Must be a comma list to use key:value style') assert(info.commalist, 'Must be a comma list to use key:value style')
return OptionTypes.MAP return 'map'
end end
if info.flaglist then if info.flaglist then
return OptionTypes.SET return 'set'
elseif info.commalist then elseif info.commalist then
return OptionTypes.ARRAY return 'array'
end
end end
error('Fallthrough in OptionTypes')
else
error('Not a known info.type:' .. info.type) error('Not a known info.type:' .. info.type)
end end
end
-- Check whether the OptionTypes is allowed for vim.opt -- Check whether the OptionTypes is allowed for vim.opt
-- If it does not match, throw an error which indicates which option causes the error. -- If it does not match, throw an error which indicates which option causes the error.
@@ -269,12 +242,12 @@ local function assert_valid_value(name, value, types)
end 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 --- Convert a lua value to a vimoption_T value
@@ -282,17 +255,17 @@ 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 = function(_, value)
return value return value
end, end,
[OptionTypes.NUMBER] = function(_, value) number = function(_, value)
return value return value
end, end,
[OptionTypes.STRING] = function(_, value) string = function(_, value)
return value return value
end, 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
@@ -319,7 +292,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
@@ -329,7 +302,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
@@ -361,17 +334,17 @@ 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 = function(_, value)
return value return value
end, end,
[OptionTypes.NUMBER] = function(_, value) number = function(_, value)
return value return value
end, end,
[OptionTypes.STRING] = function(_, value) string = function(_, value)
return value return value
end, 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)
@@ -420,7 +393,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
@@ -451,7 +424,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
@@ -485,15 +458,15 @@ end
--- Handles the '^' operator --- Handles the '^' operator
local prepend_value = (function() local prepend_value = (function()
local methods = { local methods = {
[OptionTypes.NUMBER] = function() 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
@@ -501,11 +474,11 @@ local prepend_value = (function()
return left return left
end, end,
[OptionTypes.MAP] = function(left, right) map = function(left, right)
return vim.tbl_extend('force', left, right) return vim.tbl_extend('force', left, right)
end, end,
[OptionTypes.SET] = function(left, right) set = function(left, right)
return vim.tbl_extend('force', left, right) return vim.tbl_extend('force', left, right)
end, end,
} }
@@ -524,15 +497,15 @@ end)()
--- Handles the '+' operator --- Handles the '+' operator
local add_value = (function() local add_value = (function()
local methods = { local methods = {
[OptionTypes.NUMBER] = function(left, right) 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
@@ -540,11 +513,11 @@ local add_value = (function()
return left return left
end, end,
[OptionTypes.MAP] = function(left, right) map = function(left, right)
return vim.tbl_extend('force', left, right) return vim.tbl_extend('force', left, right)
end, end,
[OptionTypes.SET] = function(left, right) set = function(left, right)
return vim.tbl_extend('force', left, right) return vim.tbl_extend('force', left, right)
end, end,
} }
@@ -580,15 +553,15 @@ local remove_value = (function()
end end
local methods = { local 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
@@ -600,7 +573,7 @@ local remove_value = (function()
return left return left
end, end,
[OptionTypes.MAP] = function(left, right) map = function(left, right)
if type(right) == 'string' then if type(right) == 'string' then
left[right] = nil left[right] = nil
else else
@@ -612,7 +585,7 @@ local remove_value = (function()
return left return left
end, end,
[OptionTypes.SET] = function(left, right) set = function(left, right)
if type(right) == 'string' then if type(right) == 'string' then
left[right] = nil left[right] = nil
else else
@@ -630,7 +603,7 @@ local remove_value = (function()
end end
end)() end)()
local create_option_metatable = function(set_type) local create_option_metatable = function(scope)
local set_mt, option_mt local set_mt, option_mt
local make_option = function(name, value) local make_option = function(name, value)
@@ -650,13 +623,6 @@ 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
@@ -711,6 +677,6 @@ local create_option_metatable = function(set_type)
return set_mt return set_mt
end end
vim.opt = setmetatable({}, create_option_metatable(SET_TYPES.SET)) vim.opt = setmetatable({}, create_option_metatable())
vim.opt_local = setmetatable({}, create_option_metatable(SET_TYPES.LOCAL)) vim.opt_local = setmetatable({}, create_option_metatable('local'))
vim.opt_global = setmetatable({}, create_option_metatable(SET_TYPES.GLOBAL)) vim.opt_global = setmetatable({}, create_option_metatable('global'))