Skip to content

Commit

Permalink
refactor: deprecate util.find_package_json_ancestor
Browse files Browse the repository at this point in the history
Work on #2079.
  • Loading branch information
dundargoc committed Dec 13, 2024
1 parent 124be12 commit f675f8c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 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\.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
Expand Down
16 changes: 12 additions & 4 deletions doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions lua/lspconfig/configs/cssmodules_ls.lua
Original file line number Diff line number Diff line change
@@ -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 = [[
Expand Down
6 changes: 3 additions & 3 deletions lua/lspconfig/configs/pug.lua
Original file line number Diff line number Diff line change
@@ -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 = [[
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/rome.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/tailwindcss.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 6 additions & 10 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit f675f8c

Please sign in to comment.