diff options
| author | Siddhant Agarwal <68201519+siddhantdev@users.noreply.github.com> | 2025-06-09 22:01:37 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-09 09:31:37 -0700 |
| commit | 2f0fbdaa480c5b014b68bfdb85aa105d0bef3637 (patch) | |
| tree | 559c832e978ccfec1e91971beb1176e1d202e24f /runtime/lua/vim/fs.lua | |
| parent | 2d980e37c836a52074bea59bbeeed9b9062c26db (diff) | |
feat(vim.fs): root() can specify "equal priority" #34276
Diffstat (limited to 'runtime/lua/vim/fs.lua')
| -rw-r--r-- | runtime/lua/vim/fs.lua | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 1a99627347..496d37af31 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -388,14 +388,29 @@ end --- vim.fs.root(0, function(name, path) --- return name:match('%.csproj$') ~= nil --- end) +--- +--- -- Find the first ancestor directory containing EITHER "stylua.toml" or ".luarc.json"; if +--- -- not found, find the first ancestor containing ".git": +--- vim.fs.root(0, { { 'stylua.toml', '.luarc.json' }, '.git' }) --- ``` --- --- @since 12 --- @param source integer|string Buffer number (0 for current buffer) or file path (absolute or --- relative to the |current-directory|) to begin the search from. ---- @param marker (string|string[]|fun(name: string, path: string): boolean) A marker, or list ---- of markers, to search for. If a function, the function is called for each ---- evaluated item and should return true if {name} and {path} are a match. +--- @param marker (string|string[]|fun(name: string, path: string): boolean)[]|string|fun(name: string, path: string): boolean A marker or a list of markers. +--- A marker has one of three types: string, a list of strings or a function. The +--- parameter also accepts a list of markers, each of which is any of those three +--- types. If a marker is a function, it is called for each evaluated item and +--- should return true if {name} and {path} are a match. If a list of markers is +--- passed, each marker in the list is evaluated in order and the first marker +--- which is matched returns the parent directory that it found. This allows +--- listing markers with priority. E.g. - in the following list, a parent directory +--- containing either 'a' or 'b' is searched for. If neither is found, then 'c' is +--- searched for. So, 'c' has lower priority than 'a' and 'b' which have equal +--- priority. +--- ```lua +--- marker = { { 'a', 'b' }, 'c' } +--- ``` --- @return string? # Directory path containing one of the given markers, or nil if no directory was --- found. function M.root(source, marker) @@ -415,16 +430,19 @@ function M.root(source, marker) error('invalid type for argument "source": expected string or buffer number') end - local paths = M.find(marker, { - upward = true, - path = vim.fn.fnamemodify(path, ':p:h'), - }) + local markers = type(marker) == 'table' and marker or { marker } + for _, mark in ipairs(markers) do + local paths = M.find(mark, { + upward = true, + path = vim.fn.fnamemodify(path, ':p:h'), + }) - if #paths == 0 then - return nil + if #paths ~= 0 then + return vim.fs.dirname(paths[1]) + end end - return vim.fs.dirname(paths[1]) + return nil end --- Split a Windows path into a prefix and a body, such that the body can be processed like a POSIX |
