From e6865ea69c6594fbe5c0cafee52636f0ff521aa1 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Sun, 22 Dec 2024 17:52:26 +0100 Subject: [PATCH] refactor: deprecate util.path.is_descendant Work on https://github.com/neovim/nvim-lspconfig/issues/2079. --- .github/ci/run_sanitizer.sh | 2 +- lua/lspconfig/configs/intelephense.lua | 6 +++++- lua/lspconfig/configs/phan.lua | 6 +++++- lua/lspconfig/configs/phpactor.lua | 6 +++++- lua/lspconfig/configs/rust_analyzer.lua | 6 +++++- lua/lspconfig/configs/smarty_ls.lua | 6 +++++- lua/lspconfig/util.lua | 20 +++++--------------- 7 files changed, 31 insertions(+), 21 deletions(-) diff --git a/.github/ci/run_sanitizer.sh b/.github/ci/run_sanitizer.sh index edcf4b5a98..01065b3495 100644 --- a/.github/ci/run_sanitizer.sh +++ b/.github/ci/run_sanitizer.sh @@ -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 diff --git a/lua/lspconfig/configs/intelephense.lua b/lua/lspconfig/configs/intelephense.lua index 2921d51799..0f10a1a4f0 100644 --- a/lua/lspconfig/configs/intelephense.lua +++ b/lua/lspconfig/configs/intelephense.lua @@ -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' }, @@ -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 = { diff --git a/lua/lspconfig/configs/phan.lua b/lua/lspconfig/configs/phan.lua index 8663c282f4..35981ee7d3 100644 --- a/lua/lspconfig/configs/phan.lua +++ b/lua/lspconfig/configs/phan.lua @@ -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', @@ -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 = { diff --git a/lua/lspconfig/configs/phpactor.lua b/lua/lspconfig/configs/phpactor.lua index 21c85b1d31..5066941fbd 100644 --- a/lua/lspconfig/configs/phpactor.lua +++ b/lua/lspconfig/configs/phpactor.lua @@ -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' }, @@ -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 = { diff --git a/lua/lspconfig/configs/rust_analyzer.lua b/lua/lspconfig/configs/rust_analyzer.lua index 5f592d4007..6ee804244d 100644 --- a/lua/lspconfig/configs/rust_analyzer.lua +++ b/lua/lspconfig/configs/rust_analyzer.lua @@ -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' @@ -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 diff --git a/lua/lspconfig/configs/smarty_ls.lua b/lua/lspconfig/configs/smarty_ls.lua index fd6c212a11..c558bf3c06 100644 --- a/lua/lspconfig/configs/smarty_ls.lua +++ b/lua/lspconfig/configs/smarty_ls.lua @@ -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' }, @@ -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 = { diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index e3e238079f..be7901a410 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -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)() @@ -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])