fix(lua): vim.fs typing (#24608)

This commit is contained in:
Lewis Russell
2023-08-08 11:58:29 +01:00
committed by GitHub
parent 11ad8fa4cd
commit 37c58226a8
2 changed files with 30 additions and 16 deletions

View File

@@ -2884,11 +2884,11 @@ vim.fs.find({names}, {opts}) *vim.fs.find()*
< <
Parameters: ~ Parameters: ~
• {names} (string|table|fun(name: string, path: string): boolean) Names • {names} (string|string[]|fun(name: string, path: string): boolean)
of the items to find. Must be base names, paths and globs are Names of the items to find. Must be base names, paths and
not supported when {names} is a string or a table. If {names} globs are not supported when {names} is a string or a table.
is a function, it is called for each traversed item with If {names} is a function, it is called for each traversed
args: item with args:
• name: base name of the current item • name: base name of the current item
• 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.
@@ -2907,7 +2907,7 @@ vim.fs.find({names}, {opts}) *vim.fs.find()*
number of matches. number of matches.
Return: ~ Return: ~
(table) Normalized paths |vim.fs.normalize()| of all matching items (string[]) Normalized paths |vim.fs.normalize()| of all matching items
vim.fs.joinpath({...}) *vim.fs.joinpath()* vim.fs.joinpath({...}) *vim.fs.joinpath()*
Concatenate directories and/or file paths into a single path with Concatenate directories and/or file paths into a single path with
@@ -2967,8 +2967,10 @@ vim.fs.parents({start}) *vim.fs.parents()*
Parameters: ~ Parameters: ~
• {start} (string) Initial path. • {start} (string) Initial path.
Return: ~ Return (multiple): ~
(function) Iterator fun(_, dir: string): string? Iterator
nil
(string|nil)
============================================================================== ==============================================================================

View File

@@ -20,7 +20,9 @@ local iswin = vim.uv.os_uname().sysname == 'Windows_NT'
--- </pre> --- </pre>
--- ---
---@param start (string) Initial path. ---@param start (string) Initial path.
---@return function Iterator ---@return fun(_, dir: string): string? # Iterator
---@return nil
---@return string|nil
function M.parents(start) function M.parents(start)
return function(_, dir) return function(_, dir)
local parent = M.dirname(dir) local parent = M.dirname(dir)
@@ -120,6 +122,7 @@ function M.dir(path, opts)
return coroutine.wrap(function() return coroutine.wrap(function()
local dirs = { { path, 1 } } local dirs = { { path, 1 } }
while #dirs > 0 do while #dirs > 0 do
--- @type string, integer
local dir0, level = unpack(table.remove(dirs, 1)) local dir0, level = unpack(table.remove(dirs, 1))
local dir = level == 1 and dir0 or M.joinpath(path, dir0) local dir = level == 1 and dir0 or M.joinpath(path, dir0)
local fs = vim.uv.fs_scandir(M.normalize(dir)) local fs = vim.uv.fs_scandir(M.normalize(dir))
@@ -143,6 +146,13 @@ function M.dir(path, opts)
end) end)
end end
--- @class vim.fs.find.opts
--- @field path string
--- @field upward boolean
--- @field stop string
--- @field type string
--- @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.
--- ---
--- Finds items given in {names} starting from {path}. If {upward} is "true" --- Finds items given in {names} starting from {path}. If {upward} is "true"
@@ -175,7 +185,7 @@ end
--- end, {limit = math.huge, type = 'file'}) --- end, {limit = math.huge, type = 'file'})
--- </pre> --- </pre>
--- ---
---@param names (string|table|fun(name: string, path: string): boolean) Names of the items to find. ---@param names (string|string[]|fun(name: string, path: string): boolean) Names of the items to find.
--- Must be base names, paths and globs are not supported when {names} is a string or a table. --- Must be base names, paths and globs are not supported when {names} is a string or a table.
--- If {names} is a function, it is called for each traversed item with args: --- If {names} is a function, it is called for each traversed item with args:
--- - name: base name of the current item --- - name: base name of the current item
@@ -196,9 +206,9 @@ end
--- - limit (number, default 1): Stop the search after --- - limit (number, default 1): Stop the search after
--- finding this many matches. Use `math.huge` to --- finding this many matches. Use `math.huge` to
--- place no limit on the number of matches. --- place no limit on the number of matches.
---@return (table) Normalized paths |vim.fs.normalize()| of all matching items ---@return (string[]) # Normalized paths |vim.fs.normalize()| of all matching items
function M.find(names, opts) function M.find(names, opts)
opts = opts or {} opts = opts or {} --[[@as vim.fs.find.opts]]
vim.validate({ vim.validate({
names = { names, { 's', 't', 'f' } }, names = { names, { 's', 't', 'f' } },
path = { opts.path, 's', true }, path = { opts.path, 's', true },
@@ -208,13 +218,15 @@ function M.find(names, opts)
limit = { opts.limit, 'n', true }, limit = { opts.limit, 'n', true },
}) })
names = type(names) == 'string' and { names } or names if type(names) == 'string' then
names = { names }
end
local path = opts.path or vim.uv.cwd() local path = opts.path or vim.uv.cwd()
local stop = opts.stop local stop = opts.stop
local limit = opts.limit or 1 local limit = opts.limit or 1
local matches = {} local matches = {} --- @type string[]
local function add(match) local function add(match)
matches[#matches + 1] = M.normalize(match) matches[#matches + 1] = M.normalize(match)
@@ -224,7 +236,7 @@ function M.find(names, opts)
end end
if opts.upward then if opts.upward then
local test local test --- @type fun(p: string): string[]
if type(names) == 'function' then if type(names) == 'function' then
test = function(p) test = function(p)
@@ -238,7 +250,7 @@ function M.find(names, opts)
end end
else else
test = function(p) test = function(p)
local t = {} local t = {} --- @type string[]
for _, name in ipairs(names) do for _, name in ipairs(names) do
local f = M.joinpath(p, name) local f = M.joinpath(p, name)
local stat = vim.uv.fs_stat(f) local stat = vim.uv.fs_stat(f)