mirror of
https://github.com/neovim/nvim-lspconfig
synced 2024-09-08 02:25:50 +02:00
fix: util.root_pattern prioritises pattern order (#2885)
* fix: util.root_pattern prioritises pattern order Instead of traversing the filesystem upwards once and returning the first match of all the patterns, it traverses the filesystem upwards once for each pattern. This means that the order of the patterns provided matters, and the highest priority patterns should be put first. Also updated corresponding tests. * docs: update util.root_pattern description
This commit is contained in:
parent
a4083b0e87
commit
7384e7149e
3 changed files with 16 additions and 12 deletions
|
@ -371,7 +371,8 @@ below returns a function that takes as its argument the current buffer path.
|
|||
- `util.root_pattern`: function which takes multiple arguments, each
|
||||
corresponding to a different root pattern against which the contents of the
|
||||
current directory are matched using |vim.fn.glob()| while traversing up the
|
||||
filesystem.
|
||||
filesystem. Parent directories are traversed once per pattern, in the order
|
||||
the patterns are specified.
|
||||
>
|
||||
root_dir = util.root_pattern('pyproject.toml', 'requirements.txt')
|
||||
<
|
||||
|
|
|
@ -250,18 +250,21 @@ end
|
|||
|
||||
function M.root_pattern(...)
|
||||
local patterns = vim.tbl_flatten { ... }
|
||||
local function matcher(path)
|
||||
for _, pattern in ipairs(patterns) do
|
||||
for _, p in ipairs(vim.fn.glob(M.path.join(M.path.escape_wildcards(path), pattern), true, true)) do
|
||||
if M.path.exists(p) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return function(startpath)
|
||||
startpath = M.strip_archive_subpath(startpath)
|
||||
return M.search_ancestors(startpath, matcher)
|
||||
for _, pattern in ipairs(patterns) do
|
||||
local match = M.search_ancestors(startpath, function(path)
|
||||
for _, p in ipairs(vim.fn.glob(M.path.join(M.path.escape_wildcards(path), pattern), true, true)) do
|
||||
if M.path.exists(p) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
if match ~= nil then
|
||||
return match
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -136,7 +136,7 @@ describe('lspconfig', function()
|
|||
-- change the working directory to test directory
|
||||
vim.api.nvim_command 'cd ./test/test_dir/a'
|
||||
local cwd = vim.fn.getcwd()
|
||||
eq(true, cwd == lspconfig.util.root_pattern { 'root_marker.txt', 'a_marker.txt' }(cwd))
|
||||
eq(true, cwd == lspconfig.util.root_pattern { 'a_marker.txt', 'root_marker.txt' }(cwd))
|
||||
end)
|
||||
|
||||
it('resolves to root_marker.txt', function()
|
||||
|
|
Loading…
Reference in a new issue