diff options
author | David Zhang <crispgm@gmail.com> | 2021-05-30 20:49:19 +0800 |
---|---|---|
committer | TJ DeVries <devries.timothyj@gmail.com> | 2021-06-29 08:42:07 -0400 |
commit | b02e64c4dffd9044ea83e3f2d74b5f6db6bf6b99 (patch) | |
tree | 9b920cc72dc83001e68a702410acb72005209eb2 /runtime/lua/vim/_meta.lua | |
parent | e6175f6389ea3fdc685fe2158948d49caa8cdba2 (diff) | |
download | rneovim-b02e64c4dffd9044ea83e3f2d74b5f6db6bf6b99.tar.gz rneovim-b02e64c4dffd9044ea83e3f2d74b5f6db6bf6b99.tar.bz2 rneovim-b02e64c4dffd9044ea83e3f2d74b5f6db6bf6b99.zip |
fix(vim.opt): Add basic error handling
Diffstat (limited to 'runtime/lua/vim/_meta.lua')
-rw-r--r-- | runtime/lua/vim/_meta.lua | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index c3f3bf66d3..92c8fe0e26 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -311,6 +311,28 @@ local get_option_type = function(name, info) end +-- Check whether the OptionTypes is allowed for vim.opt +-- If it does not match, throw an error which indicates which option causes the error. +local function assert_valid_value(name, value, types) + local type_of_value = type(value) + for _, valid_type in ipairs(types) do + if valid_type == type_of_value then + return + end + end + + error(string.format("Invalid option type '%s' for '%s', should be %s", type_of_value, name, table.concat(types, " or "))) +end + +local valid_types = { + [OptionTypes.BOOLEAN] = { "boolean" }, + [OptionTypes.NUMBER] = { "number" }, + [OptionTypes.STRING] = { "string" }, + [OptionTypes.SET] = { "string", "table" }, + [OptionTypes.ARRAY] = { "string", "table" }, + [OptionTypes.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. @@ -340,7 +362,6 @@ local convert_value_to_vim = (function() [OptionTypes.MAP] = function(_, value) if type(value) == "string" then return value end - if type(value) == "function" then error(debug.traceback("asdf")) end local result = {} for opt_key, opt_value in pairs(value) do @@ -353,7 +374,10 @@ local convert_value_to_vim = (function() } return function(name, info, value) - return to_vim_value[get_option_type(name, info)](info, value) + local option_type = get_option_type(name, info) + assert_valid_value(name, value, valid_types[option_type]) + + return to_vim_value[option_type](info, value) end end)() |