diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-09-08 17:09:44 +0100 |
---|---|---|
committer | Lewis Russell <lewis6991@gmail.com> | 2022-09-09 12:38:15 +0100 |
commit | 038c7115393c931b29c451eb1434d283c74ba55e (patch) | |
tree | a7ad0ba9c8e865fa63b3a14e24d5d3a32fca50d1 /runtime/lua/vim/_meta.lua | |
parent | 9272d20ea48ea0beb1ecefc0331344a1a3e4b05e (diff) | |
download | rneovim-038c7115393c931b29c451eb1434d283c74ba55e.tar.gz rneovim-038c7115393c931b29c451eb1434d283c74ba55e.tar.bz2 rneovim-038c7115393c931b29c451eb1434d283c74ba55e.zip |
refactor(vim.opt): de-nest code
Diffstat (limited to 'runtime/lua/vim/_meta.lua')
-rw-r--r-- | runtime/lua/vim/_meta.lua | 446 |
1 files changed, 218 insertions, 228 deletions
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index fc5df0116d..dada918d69 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -183,299 +183,289 @@ 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 = { - boolean = passthrough, - number = passthrough, - string = passthrough, - - set = function(info, value) - if type(value) == 'string' then - return value - end - - if info.flaglist and info.commalist then - local keys = {} - for k, v in pairs(value) do - if v then - table.insert(keys, k) - end - end +-- 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 = { + boolean = passthrough, + number = passthrough, + string = passthrough, + + set = function(info, value) + if type(value) == 'string' then + return value + end - table.sort(keys) - return table.concat(keys, ',') - else - local result = '' - for k, v in pairs(value) do - if v then - result = result .. k - end + if info.flaglist and info.commalist then + local keys = {} + for k, v in pairs(value) do + if v then + table.insert(keys, k) end - - return result end - end, - array = function(info, value) - if type(value) == 'string' then - return value - end - if not info.allows_duplicates then - value = remove_duplicate_values(value) + table.sort(keys) + return table.concat(keys, ',') + else + local result = '' + for k, v in pairs(value) do + if v then + result = result .. k + end end - return table.concat(value, ',') - end, - map = function(_, value) - if type(value) == 'string' then - return value - end + return result + end + end, - local result = {} - for opt_key, opt_value in pairs(value) do - table.insert(result, string.format('%s:%s', opt_key, opt_value)) - end + array = function(info, value) + if type(value) == 'string' then + return value + end + if not info.allows_duplicates then + value = remove_duplicate_values(value) + end + return table.concat(value, ',') + end, - table.sort(result) - return table.concat(result, ',') - end, - } + map = function(_, value) + if type(value) == 'string' then + return value + end - return function(name, info, value) - if value == nil then - return vim.NIL + local result = {} + for opt_key, opt_value in pairs(value) do + table.insert(result, string.format('%s:%s', opt_key, opt_value)) end - assert_valid_value(name, value, valid_types[info.metatype]) + table.sort(result) + return table.concat(result, ',') + end, +} - return to_vim_value[info.metatype](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 -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 = { - boolean = passthrough, - number = passthrough, - string = passthrough, - - array = function(info, value) - if type(value) == 'table' then - if not info.allows_duplicates then - value = remove_duplicate_values(value) - end + assert_valid_value(name, value, valid_types[info.metatype]) - return value - end + return to_vim_value[info.metatype](info, value) +end + +-- 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 = { + boolean = passthrough, + number = passthrough, + string = passthrough, - -- Empty strings mean that there is nothing there, - -- so empty table should be returned. - if value == '' then - return {} + array = function(info, value) + if type(value) == 'table' then + if not info.allows_duplicates then + value = remove_duplicate_values(value) end - -- Handles unescaped commas in a list. - if string.find(value, ',,,') then - local left, right = unpack(vim.split(value, ',,,')) + return value + end - local result = {} - vim.list_extend(result, vim.split(left, ',')) - table.insert(result, ',') - vim.list_extend(result, vim.split(right, ',')) + -- Empty strings mean that there is nothing there, + -- so empty table should be returned. + if value == '' then + return {} + end - table.sort(result) + -- Handles unescaped commas in a list. + if string.find(value, ',,,') then + local left, right = unpack(vim.split(value, ',,,')) - return result - end + local result = {} + vim.list_extend(result, vim.split(left, ',')) + table.insert(result, ',') + vim.list_extend(result, vim.split(right, ',')) - if string.find(value, ',^,,', 1, true) then - local left, right = unpack(vim.split(value, ',^,,', true)) + table.sort(result) - local result = {} - vim.list_extend(result, vim.split(left, ',')) - table.insert(result, '^,') - vim.list_extend(result, vim.split(right, ',')) + return result + end - table.sort(result) + if string.find(value, ',^,,', 1, true) then + local left, right = unpack(vim.split(value, ',^,,', true)) - return result - end + local result = {} + vim.list_extend(result, vim.split(left, ',')) + table.insert(result, '^,') + vim.list_extend(result, vim.split(right, ',')) - return vim.split(value, ',') - end, + table.sort(result) - set = function(info, value) - if type(value) == 'table' then - return value - end + return result + end - -- Empty strings mean that there is nothing there, - -- so empty table should be returned. - if value == '' then - return {} - end + return vim.split(value, ',') + end, - assert(info.flaglist, 'That is the only one I know how to handle') + set = function(info, value) + if type(value) == 'table' then + return value + end - if info.flaglist and info.commalist then - local split_value = vim.split(value, ',') - local result = {} - for _, v in ipairs(split_value) do - result[v] = true - end + -- Empty strings mean that there is nothing there, + -- so empty table should be returned. + if value == '' then + return {} + end - return result - else - local result = {} - for i = 1, #value do - result[value:sub(i, i)] = true - end + assert(info.flaglist, 'That is the only one I know how to handle') - return result + if info.flaglist and info.commalist then + local split_value = vim.split(value, ',') + local result = {} + for _, v in ipairs(split_value) do + result[v] = true end - end, - map = function(info, raw_value) - if type(raw_value) == 'table' then - return raw_value + return result + else + local result = {} + for i = 1, #value do + result[value:sub(i, i)] = true end - assert(info.commalist, 'Only commas are supported currently') + return result + end + end, - local result = {} + map = function(info, raw_value) + if type(raw_value) == 'table' then + return raw_value + end - local comma_split = vim.split(raw_value, ',') - for _, key_value_str in ipairs(comma_split) do - local key, value = unpack(vim.split(key_value_str, ':')) - key = vim.trim(key) + assert(info.commalist, 'Only commas are supported currently') - result[key] = value - end + local result = {} - return result - end, - } + local comma_split = vim.split(raw_value, ',') + for _, key_value_str in ipairs(comma_split) do + local key, value = unpack(vim.split(key_value_str, ':')) + key = vim.trim(key) - return function(info, option_value) - return to_lua_value[info.metatype](info, option_value) - end -end)() + result[key] = value + end ---- Handles the '^' operator -local prepend_value = (function() - local methods = { - number = function() - error("The '^' operator is not currently supported for") - end, + return result + end, +} - string = function(left, right) - return right .. left - end, +--- 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 - array = function(left, right) - for i = #right, 1, -1 do - table.insert(left, 1, right[i]) - end +local prepend_methods = { + number = function() + error("The '^' operator is not currently supported for") + end, - return left - end, + string = function(left, right) + return right .. left + end, - map = tbl_merge, - set = tbl_merge, - } + array = function(left, right) + for i = #right, 1, -1 do + table.insert(left, 1, right[i]) + end - return function(info, current, new) - methods[info.metatype]( - convert_value_to_lua(info, current), - convert_value_to_lua(info, new) - ) - end -end)() + return left + end, ---- Handles the '+' operator -local add_value = (function() - local methods = { - number = function(left, right) - return left + right - end, + map = tbl_merge, + set = tbl_merge, +} - string = function(left, right) - return left .. right - end, +--- 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 - array = function(left, right) - for _, v in ipairs(right) do - table.insert(left, v) - end +local add_methods = { + number = function(left, right) + return left + right + end, - return left - end, + string = function(left, right) + return left .. right + end, - map = tbl_merge, - set = tbl_merge, - } + array = function(left, right) + for _, v in ipairs(right) do + table.insert(left, v) + end - return function(info, current, new) - methods[info.metatype]( - convert_value_to_lua(info, current), - convert_value_to_lua(info, new) - ) - end -end)() + return left + 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 - for i, v in ipairs(t) do - if v == val then - remove_index = i - end - end + map = tbl_merge, + set = tbl_merge, +} - if remove_index then - table.remove(t, remove_index) +--- 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 + +local function remove_one_item(t, val) + if vim.tbl_islist(t) then + local remove_index = nil + for i, v in ipairs(t) do + if v == val then + remove_index = i end - else - t[val] = nil end + + if remove_index then + table.remove(t, remove_index) + end + else + t[val] = nil end +end - local methods = { - number = function(left, right) - return left - right - end, +local remove_methods = { + number = function(left, right) + return left - right + end, - string = function() - error('Subtraction not supported for strings.') - end, + string = function() + error('Subtraction not supported for strings.') + end, - array = function(left, right) - if type(right) == 'string' then - remove_one_item(left, right) - else - for _, v in ipairs(right) do - remove_one_item(left, v) - end + array = function(left, right) + if type(right) == 'string' then + remove_one_item(left, right) + else + for _, v in ipairs(right) do + remove_one_item(left, v) end + end - return left - end, + return left + end, - map = tbl_remove, - set = tbl_remove, - } + map = tbl_remove, + set = tbl_remove, +} - return function(info, current, new) - return methods[info.metatype](convert_value_to_lua(info, current), new) - end -end)() +--- Handles the '-' operator +local function remove_value(info, current, new) + return remove_methods[info.metatype](convert_value_to_lua(info, current), new) +end local function create_option_accessor(scope) local option_mt |