From bc15b075d14c85098d674a2466d2386e08b0005f Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 1 Mar 2023 10:51:22 -0600 Subject: feat(vim.fs): pass path to find() predicate, lazy evaluate #22378 Problem: No easy way to find files under certain directories (ex: grab all files under `test/`) or exclude the content of certain paths (ex. `build/`, `.git/`) Solution: Pass the full `path` as an arg to the predicate. --- runtime/lua/vim/fs.lua | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index a0d2c4c339..2c3fc64d57 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -144,11 +144,34 @@ end --- The search can be narrowed to find only files or only directories by --- specifying {type} to be "file" or "directory", respectively. --- ----@param names (string|table|fun(name: string): boolean) Names of the files +--- Examples: +---
lua
+--- -- location of Cargo.toml from the current buffer's path
+--- local cargo = vim.fs.find('Cargo.toml', {
+---   upward = true,
+---   stop = vim.loop.os_homedir(),
+---   path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
+--- })
+---
+--- -- list all test directories under the runtime directory
+--- local test_dirs = vim.fs.find(
+---   {'test', 'tst', 'testdir'},
+---   {limit = math.huge, type = 'directory', path = './runtime/'}
+--- )
+---
+--- -- get all files ending with .cpp or .hpp inside lib/
+--- local cpp_hpp = vim.fs.find(function(name, path)
+---   return name:match('.*%.[ch]pp$') and path:match('[/\\\\]lib$')
+--- end, {limit = math.huge, type = 'file'})
+--- 
+--- +---@param names (string|table|fun(name: string, path: string): boolean) Names of the files --- and directories to find. ---- Must be base names, paths and globs are not supported. ---- The function is called per file and directory within the ---- traversed directories to test if they match {names}. +--- 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 file and directory with args: +--- - name: base name of the current item +--- - path: full path of the current item +--- The function should return `true` if the given file or directory is considered a match. --- ---@param opts (table) Optional keyword arguments: --- - path (string): Path to begin searching from. If @@ -201,7 +224,7 @@ function M.find(names, opts) test = function(p) local t = {} for name, type in M.dir(p) do - if names(name) and (not opts.type or opts.type == type) then + if (not opts.type or opts.type == type) and names(name, p) then table.insert(t, join_paths(p, name)) end end @@ -250,7 +273,7 @@ function M.find(names, opts) for other, type_ in M.dir(dir) do local f = join_paths(dir, other) if type(names) == 'function' then - if names(other) and (not opts.type or opts.type == type_) then + if (not opts.type or opts.type == type_) and names(other, dir) then if add(f) then return matches end -- cgit From 3c82ce0d625184a90e6b2dda96e38fcb44f901d2 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sun, 26 Mar 2023 12:46:24 +0100 Subject: refactor(loader): use vim.fs --- runtime/lua/vim/fs.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 2c3fc64d57..c6371e9163 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -77,6 +77,8 @@ local function join_paths(...) return (table.concat({ ... }, '/'):gsub('//+', '/')) end +---@alias Iterator fun(): string?, string? + --- Return an iterator over the files and directories located in {path} --- ---@param path (string) An absolute or relative path to the directory to iterate @@ -100,10 +102,13 @@ function M.dir(path, opts) }) if not opts.depth or opts.depth == 1 then - return function(fs) + local fs = vim.loop.fs_scandir(M.normalize(path)) + return function() + if not fs then + return + end return vim.loop.fs_scandir_next(fs) - end, - vim.loop.fs_scandir(M.normalize(path)) + end end --- @async -- cgit From 25fa051fa157ca4bce712e61602447a4222581ca Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sun, 26 Mar 2023 13:01:48 +0100 Subject: feat(vim.fs): improve normalize - Add options argument with an option to expand env vars - Resolve '//' -> '/' - Use in vim.loader --- runtime/lua/vim/fs.lua | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index c6371e9163..407b334f20 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -321,16 +321,32 @@ end --- --- ---@param path (string) Path to normalize +---@param opts table|nil Options: +--- - expand_env: boolean Expand environment variables (default: true) ---@return (string) Normalized path -function M.normalize(path) - vim.validate({ path = { path, 's' } }) - return ( - path - :gsub('^~$', vim.loop.os_homedir()) - :gsub('^~/', vim.loop.os_homedir() .. '/') - :gsub('%$([%w_]+)', vim.loop.os_getenv) - :gsub('\\', '/') - ) +function M.normalize(path, opts) + opts = opts or {} + + vim.validate({ + path = { path, { 'string' } }, + expand_env = { opts.expand_env, { 'boolean' }, true }, + }) + + if path:sub(1, 1) == '~' then + local home = vim.loop.os_homedir() or '~' + if home:sub(-1) == '\\' or home:sub(-1) == '/' then + home = home:sub(1, -2) + end + path = home .. path:sub(2) + end + + if opts.expand_env == nil or opts.expand_env then + path = path:gsub('%$([%w_]+)', vim.loop.os_getenv) + end + + path = path:gsub('\\', '/'):gsub('/+', '/') + + return path:sub(-1) == '/' and path:sub(1, -2) or path end return M -- cgit From e826d09c18ab8840592b6cdbbcfe3e311a047174 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Tue, 4 Apr 2023 23:37:46 +0200 Subject: fix(windows): consistent normalization in fs.find vim.fs.find(".luacheckrc") ``` c:\\projects\\neovim/.luacheckrc # before c:/projects/neovim/.luacheckrc # after ``` Co-authored-by: kylo252 <59826753+kylo252@users.noreply.github.com> --- runtime/lua/vim/fs.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 407b334f20..2a51bde263 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -216,7 +216,7 @@ function M.find(names, opts) ---@private local function add(match) - matches[#matches + 1] = match + matches[#matches + 1] = M.normalize(match) if #matches == limit then return true end -- cgit From 189fb6203262340e7a59e782be970bcd8ae28e61 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 17 May 2023 11:42:18 +0100 Subject: feat(treesitter): improved logging (#23638) - Add bindings to Treesitter ts_parser_set_logger and ts_parser_logger - Add logfile with path STDPATH('log')/treesitter.c - Rework existing LanguageTree loggin to use logfile - Begin implementing log levels for vim.g.__ts_debug --- runtime/lua/vim/fs.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 2a51bde263..7f9f3a2bce 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -73,10 +73,14 @@ function M.basename(file) end ---@private -local function join_paths(...) +---@param ... string +---@return string +function M._join_paths(...) return (table.concat({ ... }, '/'):gsub('//+', '/')) end +local join_paths = M._join_paths + ---@alias Iterator fun(): string?, string? --- Return an iterator over the files and directories located in {path} -- cgit From e3e6fadfd82861471c32fdcabe00bbef3de84563 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Sat, 20 May 2023 17:30:48 +0200 Subject: feat(fs): expose join_paths as `vim.fs.joinpath` (#23685) This is a small function but used a lot in some plugins. --- runtime/lua/vim/fs.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 7f9f3a2bce..864ba495f1 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -72,15 +72,15 @@ function M.basename(file) return file:match('[/\\]$') and '' or (file:match('[^\\/]*$'):gsub('\\', '/')) end ----@private +--- Concatenate directories and/or file into a single path with normalization +--- (e.g., `"foo/"` and `"bar"` get joined to `"foo/bar"`) +--- ---@param ... string ---@return string -function M._join_paths(...) +function M.joinpath(...) return (table.concat({ ... }, '/'):gsub('//+', '/')) end -local join_paths = M._join_paths - ---@alias Iterator fun(): string?, string? --- Return an iterator over the files and directories located in {path} @@ -120,14 +120,14 @@ function M.dir(path, opts) local dirs = { { path, 1 } } while #dirs > 0 do local dir0, level = unpack(table.remove(dirs, 1)) - local dir = level == 1 and dir0 or join_paths(path, dir0) + local dir = level == 1 and dir0 or M.joinpath(path, dir0) local fs = vim.loop.fs_scandir(M.normalize(dir)) while fs do local name, t = vim.loop.fs_scandir_next(fs) if not name then break end - local f = level == 1 and name or join_paths(dir0, name) + local f = level == 1 and name or M.joinpath(dir0, name) coroutine.yield(f, t) if opts.depth @@ -234,7 +234,7 @@ function M.find(names, opts) local t = {} for name, type in M.dir(p) do if (not opts.type or opts.type == type) and names(name, p) then - table.insert(t, join_paths(p, name)) + table.insert(t, M.joinpath(p, name)) end end return t @@ -243,7 +243,7 @@ function M.find(names, opts) test = function(p) local t = {} for _, name in ipairs(names) do - local f = join_paths(p, name) + local f = M.joinpath(p, name) local stat = vim.loop.fs_stat(f) if stat and (not opts.type or opts.type == stat.type) then t[#t + 1] = f @@ -280,7 +280,7 @@ function M.find(names, opts) end for other, type_ in M.dir(dir) do - local f = join_paths(dir, other) + local f = M.joinpath(dir, other) if type(names) == 'function' then if (not opts.type or opts.type == type_) and names(other, dir) then if add(f) then -- cgit From 2db719f6c2b677fcbc197b02fe52764a851523b2 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sat, 3 Jun 2023 11:06:00 +0100 Subject: feat(lua): rename vim.loop -> vim.uv (#22846) --- runtime/lua/vim/fs.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 864ba495f1..5e63fb6237 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -1,6 +1,6 @@ local M = {} -local iswin = vim.loop.os_uname().sysname == 'Windows_NT' +local iswin = vim.uv.os_uname().sysname == 'Windows_NT' --- Iterate over all the parents of the given file or directory. --- @@ -106,12 +106,12 @@ function M.dir(path, opts) }) if not opts.depth or opts.depth == 1 then - local fs = vim.loop.fs_scandir(M.normalize(path)) + local fs = vim.uv.fs_scandir(M.normalize(path)) return function() if not fs then return end - return vim.loop.fs_scandir_next(fs) + return vim.uv.fs_scandir_next(fs) end end @@ -121,9 +121,9 @@ function M.dir(path, opts) while #dirs > 0 do local dir0, level = unpack(table.remove(dirs, 1)) local dir = level == 1 and dir0 or M.joinpath(path, dir0) - local fs = vim.loop.fs_scandir(M.normalize(dir)) + local fs = vim.uv.fs_scandir(M.normalize(dir)) while fs do - local name, t = vim.loop.fs_scandir_next(fs) + local name, t = vim.uv.fs_scandir_next(fs) if not name then break end @@ -158,7 +158,7 @@ end --- -- location of Cargo.toml from the current buffer's path --- local cargo = vim.fs.find('Cargo.toml', { --- upward = true, ---- stop = vim.loop.os_homedir(), +--- stop = vim.uv.os_homedir(), --- path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)), --- }) --- @@ -212,7 +212,7 @@ function M.find(names, opts) names = type(names) == 'string' and { names } or names - local path = opts.path or vim.loop.cwd() + local path = opts.path or vim.uv.cwd() local stop = opts.stop local limit = opts.limit or 1 @@ -244,7 +244,7 @@ function M.find(names, opts) local t = {} for _, name in ipairs(names) do local f = M.joinpath(p, name) - local stat = vim.loop.fs_stat(f) + local stat = vim.uv.fs_stat(f) if stat and (not opts.type or opts.type == stat.type) then t[#t + 1] = f end @@ -337,7 +337,7 @@ function M.normalize(path, opts) }) if path:sub(1, 1) == '~' then - local home = vim.loop.os_homedir() or '~' + local home = vim.uv.os_homedir() or '~' if home:sub(-1) == '\\' or home:sub(-1) == '/' then home = home:sub(1, -2) end @@ -345,7 +345,7 @@ function M.normalize(path, opts) end if opts.expand_env == nil or opts.expand_env then - path = path:gsub('%$([%w_]+)', vim.loop.os_getenv) + path = path:gsub('%$([%w_]+)', vim.uv.os_getenv) end path = path:gsub('\\', '/'):gsub('/+', '/') -- cgit From 7154f0c98619d0789618f370a6c7f81ee1775469 Mon Sep 17 00:00:00 2001 From: Jonas Strittmatter <40792180+smjonas@users.noreply.github.com> Date: Sat, 10 Jun 2023 03:37:05 +0200 Subject: docs: fix typos (#23917) --- runtime/lua/vim/fs.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 5e63fb6237..c89147dbd2 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -72,7 +72,7 @@ function M.basename(file) return file:match('[/\\]$') and '' or (file:match('[^\\/]*$'):gsub('\\', '/')) end ---- Concatenate directories and/or file into a single path with normalization +--- Concatenate directories and/or file paths into a single path with normalization --- (e.g., `"foo/"` and `"bar"` get joined to `"foo/bar"`) --- ---@param ... string -- cgit From 8a7e3353eb5bffb10015254917361266b4b20511 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Sun, 18 Jun 2023 14:49:33 +0300 Subject: fix(fs): make `normalize()` work with '/' path (#24047) Problem: Current implementation of "remove trailing /" doesn't account for the case of literal '/' as path. Solution: Remove trailing / only if it preceded by something else. Co-authored by: notomo --- runtime/lua/vim/fs.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index c89147dbd2..cdb314fa3d 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -348,9 +348,7 @@ function M.normalize(path, opts) path = path:gsub('%$([%w_]+)', vim.uv.os_getenv) end - path = path:gsub('\\', '/'):gsub('/+', '/') - - return path:sub(-1) == '/' and path:sub(1, -2) or path + return (path:gsub('\\', '/'):gsub('/+', '/'):gsub('(.)/$', '%1')) end return M -- cgit From 766f4978d6cb146511cf0b676c01e5327db46647 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 10 Jul 2023 19:38:15 +0800 Subject: fix(lint): lint warnings #24226 --- runtime/lua/vim/fs.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index cdb314fa3d..48d76e5d7e 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -20,7 +20,7 @@ local iswin = vim.uv.os_uname().sysname == 'Windows_NT' --- --- ---@param start (string) Initial file or directory. ----@return (function) Iterator +---@return function Iterator function M.parents(start) return function(_, dir) local parent = M.dirname(dir) @@ -37,7 +37,7 @@ end --- Return the parent directory of the given file or directory --- ---@param file (string) File or directory ----@return (string) Parent directory of {file} +---@return string|nil Parent directory of {file} function M.dirname(file) if file == nil then return nil @@ -59,8 +59,8 @@ end --- Return the basename of the given file or directory --- ----@param file (string) File or directory ----@return (string) Basename of {file} +---@param file string File or directory +---@return string|nil Basename of {file} function M.basename(file) if file == nil then return nil -- cgit From e4da418ba8388e94bb186e3f9a2004ee1e96f1e5 Mon Sep 17 00:00:00 2001 From: Mike <4576770+mike325@users.noreply.github.com> Date: Tue, 18 Jul 2023 08:36:04 +0200 Subject: fix(fs.lua): normalize slash truncation (#23753) Preserve last slash in windows' root drive directories --- runtime/lua/vim/fs.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 48d76e5d7e..d06dcb87cf 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -348,7 +348,11 @@ function M.normalize(path, opts) path = path:gsub('%$([%w_]+)', vim.uv.os_getenv) end - return (path:gsub('\\', '/'):gsub('/+', '/'):gsub('(.)/$', '%1')) + path = path:gsub('\\', '/'):gsub('/+', '/') + if iswin and path:match('^%w:/$') then + return path + end + return (path:gsub('(.)/$', '%1')) end return M -- cgit From be74807eef13ff8c90d55cf8b22b01d6d33b1641 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 18 Jul 2023 15:42:30 +0100 Subject: docs(lua): more improvements (#24387) * docs(lua): teach lua2dox how to table * docs(lua): teach gen_vimdoc.py about local functions No more need to mark local functions with @private * docs(lua): mention @nodoc and @meta in dev-lua-doc * fixup! Co-authored-by: Justin M. Keyes --------- Co-authored-by: Justin M. Keyes --- runtime/lua/vim/fs.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index d06dcb87cf..842767098c 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -218,7 +218,6 @@ function M.find(names, opts) local matches = {} - ---@private local function add(match) matches[#matches + 1] = M.normalize(match) if #matches == limit then -- cgit From 86ce3878d662c1dbfec61a5ad8e7c16c4283ed5c Mon Sep 17 00:00:00 2001 From: futsuuu <105504350+futsuuu@users.noreply.github.com> Date: Thu, 20 Jul 2023 01:55:35 +0900 Subject: docs(lua): clarify fs.find() documentation #24394 --- runtime/lua/vim/fs.lua | 54 ++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 842767098c..92966523c0 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -2,7 +2,7 @@ local M = {} local iswin = vim.uv.os_uname().sysname == 'Windows_NT' ---- Iterate over all the parents of the given file or directory. +--- Iterate over all the parents of the given path. --- --- Example: ---
lua
@@ -19,7 +19,7 @@ local iswin = vim.uv.os_uname().sysname == 'Windows_NT'
 --- end
 --- 
--- ----@param start (string) Initial file or directory. +---@param start (string) Initial path. ---@return function Iterator function M.parents(start) return function(_, dir) @@ -34,9 +34,9 @@ function M.parents(start) start end ---- Return the parent directory of the given file or directory +--- Return the parent directory of the given path --- ----@param file (string) File or directory +---@param file (string) Path ---@return string|nil Parent directory of {file} function M.dirname(file) if file == nil then @@ -57,9 +57,9 @@ function M.dirname(file) return (dir:gsub('\\', '/')) end ---- Return the basename of the given file or directory +--- Return the basename of the given path --- ----@param file string File or directory +---@param file string Path ---@return string|nil Basename of {file} function M.basename(file) if file == nil then @@ -83,7 +83,7 @@ end ---@alias Iterator fun(): string?, string? ---- Return an iterator over the files and directories located in {path} +--- Return an iterator over the items located in {path} --- ---@param path (string) An absolute or relative path to the directory to iterate --- over. The path is first normalized |vim.fs.normalize()|. @@ -93,9 +93,10 @@ end --- to control traversal. Return false to stop searching the current directory. --- Only useful when depth > 1 --- ----@return Iterator over files and directories in {path}. Each iteration yields ---- two values: name and type. Each "name" is the basename of the file or ---- directory relative to {path}. Type is one of "file" or "directory". +---@return Iterator over items in {path}. Each iteration yields two values: "name" and "type". +--- "name" is the basename of the item relative to {path}. +--- "type" is one of the following: +--- "file", "directory", "link", "fifo", "socket", "char", "block", "unknown". function M.dir(path, opts) opts = opts or {} @@ -142,16 +143,16 @@ function M.dir(path, opts) end) end ---- Find files or directories in the given path. +--- Find files or directories (or other items as specified by `opts.type`) in the given path. --- ---- Finds any files or directories given in {names} starting from {path}. If ---- {upward} is "true" then the search traverses upward through parent ---- directories; otherwise, the search traverses downward. Note that downward ---- searches are recursive and may search through many directories! If {stop} ---- is non-nil, then the search stops when the directory given in {stop} is ---- reached. The search terminates when {limit} (default 1) matches are found. ---- The search can be narrowed to find only files or only directories by ---- specifying {type} to be "file" or "directory", respectively. +--- Finds items given in {names} starting from {path}. If {upward} is "true" +--- then the search traverses upward through parent directories; otherwise, +--- the search traverses downward. Note that downward searches are recursive +--- and may search through many directories! If {stop} is non-nil, then the +--- search stops when the directory given in {stop} is reached. The search +--- terminates when {limit} (default 1) matches are found. You can set {type} +--- to "file", "directory", "link", "socket", "char", "block", or "fifo" +--- to narrow the search to find only that type. --- --- Examples: ---
lua
@@ -174,13 +175,12 @@ end
 --- end, {limit = math.huge, type = 'file'})
 --- 
--- ----@param names (string|table|fun(name: string, path: string): boolean) Names of the files ---- and directories to find. +---@param names (string|table|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. ---- If {names} is a function, it is called for each traversed file and directory with args: +--- If {names} is a function, it is called for each traversed item with args: --- - name: base name of the current item --- - path: full path of the current item ---- The function should return `true` if the given file or directory is considered a match. +--- The function should return `true` if the given item is considered a match. --- ---@param opts (table) Optional keyword arguments: --- - path (string): Path to begin searching from. If @@ -191,14 +191,12 @@ end --- (recursively). --- - stop (string): Stop searching when this directory is --- reached. The directory itself is not searched. ---- - type (string): Find only files ("file") or ---- directories ("directory"). If omitted, both ---- files and directories that match {names} are ---- included. +--- - type (string): Find only items of the given type. +--- If omitted, all items that match {names} are included. --- - limit (number, default 1): Stop the search after --- finding this many matches. Use `math.huge` to --- place no limit on the number of matches. ----@return (table) Normalized paths |vim.fs.normalize()| of all matching files or directories +---@return (table) Normalized paths |vim.fs.normalize()| of all matching items function M.find(names, opts) opts = opts or {} vim.validate({ -- cgit From 37c58226a8fd02afe9836f2fe43b31570fc50bb9 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 8 Aug 2023 11:58:29 +0100 Subject: fix(lua): vim.fs typing (#24608) --- runtime/lua/vim/fs.lua | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'runtime/lua/vim/fs.lua') diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 92966523c0..7cd9e3cf04 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -20,7 +20,9 @@ local iswin = vim.uv.os_uname().sysname == 'Windows_NT' --- --- ---@param start (string) Initial path. ----@return function Iterator +---@return fun(_, dir: string): string? # Iterator +---@return nil +---@return string|nil function M.parents(start) return function(_, dir) local parent = M.dirname(dir) @@ -120,6 +122,7 @@ function M.dir(path, opts) return coroutine.wrap(function() local dirs = { { path, 1 } } while #dirs > 0 do + --- @type string, integer local dir0, level = unpack(table.remove(dirs, 1)) local dir = level == 1 and dir0 or M.joinpath(path, dir0) local fs = vim.uv.fs_scandir(M.normalize(dir)) @@ -143,6 +146,13 @@ function M.dir(path, opts) 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. --- --- Finds items given in {names} starting from {path}. If {upward} is "true" @@ -175,7 +185,7 @@ end --- end, {limit = math.huge, type = 'file'}) --- --- ----@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. --- If {names} is a function, it is called for each traversed item with args: --- - name: base name of the current item @@ -196,9 +206,9 @@ end --- - limit (number, default 1): Stop the search after --- finding this many matches. Use `math.huge` to --- 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) - opts = opts or {} + opts = opts or {} --[[@as vim.fs.find.opts]] vim.validate({ names = { names, { 's', 't', 'f' } }, path = { opts.path, 's', true }, @@ -208,13 +218,15 @@ function M.find(names, opts) 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 stop = opts.stop local limit = opts.limit or 1 - local matches = {} + local matches = {} --- @type string[] local function add(match) matches[#matches + 1] = M.normalize(match) @@ -224,7 +236,7 @@ function M.find(names, opts) end if opts.upward then - local test + local test --- @type fun(p: string): string[] if type(names) == 'function' then test = function(p) @@ -238,7 +250,7 @@ function M.find(names, opts) end else test = function(p) - local t = {} + local t = {} --- @type string[] for _, name in ipairs(names) do local f = M.joinpath(p, name) local stat = vim.uv.fs_stat(f) -- cgit From 2e92065686f62851318150a315591c30b8306a4b Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 14 Sep 2023 08:23:01 -0500 Subject: docs: replace
 with ``` (#25136)

---
 runtime/lua/vim/fs.lua | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

(limited to 'runtime/lua/vim/fs.lua')

diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua
index 7cd9e3cf04..22612a7255 100644
--- a/runtime/lua/vim/fs.lua
+++ b/runtime/lua/vim/fs.lua
@@ -5,7 +5,8 @@ local iswin = vim.uv.os_uname().sysname == 'Windows_NT'
 --- Iterate over all the parents of the given path.
 ---
 --- Example:
---- 
lua
+---
+--- ```lua
 --- local root_dir
 --- for dir in vim.fs.parents(vim.api.nvim_buf_get_name(0)) do
 ---   if vim.fn.isdirectory(dir .. "/.git") == 1 then
@@ -17,7 +18,7 @@ local iswin = vim.uv.os_uname().sysname == 'Windows_NT'
 --- if root_dir then
 ---   print("Found git repository at", root_dir)
 --- end
---- 
+--- ``` --- ---@param start (string) Initial path. ---@return fun(_, dir: string): string? # Iterator @@ -165,7 +166,8 @@ end --- to narrow the search to find only that type. --- --- Examples: ----
lua
+---
+--- ```lua
 --- -- location of Cargo.toml from the current buffer's path
 --- local cargo = vim.fs.find('Cargo.toml', {
 ---   upward = true,
@@ -183,7 +185,7 @@ end
 --- local cpp_hpp = vim.fs.find(function(name, path)
 ---   return name:match('.*%.[ch]pp$') and path:match('[/\\\\]lib$')
 --- end, {limit = math.huge, type = 'file'})
---- 
+--- ``` --- ---@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. @@ -322,16 +324,17 @@ end --- variables are also expanded. --- --- Examples: ----
lua
----   vim.fs.normalize('C:\\\\Users\\\\jdoe')
----   --> 'C:/Users/jdoe'
 ---
----   vim.fs.normalize('~/src/neovim')
----   --> '/home/jdoe/src/neovim'
+--- ```lua
+--- vim.fs.normalize('C:\\\\Users\\\\jdoe')
+--- -- 'C:/Users/jdoe'
+---
+--- vim.fs.normalize('~/src/neovim')
+--- -- '/home/jdoe/src/neovim'
 ---
----   vim.fs.normalize('$XDG_CONFIG_HOME/nvim/init.vim')
----   --> '/Users/jdoe/.config/nvim/init.vim'
---- 
+--- vim.fs.normalize('$XDG_CONFIG_HOME/nvim/init.vim') +--- -- '/Users/jdoe/.config/nvim/init.vim' +--- ``` --- ---@param path (string) Path to normalize ---@param opts table|nil Options: -- cgit