Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: util.root_pattern prioritises pattern order #2885

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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')
<
Expand Down
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better use vim.fs module

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
Loading