Skip to content

Commit

Permalink
refactor: deprecate util.path.is_descendant
Browse files Browse the repository at this point in the history
Work on neovim#2079.
  • Loading branch information
dundargoc committed Dec 22, 2024
1 parent 9204642 commit e6865ea
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/ci/run_sanitizer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANC
exit 1
fi

SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.path\.join|util\.path\.iterate_parents|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor)'
SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.path\.join|util\.path\.iterate_parents|util\.path\.is_descendant|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor)'

if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then
echo
Expand Down
6 changes: 5 additions & 1 deletion lua/lspconfig/configs/intelephense.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local util = require 'lspconfig.util'

local function is_descendant(root, path)
return vim.startswith(vim.fs.normalize(path), vim.fs.normalize(root))
end

return {
default_config = {
cmd = { 'intelephense', '--stdio' },
Expand All @@ -9,7 +13,7 @@ return {
local root = util.root_pattern('composer.json', '.git')(pattern)

-- prefer cwd if root is a descendant
return util.path.is_descendant(cwd, root) and cwd or root
return is_descendant(cwd, root) and cwd or root
end,
},
docs = {
Expand Down
6 changes: 5 additions & 1 deletion lua/lspconfig/configs/phan.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local util = require 'lspconfig.util'

local function is_descendant(root, path)
return vim.startswith(vim.fs.normalize(path), vim.fs.normalize(root))
end

local cmd = {
'phan',
'-m',
Expand All @@ -23,7 +27,7 @@ return {
local root = util.root_pattern('composer.json', '.git')(pattern)

-- prefer cwd if root is a descendant
return util.path.is_descendant(cwd, root) and cwd or root
return is_descendant(cwd, root) and cwd or root
end,
},
docs = {
Expand Down
6 changes: 5 additions & 1 deletion lua/lspconfig/configs/phpactor.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local util = require 'lspconfig.util'

local function is_descendant(root, path)
return vim.startswith(vim.fs.normalize(path), vim.fs.normalize(root))
end

return {
default_config = {
cmd = { 'phpactor', 'language-server' },
Expand All @@ -9,7 +13,7 @@ return {
local root = util.root_pattern('composer.json', '.git', '.phpactor.json', '.phpactor.yml')(pattern)

-- prefer cwd if root is a descendant
return util.path.is_descendant(cwd, root) and cwd or root
return is_descendant(cwd, root) and cwd or root
end,
},
docs = {
Expand Down
6 changes: 5 additions & 1 deletion lua/lspconfig/configs/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ local function reload_workspace(bufnr)
end
end

local function is_descendant(root, path)
return vim.startswith(vim.fs.normalize(path), vim.fs.normalize(root))
end

local function is_library(fname)
local user_home = vim.fs.normalize(vim.env.HOME)
local cargo_home = os.getenv 'CARGO_HOME' or user_home .. '/.cargo'
Expand All @@ -25,7 +29,7 @@ local function is_library(fname)
local toolchains = rustup_home .. '/toolchains'

for _, item in ipairs { toolchains, registry, git_registry } do
if util.path.is_descendant(item, fname) then
if is_descendant(item, fname) then
local clients = util.get_lsp_clients { name = 'rust_analyzer' }
return #clients > 0 and clients[#clients].config.root_dir or nil
end
Expand Down
6 changes: 5 additions & 1 deletion lua/lspconfig/configs/smarty_ls.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local util = require 'lspconfig.util'

local function is_descendant(root, path)
return vim.startswith(vim.fs.normalize(path), vim.fs.normalize(root))
end

return {
default_config = {
cmd = { 'smarty-language-server', '--stdio' },
Expand All @@ -9,7 +13,7 @@ return {
local root = util.root_pattern('composer.json', '.git')(pattern)

-- prefer cwd if root is a descendant
return util.path.is_descendant(cwd, root) and cwd or root
return is_descendant(cwd, root) and cwd or root
end,
settings = {
smarty = {
Expand Down
20 changes: 5 additions & 15 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,8 @@ M.path = (function()
end
end

local function is_descendant(root, path)
if not path then
return false
end

local function cb(dir, _)
return dir == root
end

local dir, _ = traverse_parents(path, cb)

return dir == root
end

return {
traverse_parents = traverse_parents,
is_descendant = is_descendant,
}
end)()

Expand Down Expand Up @@ -348,6 +333,11 @@ M.path.path_separator = vim.fn.has('win32') == 1 and ';' or ':'
--- @deprecated use `vim.fs.parents(path)` instead
M.path.iterate_parents = vim.fs.parents

--- @deprecated use `vim.startswith(vim.fs.normalize(path), vim.fs.normalize(root))` instead
function M.path.is_descendant(root, path)
return vim.startswith(vim.fs.normalize(path), vim.fs.normalize(root))
end

--- @deprecated use `vim.fs.dirname(vim.fs.find('.hg', { path = startpath, upward = true })[1])` instead
function M.find_mercurial_ancestor(startpath)
return vim.fs.dirname(vim.fs.find('.hg', { path = startpath, upward = true })[1])
Expand Down

0 comments on commit e6865ea

Please sign in to comment.