Skip to content

Commit

Permalink
fix: util.root_pattern priorities pattern order
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
emilioziniades committed Nov 13, 2023
1 parent b085221 commit 9284887
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,22 @@ end

function M.root_pattern(...)
local patterns = vim.tbl_flatten { ... }
local function matcher(path)
return function(startpath)
startpath = M.strip_archive_subpath(startpath)
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
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
return function(startpath)
startpath = M.strip_archive_subpath(startpath)
return M.search_ancestors(startpath, matcher)
end
end

function M.find_git_ancestor(startpath)
Expand Down
2 changes: 1 addition & 1 deletion test/lspconfig_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9284887

Please # to comment.