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

@ -1433,9 +1433,6 @@ vim.env *vim.env*
print(vim.env.TERM) print(vim.env.TERM)
< <
Parameters: ~
• {var} (`string`)
vim.go *vim.go* vim.go *vim.go*
Get or set global |options|. Like `:setglobal`. Invalid key is an error. Get or set global |options|. Like `:setglobal`. Invalid key is an error.
@ -2927,16 +2924,16 @@ vim.fs.find({names}, {opts}) *vim.fs.find()*
• path: full path of the current item The function should • path: full path of the current item The function should
return `true` if the given item is considered a match. return `true` if the given item is considered a match.
• {opts} (`table`) Optional keyword arguments: • {opts} (`table`) Optional keyword arguments:
• {path} (`string`) Path to begin searching from. If omitted, • {path}? (`string`) Path to begin searching from. If
the |current-directory| is used. omitted, the |current-directory| is used.
• {upward} (`boolean`, default: `false`) Search upward • {upward}? (`boolean`, default: `false`) Search upward
through parent directories. Otherwise, search through child through parent directories. Otherwise, search through child
directories (recursively). directories (recursively).
• {stop} (`string`) Stop searching when this directory is • {stop}? (`string`) Stop searching when this directory is
reached. The directory itself is not searched. reached. The directory itself is not searched.
• {type} (`string`) Find only items of the given type. If • {type}? (`string`) Find only items of the given type. If
omitted, all items that match {names} are included. omitted, all items that match {names} are included.
• {limit} (`number`, default: `1`) Stop the search after • {limit}? (`number`, default: `1`) Stop the search after
finding this many matches. Use `math.huge` to place no finding this many matches. Use `math.huge` to place no
limit on the number of matches. limit on the number of matches.

View File

@ -275,6 +275,7 @@ do
for _, line in ipairs(lines) do for _, line in ipairs(lines) do
nchars = nchars + line:len() nchars = nchars + line:len()
end end
--- @type integer, integer
local row, col = unpack(vim.api.nvim_win_get_cursor(0)) local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local bufline = vim.api.nvim_buf_get_lines(0, row - 1, row, true)[1] local bufline = vim.api.nvim_buf_get_lines(0, row - 1, row, true)[1]
local firstline = lines[1] local firstline = lines[1]
@ -355,8 +356,11 @@ end
-- vim.fn.{func}(...) -- vim.fn.{func}(...)
---@nodoc ---@nodoc
vim.fn = setmetatable({}, { vim.fn = setmetatable({}, {
--- @param t table<string,function>
--- @param key string
--- @return function
__index = function(t, key) __index = function(t, key)
local _fn local _fn --- @type function
if vim.api[key] ~= nil then if vim.api[key] ~= nil then
_fn = function() _fn = function()
error(string.format('Tried to call API function with vim.fn: use vim.api.%s instead', key)) error(string.format('Tried to call API function with vim.fn: use vim.api.%s instead', key))
@ -620,7 +624,7 @@ function vim.notify(msg, level, opts) -- luacheck: no unused args
end end
do do
local notified = {} local notified = {} --- @type table<string,true>
--- Displays a notification only one time. --- Displays a notification only one time.
--- ---
@ -641,7 +645,7 @@ do
end end
end end
local on_key_cbs = {} local on_key_cbs = {} --- @type table<integer,function>
--- Adds Lua function {fn} with namespace id {ns_id} as a listener to every, --- Adds Lua function {fn} with namespace id {ns_id} as a listener to every,
--- yes every, input key. --- yes every, input key.
@ -711,6 +715,7 @@ end
--- 2. Can we get it to return things from global namespace even with `print(` in front. --- 2. Can we get it to return things from global namespace even with `print(` in front.
--- ---
--- @param pat string --- @param pat string
--- @return any[], integer
function vim._expand_pat(pat, env) function vim._expand_pat(pat, env)
env = env or _G env = env or _G
@ -743,7 +748,7 @@ function vim._expand_pat(pat, env)
if type(final_env) ~= 'table' then if type(final_env) ~= 'table' then
return {}, 0 return {}, 0
end end
local key local key --- @type any
-- Normally, we just have a string -- Normally, we just have a string
-- Just attempt to get the string directly from the environment -- Just attempt to get the string directly from the environment
@ -785,7 +790,8 @@ function vim._expand_pat(pat, env)
end end
end end
local keys = {} local keys = {} --- @type table<string,true>
--- @param obj table<any,any>
local function insert_keys(obj) local function insert_keys(obj)
for k, _ in pairs(obj) do for k, _ in pairs(obj) do
if type(k) == 'string' and string.sub(k, 1, string.len(match_part)) == match_part then if type(k) == 'string' and string.sub(k, 1, string.len(match_part)) == match_part then
@ -813,6 +819,7 @@ function vim._expand_pat(pat, env)
end end
--- @param lua_string string --- @param lua_string string
--- @return (string|string[])[], integer
vim._expand_pat_get_parts = function(lua_string) vim._expand_pat_get_parts = function(lua_string)
local parts = {} local parts = {}
@ -870,6 +877,7 @@ vim._expand_pat_get_parts = function(lua_string)
end end
end end
--- @param val any[]
parts = vim.tbl_filter(function(val) parts = vim.tbl_filter(function(val)
return #val > 0 return #val > 0
end, parts) end, parts)
@ -880,7 +888,7 @@ end
do do
-- Ideally we should just call complete() inside omnifunc, though there are -- Ideally we should just call complete() inside omnifunc, though there are
-- some bugs, so fake the two-step dance for now. -- some bugs, so fake the two-step dance for now.
local matches local matches --- @type any[]
--- Omnifunc for completing Lua values from the runtime Lua interpreter, --- Omnifunc for completing Lua values from the runtime Lua interpreter,
--- similar to the builtin completion for the `:lua` command. --- similar to the builtin completion for the `:lua` command.

View File

@ -1,5 +1,5 @@
local pathtrails = {} local pathtrails = {} --- @type table<string,true> ta
vim._so_trails = {} vim._so_trails = {} --- @type string[]
for s in (package.cpath .. ';'):gmatch('[^;]*;') do for s in (package.cpath .. ';'):gmatch('[^;]*;') do
s = s:sub(1, -2) -- Strip trailing semicolon s = s:sub(1, -2) -- Strip trailing semicolon
-- Find out path patterns. pathtrail should contain something like -- Find out path patterns. pathtrail should contain something like
@ -65,6 +65,7 @@ vim._submodules = {
-- These are for loading runtime modules in the vim namespace lazily. -- These are for loading runtime modules in the vim namespace lazily.
setmetatable(vim, { setmetatable(vim, {
--- @param t table<any,any>
__index = function(t, key) __index = function(t, key)
if vim._submodules[key] then if vim._submodules[key] then
t[key] = require('vim.' .. key) t[key] = require('vim.' .. key)
@ -73,6 +74,7 @@ setmetatable(vim, {
require('vim._inspector') require('vim._inspector')
return t[key] return t[key]
elseif vim.startswith(key, 'uri_') then elseif vim.startswith(key, 'uri_') then
--- @type any?
local val = require('vim.uri')[key] local val = require('vim.uri')[key]
if val ~= nil then if val ~= nil then
-- Expose all `vim.uri` functions on the `vim` module. -- Expose all `vim.uri` functions on the `vim` module.

View File

@ -129,7 +129,7 @@ error('Cannot require a meta file')
--- @field last_set_chan integer --- @field last_set_chan integer
--- @field type 'string'|'boolean'|'number' --- @field type 'string'|'boolean'|'number'
--- @field default string|boolean|integer --- @field default string|boolean|integer
--- @field allow_duplicates boolean --- @field allows_duplicates boolean
--- @class vim.api.keyset.parse_cmd.mods --- @class vim.api.keyset.parse_cmd.mods
--- @field filter { force: boolean, pattern: string } --- @field filter { force: boolean, pattern: string }

View File

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

View File

@ -152,25 +152,25 @@ end
--- ---
--- Path to begin searching from. If --- Path to begin searching from. If
--- omitted, the |current-directory| is used. --- omitted, the |current-directory| is used.
--- @field path string --- @field path? string
--- ---
--- Search upward through parent directories. --- Search upward through parent directories.
--- Otherwise, search through child directories (recursively). --- Otherwise, search through child directories (recursively).
--- (default: `false`) --- (default: `false`)
--- @field upward boolean --- @field upward? boolean
--- ---
--- Stop searching when this directory is reached. --- Stop searching when this directory is reached.
--- The directory itself is not searched. --- The directory itself is not searched.
--- @field stop string --- @field stop? string
--- ---
--- Find only items of the given type. --- Find only items of the given type.
--- If omitted, all items that match {names} are included. --- If omitted, all items that match {names} are included.
--- @field type string --- @field type? string
--- ---
--- Stop the search after finding this many matches. --- Stop the search after finding this many matches.
--- Use `math.huge` to place no limit on the number of matches. --- Use `math.huge` to place no limit on the number of matches.
--- (default: `1`) --- (default: `1`)
--- @field limit number --- @field limit? number
--- Find files or directories (or other items as specified by `opts.type`) in the given path. --- Find files or directories (or other items as specified by `opts.type`) in the given path.
--- ---
@ -229,7 +229,7 @@ function M.find(names, opts)
names = { names } names = { names }
end end
local path = opts.path or vim.uv.cwd() local path = opts.path or assert(vim.uv.cwd())
local stop = opts.stop local stop = opts.stop
local limit = opts.limit or 1 local limit = opts.limit or 1

View File

@ -7,7 +7,7 @@ local loaders = package.loaders
local M = {} local M = {}
---@alias CacheHash {mtime: {nsec: integer, sec: integer}, size: integer, type?: uv.aliases.fs_stat_types} ---@alias CacheHash {mtime: {nsec: integer, sec: integer}, size: integer, type?: string}
---@alias CacheEntry {hash:CacheHash, chunk:string} ---@alias CacheEntry {hash:CacheHash, chunk:string}
--- @class vim.loader.find.Opts --- @class vim.loader.find.Opts

View File

@ -592,7 +592,8 @@ function vim.spairs(t)
if keys[i] then if keys[i] then
return keys[i], t[keys[i]] return keys[i], t[keys[i]]
end end
end end,
t
end end
--- Tests if `t` is an "array": a table indexed _only_ by integers (potentially non-contiguous). --- Tests if `t` is an "array": a table indexed _only_ by integers (potentially non-contiguous).