Skip to content

Commit

Permalink
refactor: deprecate util.find_node_modules_ancestor
Browse files Browse the repository at this point in the history
Work on #2079.
  • Loading branch information
dundargoc committed Dec 8, 2024
1 parent 47f236c commit 19626a3
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 22 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)'
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)'

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
11 changes: 8 additions & 3 deletions doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,14 @@ below returns a function that takes as its argument the current buffer path.
- `util.find_git_ancestor`: a function that locates the first parent directory
containing a `.git` directory. >
root_dir = util.find_git_ancestor
- `util.find_node_modules_ancestor`: a function that locates the first parent
directory containing a `node_modules` directory. >
root_dir = util.find_node_modules_ancestor
- Locate the first parent dir containing a "node_modules" dir: >lua
vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
<
If you have Nvim 0.10 or newer then >lua
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
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/angularls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local util = require 'lspconfig.util'
-- in order to use your projects configured versions.
-- This defaults to the vim cwd, but will get overwritten by the resolved root of the file.
local function get_probe_dir(root_dir)
local project_root = util.find_node_modules_ancestor(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]

return project_root and (project_root .. '/node_modules') or ''
end
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/astro.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local util = require 'lspconfig.util'

local function get_typescript_server_path(root_dir)
local project_root = util.find_node_modules_ancestor(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
return project_root and (util.path.join(project_root, 'node_modules', 'typescript', 'lib')) or ''
end

Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/glint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ return {
default_config = {
cmd = { 'glint-language-server' },
on_new_config = function(config, new_root_dir)
local project_root = util.find_node_modules_ancestor(new_root_dir)
local project_root = vim.fs.find('node_modules', { path = new_root_dir, upward = true })[1]
-- Glint should not be installed globally.
local node_bin_path = util.path.join(project_root, 'node_modules', '.bin')
local path = node_bin_path .. util.path.path_separator .. vim.env.PATH
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/mdx_analyzer.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local util = require 'lspconfig.util'

local function get_typescript_server_path(root_dir)
local project_root = util.find_node_modules_ancestor(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
return project_root and (util.path.join(project_root, 'node_modules', 'typescript', 'lib')) or ''
end

Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/relay_lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return {
},
root_dir = util.root_pattern('relay.config.*', 'package.json'),
on_new_config = function(config, root_dir)
local project_root = util.find_node_modules_ancestor(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
local node_bin_path = util.path.join(project_root, 'node_modules', '.bin')
local compiler_cmd = { util.path.join(node_bin_path, 'relay-compiler'), '--watch' }
local path = node_bin_path .. util.path.path_separator .. vim.env.PATH
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 @@ -13,7 +13,7 @@ return {
},
root_dir = function(fname)
return util.find_package_json_ancestor(fname)
or util.find_node_modules_ancestor(fname)
or vim.fs.find('node_modules', { path = fname, upward = true })[1]
or util.find_git_ancestor(fname)
end,
single_file_support = true,
Expand Down
7 changes: 4 additions & 3 deletions lua/lspconfig/configs/tailwindcss.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ return {
'postcss.config.cjs',
'postcss.config.mjs',
'postcss.config.ts'
)(fname) or util.find_package_json_ancestor(fname) or util.find_node_modules_ancestor(fname) or util.find_git_ancestor(
fname
)
)(fname) or util.find_package_json_ancestor(fname) or vim.fs.find(
'node_modules',
{ path = fname, upward = true }
)[1] or util.find_git_ancestor(fname)
end,
},
docs = {
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/volar.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local util = require 'lspconfig.util'

local function get_typescript_server_path(root_dir)
local project_root = util.find_node_modules_ancestor(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
return project_root and (util.path.join(project_root, 'node_modules', 'typescript', 'lib')) or ''
end

Expand Down
13 changes: 5 additions & 8 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,6 @@ function M.find_git_ancestor(startpath)
end)
end

function M.find_node_modules_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
if vim.fn.isdirectory(M.path.join(path, 'node_modules')) == 1 then
return path
end
end)
end

function M.find_package_json_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
local jsonpath = M.path.join(path, 'package.json')
Expand Down Expand Up @@ -400,4 +392,9 @@ function M.find_mercurial_ancestor(startpath)
return vim.fs.find('.hg', { path = startpath, upward = true })[1]
end

--- @deprecated use `vim.fs.find('node_modules', { path = startpath, upward = true })[1]` instead
function M.find_node_modules_ancestor(startpath)
return vim.fs.find('node_modules', { path = startpath, upward = true })[1]
end

return M

0 comments on commit 19626a3

Please sign in to comment.