From f675f8c430ae3012f6d140899c2cec3b59e9cb43 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Fri, 13 Dec 2024 14:02:48 +0100 Subject: [PATCH] refactor: deprecate util.find_package_json_ancestor Work on https://github.com/neovim/nvim-lspconfig/issues/2079. --- .github/ci/run_sanitizer.sh | 2 +- doc/lspconfig.txt | 16 ++++++++++++---- lua/lspconfig/configs/cssmodules_ls.lua | 6 +++--- lua/lspconfig/configs/pug.lua | 6 +++--- lua/lspconfig/configs/rome.lua | 2 +- lua/lspconfig/configs/tailwindcss.lua | 2 +- lua/lspconfig/util.lua | 16 ++++++---------- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/.github/ci/run_sanitizer.sh b/.github/ci/run_sanitizer.sh index 8adeaafa9e..9f6fd3580d 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\.find_mercurial_ancestor|util\.find_node_modules_ancestor)' +SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_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/doc/lspconfig.txt b/doc/lspconfig.txt index 0590a5d418..f9ba9bfbd8 100644 --- a/doc/lspconfig.txt +++ b/doc/lspconfig.txt @@ -304,10 +304,18 @@ below returns a function that takes as its argument the current buffer path. vim.fs.root(root_dir, "node_modules") < can be used instead. - - Note: The old `util.find_node_modules_ancestor` API is deprecated and will be removed. -- `util.find_package_json_ancestor`: a function that locates the first parent - directory containing a `package.json`. > - root_dir = util.find_package_json_ancestor + - Note: The old `util.find_node_modules_ancestor` API is deprecated and will + be removed. + +- Locate the first parent dir containing a "package.json" dir: >lua + vim.fs.find('package.json', { path = root_dir, upward = true })[1] +< + If you have Nvim 0.10 or newer then >lua + vim.fs.root(root_dir, "package.json") +< + can be used instead. + - Note: The old `util.find_package_json_ancestor` API is deprecated and will + be removed. < Note: On Windows, `lspconfig` always assumes forward slash normalized paths with capitalized drive letters. diff --git a/lua/lspconfig/configs/cssmodules_ls.lua b/lua/lspconfig/configs/cssmodules_ls.lua index 28973be745..b05d66ff00 100644 --- a/lua/lspconfig/configs/cssmodules_ls.lua +++ b/lua/lspconfig/configs/cssmodules_ls.lua @@ -1,10 +1,10 @@ -local util = require 'lspconfig.util' - return { default_config = { cmd = { 'cssmodules-language-server' }, filetypes = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact' }, - root_dir = util.find_package_json_ancestor, + root_dir = function(fname) + return vim.fs.find('package.json', { path = fname, upward = true })[1] + end, }, docs = { description = [[ diff --git a/lua/lspconfig/configs/pug.lua b/lua/lspconfig/configs/pug.lua index 28f7db61cd..444e98305f 100644 --- a/lua/lspconfig/configs/pug.lua +++ b/lua/lspconfig/configs/pug.lua @@ -1,10 +1,10 @@ -local util = require 'lspconfig.util' - return { default_config = { cmd = { 'pug-lsp' }, filetypes = { 'pug' }, - root_dir = util.find_package_json_ancestor, + root_dir = function(fname) + return vim.fs.find('package.json', { path = fname, upward = true })[1] + end, }, docs = { description = [[ diff --git a/lua/lspconfig/configs/rome.lua b/lua/lspconfig/configs/rome.lua index 831469e46c..09a3e6c435 100644 --- a/lua/lspconfig/configs/rome.lua +++ b/lua/lspconfig/configs/rome.lua @@ -12,7 +12,7 @@ return { 'typescriptreact', }, root_dir = function(fname) - return util.find_package_json_ancestor(fname) + return vim.fs.find('package.json', { path = fname, upward = true })[1] or vim.fs.find('node_modules', { path = fname, upward = true })[1] or util.find_git_ancestor(fname) end, diff --git a/lua/lspconfig/configs/tailwindcss.lua b/lua/lspconfig/configs/tailwindcss.lua index 56ed05b4ea..994e90f614 100644 --- a/lua/lspconfig/configs/tailwindcss.lua +++ b/lua/lspconfig/configs/tailwindcss.lua @@ -109,7 +109,7 @@ return { 'postcss.config.cjs', 'postcss.config.mjs', 'postcss.config.ts' - )(fname) or util.find_package_json_ancestor(fname) or vim.fs.find( + )(fname) or vim.fs.find('package.json', { path = fname, upward = true })[1] or vim.fs.find( 'node_modules', { path = fname, upward = true } )[1] or util.find_git_ancestor(fname) diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 7b894ee385..a5cf67fdff 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -246,18 +246,9 @@ function M.find_git_ancestor(startpath) end) end -function M.find_package_json_ancestor(startpath) - return M.search_ancestors(startpath, function(path) - local jsonpath = M.path.join(path, 'package.json') - if (vim.loop.fs_stat(jsonpath) or {}).type == 'file' then - return path - end - end) -end - function M.insert_package_json(config_files, field, fname) local path = vim.fn.fnamemodify(fname, ':h') - local root_with_package = M.find_package_json_ancestor(path) + local root_with_package = vim.fs.find('package.json', { path = path, upward = true })[1] if root_with_package then -- only add package.json if it contains field parameter @@ -397,4 +388,9 @@ function M.find_node_modules_ancestor(startpath) return vim.fs.find('node_modules', { path = startpath, upward = true })[1] end +--- @deprecated use `vim.fs.find('package.json', { path = startpath, upward = true })[1]` instead +function M.find_package_json_ancestor(startpath) + return vim.fs.find('package.json', { path = startpath, upward = true })[1] +end + return M