From 7384e7149e79365c96dc623d9fa3444737776982 Mon Sep 17 00:00:00 2001 From: Emilio Ziniades Date: Sat, 18 Nov 2023 09:40:13 +0200 Subject: 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 --- lua/lspconfig/util.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'lua') diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 364bf9a8..176365d1 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -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) -- cgit v1.2.3-70-g09d2