refactor(types): more fixes

This commit is contained in:
Lewis Russell
2024-03-06 10:03:55 +00:00
committed by Lewis Russell
parent d72c9d1d19
commit ea44f74d84
8 changed files with 84 additions and 40 deletions

View File

@ -105,6 +105,10 @@ local key_value_options = {
winhl = true,
}
--- @nodoc
--- @class vim._option.Info : vim.api.keyset.get_option_info
--- @field metatype 'boolean'|'string'|'number'|'map'|'array'|'set'
--- Convert a vimoption_T style dictionary to the correct OptionType associated with it.
---@return string
local function get_option_metatype(name, info)
@ -123,8 +127,10 @@ local function get_option_metatype(name, info)
end
--- @param name string
--- @return vim._option.Info
local function get_options_info(name)
local info = api.nvim_get_option_info2(name, {})
--- @cast info vim._option.Info
info.metatype = get_option_metatype(name, info)
return info
end
@ -139,7 +145,6 @@ end
--- vim.env.FOO = 'bar'
--- print(vim.env.TERM)
--- ```
---@param var string
vim.env = setmetatable({}, {
__index = function(_, k)
local v = vim.fn.getenv(k)
@ -311,9 +316,15 @@ vim.wo = new_win_opt_accessor()
--- For information on how to use, see :help vim.opt
--- Preserves the order and does not mutate the original list
--- @generic T
--- @param t T[]
--- @return T[]
local function remove_duplicate_values(t)
--- @type table, table<any,true>
local result, seen = {}, {}
for _, v in ipairs(t) do
for _, v in
ipairs(t --[[@as any[] ]])
do
if not seen[v] then
table.insert(result, v)
end
@ -324,8 +335,11 @@ local function remove_duplicate_values(t)
return result
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.
--- Check whether the OptionTypes is allowed for vim.opt
--- If it does not match, throw an error which indicates which option causes the error.
--- @param name any
--- @param value any
--- @param types string[]
local function assert_valid_value(name, value, types)
local type_of_value = type(value)
for _, valid_type in ipairs(types) do
@ -352,6 +366,8 @@ local function tbl_merge(left, right)
return vim.tbl_extend('force', left, right)
end
--- @param t table<any,any>
--- @param value any|any[]
local function tbl_remove(t, value)
if type(value) == 'string' then
t[value] = nil
@ -380,6 +396,8 @@ local to_vim_value = {
number = passthrough,
string = passthrough,
--- @param info vim._option.Info
--- @param value string|table<string,true>
set = function(info, value)
if type(value) == 'string' then
return value
@ -407,6 +425,8 @@ local to_vim_value = {
end
end,
--- @param info vim._option.Info
--- @param value string|string[]
array = function(info, value)
if type(value) == 'string' then
return value
@ -417,6 +437,7 @@ local to_vim_value = {
return table.concat(value, ',')
end,
--- @param value string|table<string,string>
map = function(_, value)
if type(value) == 'string' then
return value
@ -466,7 +487,8 @@ local to_lua_value = {
end
-- Handles unescaped commas in a list.
if string.find(value, ',,,') then
if value:find(',,,') then
--- @type string, string
local left, right = unpack(vim.split(value, ',,,'))
local result = {}
@ -479,8 +501,9 @@ local to_lua_value = {
return result
end
if string.find(value, ',^,,', 1, true) then
local left, right = unpack(vim.split(value, ',^,,', true))
if value:find(',^,,', 1, true) then
--- @type string, string
local left, right = unpack(vim.split(value, ',^,,', { plain = true }))
local result = {}
vim.list_extend(result, vim.split(left, ','))
@ -508,22 +531,20 @@ local to_lua_value = {
assert(info.flaglist, 'That is the only one I know how to handle')
local result = {} --- @type table<string,true>
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
return result
else
local result = {}
for i = 1, #value do
result[value:sub(i, i)] = true
end
return result
end
return result
end,
map = function(info, raw_value)
@ -533,10 +554,11 @@ local to_lua_value = {
assert(info.commalist, 'Only commas are supported currently')
local result = {}
local result = {} --- @type table<string,string>
local comma_split = vim.split(raw_value, ',')
for _, key_value_str in ipairs(comma_split) do
--- @type string, string
local key, value = unpack(vim.split(key_value_str, ':'))
key = vim.trim(key)
@ -582,14 +604,21 @@ local function prepend_value(info, current, new)
end
local add_methods = {
--- @param left integer
--- @param right integer
number = function(left, right)
return left + right
end,
--- @param left string
--- @param right string
string = function(left, right)
return left .. right
end,
--- @param left string[]
--- @param right string[]
--- @return string[]
array = function(left, right)
for _, v in ipairs(right) do
table.insert(left, v)
@ -610,6 +639,8 @@ local function add_value(info, current, new)
)
end
--- @param t table<any,any>
--- @param val any
local function remove_one_item(t, val)
if vim.tbl_islist(t) then
local remove_index = nil
@ -628,6 +659,8 @@ local function remove_one_item(t, val)
end
local remove_methods = {
--- @param left integer
--- @param right integer
number = function(left, right)
return left - right
end,
@ -636,6 +669,9 @@ local remove_methods = {
error('Subtraction not supported for strings.')
end,
--- @param left string[]
--- @param right string[]
--- @return string[]
array = function(left, right)
if type(right) == 'string' then
remove_one_item(left, right)