diff --git a/doc/lspconfig.txt b/doc/lspconfig.txt index 4a5cce19cf..3afc75bd16 100644 --- a/doc/lspconfig.txt +++ b/doc/lspconfig.txt @@ -376,10 +376,12 @@ below returns a function that takes as its argument the current buffer path. > root_dir = util.root_pattern('pyproject.toml', 'requirements.txt') < -- `util.find_git_ancestor`: a function that locates the first parent directory - containing a `.git` directory. +- `util.find_git_ancestor`: a function which takes a path and an optional flag + that locates the first parent directory containing a `.git` directory or file. > - root_dir = util.find_git_ancestor + root_dir = util.find_git_ancestor(fname, { + exclude_git_files = true + }) - `util.find_node_modules_ancestor`: a function that locates the first parent directory containing a `node_modules` directory. diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 176365d118..007432b606 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -268,10 +268,14 @@ function M.root_pattern(...) end end -function M.find_git_ancestor(startpath) +function M.find_git_ancestor(startpath, options) + local _options = { + exclude_git_files = options and options.exclude_git_files, + } + return M.search_ancestors(startpath, function(path) -- Support git directories and git files (worktrees) - if M.path.is_dir(M.path.join(path, '.git')) or M.path.is_file(M.path.join(path, '.git')) then + if M.path.is_dir(M.path.join(path, '.git')) or (M.path.is_file(M.path.join(path, '.git')) and not _options.exclude_git_files) then return path end end)