2022-05-15 13:38:19 -07:00
|
|
|
local M = {}
|
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
local iswin = vim.uv.os_uname().sysname == 'Windows_NT'
|
2022-12-01 08:15:05 -07:00
|
|
|
|
2023-07-19 09:55:35 -07:00
|
|
|
--- Iterate over all the parents of the given path.
|
2022-05-15 13:38:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-14 06:23:01 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2022-05-15 13:38:19 -07:00
|
|
|
--- 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
|
|
|
|
--- root_dir = dir
|
|
|
|
--- break
|
|
|
|
--- end
|
|
|
|
--- end
|
|
|
|
---
|
|
|
|
--- if root_dir then
|
|
|
|
--- print("Found git repository at", root_dir)
|
|
|
|
--- end
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```
|
2022-05-15 13:38:19 -07:00
|
|
|
---
|
2023-07-19 09:55:35 -07:00
|
|
|
---@param start (string) Initial path.
|
2023-08-08 03:58:29 -07:00
|
|
|
---@return fun(_, dir: string): string? # Iterator
|
|
|
|
---@return nil
|
|
|
|
---@return string|nil
|
2022-05-15 13:38:19 -07:00
|
|
|
function M.parents(start)
|
|
|
|
return function(_, dir)
|
2022-05-15 18:53:23 -07:00
|
|
|
local parent = M.dirname(dir)
|
2022-05-15 13:38:19 -07:00
|
|
|
if parent == dir then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return parent
|
|
|
|
end,
|
|
|
|
nil,
|
|
|
|
start
|
|
|
|
end
|
|
|
|
|
2023-07-19 09:55:35 -07:00
|
|
|
--- Return the parent directory of the given path
|
2022-05-15 18:53:23 -07:00
|
|
|
---
|
2023-07-19 09:55:35 -07:00
|
|
|
---@param file (string) Path
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return string|nil Parent directory of {file}
|
2022-05-15 18:53:23 -07:00
|
|
|
function M.dirname(file)
|
2022-06-03 05:59:19 -07:00
|
|
|
if file == nil then
|
|
|
|
return nil
|
|
|
|
end
|
2022-12-01 08:15:05 -07:00
|
|
|
vim.validate({ file = { file, 's' } })
|
|
|
|
if iswin and file:match('^%w:[\\/]?$') then
|
|
|
|
return (file:gsub('\\', '/'))
|
|
|
|
elseif not file:match('[\\/]') then
|
|
|
|
return '.'
|
|
|
|
elseif file == '/' or file:match('^/[^/]+$') then
|
|
|
|
return '/'
|
|
|
|
end
|
|
|
|
local dir = file:match('[/\\]$') and file:sub(1, #file - 1) or file:match('^([/\\]?.+)[/\\]')
|
|
|
|
if iswin and dir:match('^%w:$') then
|
|
|
|
return dir .. '/'
|
|
|
|
end
|
|
|
|
return (dir:gsub('\\', '/'))
|
2022-05-15 18:53:23 -07:00
|
|
|
end
|
|
|
|
|
2023-07-19 09:55:35 -07:00
|
|
|
--- Return the basename of the given path
|
2022-05-15 18:55:18 -07:00
|
|
|
---
|
2023-07-19 09:55:35 -07:00
|
|
|
---@param file string Path
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return string|nil Basename of {file}
|
2022-05-15 18:55:18 -07:00
|
|
|
function M.basename(file)
|
2022-12-01 08:15:05 -07:00
|
|
|
if file == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
vim.validate({ file = { file, 's' } })
|
|
|
|
if iswin and file:match('^%w:[\\/]?$') then
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
return file:match('[/\\]$') and '' or (file:match('[^\\/]*$'):gsub('\\', '/'))
|
2022-05-15 18:55:18 -07:00
|
|
|
end
|
|
|
|
|
2023-06-09 18:37:05 -07:00
|
|
|
--- Concatenate directories and/or file paths into a single path with normalization
|
2023-05-20 08:30:48 -07:00
|
|
|
--- (e.g., `"foo/"` and `"bar"` get joined to `"foo/bar"`)
|
|
|
|
---
|
2023-05-17 03:42:18 -07:00
|
|
|
---@param ... string
|
|
|
|
---@return string
|
2023-05-20 08:30:48 -07:00
|
|
|
function M.joinpath(...)
|
2023-01-03 10:24:14 -07:00
|
|
|
return (table.concat({ ... }, '/'):gsub('//+', '/'))
|
2022-12-13 06:59:31 -07:00
|
|
|
end
|
|
|
|
|
2023-03-26 04:46:24 -07:00
|
|
|
---@alias Iterator fun(): string?, string?
|
|
|
|
|
2023-07-19 09:55:35 -07:00
|
|
|
--- Return an iterator over the items located in {path}
|
2022-05-15 19:10:12 -07:00
|
|
|
---
|
|
|
|
---@param path (string) An absolute or relative path to the directory to iterate
|
2022-05-17 07:49:33 -07:00
|
|
|
--- over. The path is first normalized |vim.fs.normalize()|.
|
2022-12-13 06:59:31 -07:00
|
|
|
--- @param opts table|nil Optional keyword arguments:
|
|
|
|
--- - depth: integer|nil How deep the traverse (default 1)
|
|
|
|
--- - skip: (fun(dir_name: string): boolean)|nil Predicate
|
|
|
|
--- to control traversal. Return false to stop searching the current directory.
|
|
|
|
--- Only useful when depth > 1
|
|
|
|
---
|
2023-07-19 09:55:35 -07:00
|
|
|
---@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".
|
2022-12-13 06:59:31 -07:00
|
|
|
function M.dir(path, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
|
|
|
vim.validate({
|
|
|
|
path = { path, { 'string' } },
|
|
|
|
depth = { opts.depth, { 'number' }, true },
|
|
|
|
skip = { opts.skip, { 'function' }, true },
|
|
|
|
})
|
|
|
|
|
|
|
|
if not opts.depth or opts.depth == 1 then
|
2023-06-03 03:06:00 -07:00
|
|
|
local fs = vim.uv.fs_scandir(M.normalize(path))
|
2023-03-26 04:46:24 -07:00
|
|
|
return function()
|
|
|
|
if not fs then
|
|
|
|
return
|
|
|
|
end
|
2023-06-03 03:06:00 -07:00
|
|
|
return vim.uv.fs_scandir_next(fs)
|
2023-03-26 04:46:24 -07:00
|
|
|
end
|
2022-12-13 06:59:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- @async
|
|
|
|
return coroutine.wrap(function()
|
|
|
|
local dirs = { { path, 1 } }
|
|
|
|
while #dirs > 0 do
|
2023-08-08 03:58:29 -07:00
|
|
|
--- @type string, integer
|
2022-12-13 06:59:31 -07:00
|
|
|
local dir0, level = unpack(table.remove(dirs, 1))
|
2023-05-20 08:30:48 -07:00
|
|
|
local dir = level == 1 and dir0 or M.joinpath(path, dir0)
|
2023-06-03 03:06:00 -07:00
|
|
|
local fs = vim.uv.fs_scandir(M.normalize(dir))
|
2022-12-13 06:59:31 -07:00
|
|
|
while fs do
|
2023-06-03 03:06:00 -07:00
|
|
|
local name, t = vim.uv.fs_scandir_next(fs)
|
2022-12-13 06:59:31 -07:00
|
|
|
if not name then
|
|
|
|
break
|
|
|
|
end
|
2023-05-20 08:30:48 -07:00
|
|
|
local f = level == 1 and name or M.joinpath(dir0, name)
|
2022-12-13 06:59:31 -07:00
|
|
|
coroutine.yield(f, t)
|
|
|
|
if
|
|
|
|
opts.depth
|
|
|
|
and level < opts.depth
|
|
|
|
and t == 'directory'
|
|
|
|
and (not opts.skip or opts.skip(f) ~= false)
|
|
|
|
then
|
|
|
|
dirs[#dirs + 1] = { f, level + 1 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2022-05-15 19:10:12 -07:00
|
|
|
end
|
|
|
|
|
2023-08-08 03:58:29 -07:00
|
|
|
--- @class vim.fs.find.opts
|
|
|
|
--- @field path string
|
|
|
|
--- @field upward boolean
|
|
|
|
--- @field stop string
|
|
|
|
--- @field type string
|
|
|
|
--- @field limit number
|
|
|
|
|
2023-07-19 09:55:35 -07:00
|
|
|
--- Find files or directories (or other items as specified by `opts.type`) in the given path.
|
2022-05-15 19:37:35 -07:00
|
|
|
---
|
2023-07-19 09:55:35 -07:00
|
|
|
--- 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.
|
2022-05-15 19:37:35 -07:00
|
|
|
---
|
2023-03-01 09:51:22 -07:00
|
|
|
--- Examples:
|
2023-09-14 06:23:01 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-03-01 09:51:22 -07:00
|
|
|
--- -- location of Cargo.toml from the current buffer's path
|
|
|
|
--- local cargo = vim.fs.find('Cargo.toml', {
|
|
|
|
--- upward = true,
|
2023-06-03 03:06:00 -07:00
|
|
|
--- stop = vim.uv.os_homedir(),
|
2023-03-01 09:51:22 -07:00
|
|
|
--- 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'})
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```
|
2023-03-01 09:51:22 -07:00
|
|
|
---
|
2023-08-08 03:58:29 -07:00
|
|
|
---@param names (string|string[]|fun(name: string, path: string): boolean) Names of the items to find.
|
2023-03-01 09:51:22 -07:00
|
|
|
--- Must be base names, paths and globs are not supported when {names} is a string or a table.
|
2023-07-19 09:55:35 -07:00
|
|
|
--- If {names} is a function, it is called for each traversed item with args:
|
2023-03-01 09:51:22 -07:00
|
|
|
--- - name: base name of the current item
|
|
|
|
--- - path: full path of the current item
|
2023-07-19 09:55:35 -07:00
|
|
|
--- The function should return `true` if the given item is considered a match.
|
2022-11-23 16:40:07 -07:00
|
|
|
---
|
2022-05-15 19:37:35 -07:00
|
|
|
---@param opts (table) Optional keyword arguments:
|
|
|
|
--- - path (string): Path to begin searching from. If
|
2022-11-23 16:40:07 -07:00
|
|
|
--- omitted, the |current-directory| is used.
|
2022-05-15 19:37:35 -07:00
|
|
|
--- - upward (boolean, default false): If true, search
|
|
|
|
--- upward through parent directories. Otherwise,
|
|
|
|
--- search through child directories
|
|
|
|
--- (recursively).
|
|
|
|
--- - stop (string): Stop searching when this directory is
|
|
|
|
--- reached. The directory itself is not searched.
|
2023-07-19 09:55:35 -07:00
|
|
|
--- - type (string): Find only items of the given type.
|
|
|
|
--- If omitted, all items that match {names} are included.
|
2022-05-15 19:37:35 -07:00
|
|
|
--- - limit (number, default 1): Stop the search after
|
|
|
|
--- finding this many matches. Use `math.huge` to
|
|
|
|
--- place no limit on the number of matches.
|
2023-08-08 03:58:29 -07:00
|
|
|
---@return (string[]) # Normalized paths |vim.fs.normalize()| of all matching items
|
2022-05-15 19:37:35 -07:00
|
|
|
function M.find(names, opts)
|
2023-08-08 03:58:29 -07:00
|
|
|
opts = opts or {} --[[@as vim.fs.find.opts]]
|
2022-05-15 19:37:35 -07:00
|
|
|
vim.validate({
|
2022-09-13 13:16:20 -07:00
|
|
|
names = { names, { 's', 't', 'f' } },
|
2022-05-15 19:37:35 -07:00
|
|
|
path = { opts.path, 's', true },
|
|
|
|
upward = { opts.upward, 'b', true },
|
|
|
|
stop = { opts.stop, 's', true },
|
|
|
|
type = { opts.type, 's', true },
|
|
|
|
limit = { opts.limit, 'n', true },
|
|
|
|
})
|
|
|
|
|
2023-08-08 03:58:29 -07:00
|
|
|
if type(names) == 'string' then
|
|
|
|
names = { names }
|
|
|
|
end
|
2022-05-15 19:37:35 -07:00
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
local path = opts.path or vim.uv.cwd()
|
2022-05-15 19:37:35 -07:00
|
|
|
local stop = opts.stop
|
|
|
|
local limit = opts.limit or 1
|
|
|
|
|
2023-08-08 03:58:29 -07:00
|
|
|
local matches = {} --- @type string[]
|
2022-05-15 19:37:35 -07:00
|
|
|
|
|
|
|
local function add(match)
|
2023-04-04 14:37:46 -07:00
|
|
|
matches[#matches + 1] = M.normalize(match)
|
2022-05-15 19:37:35 -07:00
|
|
|
if #matches == limit then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.upward then
|
2023-08-08 03:58:29 -07:00
|
|
|
local test --- @type fun(p: string): string[]
|
2022-09-13 13:16:20 -07:00
|
|
|
|
|
|
|
if type(names) == 'function' then
|
|
|
|
test = function(p)
|
|
|
|
local t = {}
|
|
|
|
for name, type in M.dir(p) do
|
2023-03-01 09:51:22 -07:00
|
|
|
if (not opts.type or opts.type == type) and names(name, p) then
|
2023-05-20 08:30:48 -07:00
|
|
|
table.insert(t, M.joinpath(p, name))
|
2022-09-13 13:16:20 -07:00
|
|
|
end
|
2022-05-15 19:37:35 -07:00
|
|
|
end
|
2022-09-13 13:16:20 -07:00
|
|
|
return t
|
2022-05-15 19:37:35 -07:00
|
|
|
end
|
2022-09-13 13:16:20 -07:00
|
|
|
else
|
|
|
|
test = function(p)
|
2023-08-08 03:58:29 -07:00
|
|
|
local t = {} --- @type string[]
|
2022-09-13 13:16:20 -07:00
|
|
|
for _, name in ipairs(names) do
|
2023-05-20 08:30:48 -07:00
|
|
|
local f = M.joinpath(p, name)
|
2023-06-03 03:06:00 -07:00
|
|
|
local stat = vim.uv.fs_stat(f)
|
2022-09-13 13:16:20 -07:00
|
|
|
if stat and (not opts.type or opts.type == stat.type) then
|
|
|
|
t[#t + 1] = f
|
|
|
|
end
|
|
|
|
end
|
2022-05-15 19:37:35 -07:00
|
|
|
|
2022-09-13 13:16:20 -07:00
|
|
|
return t
|
|
|
|
end
|
2022-05-15 19:37:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
for _, match in ipairs(test(path)) do
|
|
|
|
if add(match) then
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for parent in M.parents(path) do
|
|
|
|
if stop and parent == stop then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, match in ipairs(test(parent)) do
|
|
|
|
if add(match) then
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local dirs = { path }
|
|
|
|
while #dirs > 0 do
|
|
|
|
local dir = table.remove(dirs, 1)
|
|
|
|
if stop and dir == stop then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2022-09-13 13:16:20 -07:00
|
|
|
for other, type_ in M.dir(dir) do
|
2023-05-20 08:30:48 -07:00
|
|
|
local f = M.joinpath(dir, other)
|
2022-09-13 13:16:20 -07:00
|
|
|
if type(names) == 'function' then
|
2023-03-01 09:51:22 -07:00
|
|
|
if (not opts.type or opts.type == type_) and names(other, dir) then
|
2022-05-15 19:37:35 -07:00
|
|
|
if add(f) then
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
end
|
2022-09-13 13:16:20 -07:00
|
|
|
else
|
|
|
|
for _, name in ipairs(names) do
|
|
|
|
if name == other and (not opts.type or opts.type == type_) then
|
|
|
|
if add(f) then
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-05-15 19:37:35 -07:00
|
|
|
end
|
|
|
|
|
2022-09-13 13:16:20 -07:00
|
|
|
if type_ == 'directory' then
|
2022-05-15 19:37:35 -07:00
|
|
|
dirs[#dirs + 1] = f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
|
2022-05-17 07:49:33 -07:00
|
|
|
--- Normalize a path to a standard format. A tilde (~) character at the
|
|
|
|
--- beginning of the path is expanded to the user's home directory and any
|
|
|
|
--- backslash (\\) characters are converted to forward slashes (/). Environment
|
|
|
|
--- variables are also expanded.
|
|
|
|
---
|
2022-11-23 16:40:07 -07:00
|
|
|
--- Examples:
|
2022-05-17 07:49:33 -07:00
|
|
|
---
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```lua
|
|
|
|
--- vim.fs.normalize('C:\\\\Users\\\\jdoe')
|
|
|
|
--- -- 'C:/Users/jdoe'
|
|
|
|
---
|
|
|
|
--- vim.fs.normalize('~/src/neovim')
|
|
|
|
--- -- '/home/jdoe/src/neovim'
|
2022-05-17 07:49:33 -07:00
|
|
|
---
|
2023-09-14 06:23:01 -07:00
|
|
|
--- vim.fs.normalize('$XDG_CONFIG_HOME/nvim/init.vim')
|
|
|
|
--- -- '/Users/jdoe/.config/nvim/init.vim'
|
|
|
|
--- ```
|
2022-05-17 07:49:33 -07:00
|
|
|
---
|
|
|
|
---@param path (string) Path to normalize
|
2023-03-26 05:01:48 -07:00
|
|
|
---@param opts table|nil Options:
|
|
|
|
--- - expand_env: boolean Expand environment variables (default: true)
|
2022-05-17 07:49:33 -07:00
|
|
|
---@return (string) Normalized path
|
2023-03-26 05:01:48 -07:00
|
|
|
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
|
2023-06-03 03:06:00 -07:00
|
|
|
local home = vim.uv.os_homedir() or '~'
|
2023-03-26 05:01:48 -07:00
|
|
|
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
|
2023-06-03 03:06:00 -07:00
|
|
|
path = path:gsub('%$([%w_]+)', vim.uv.os_getenv)
|
2023-03-26 05:01:48 -07:00
|
|
|
end
|
|
|
|
|
2023-07-17 23:36:04 -07:00
|
|
|
path = path:gsub('\\', '/'):gsub('/+', '/')
|
|
|
|
if iswin and path:match('^%w:/$') then
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
return (path:gsub('(.)/$', '%1'))
|
2022-05-17 07:49:33 -07:00
|
|
|
end
|
|
|
|
|
2022-05-15 13:38:19 -07:00
|
|
|
return M
|