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 Nov 30, 2024
1 parent e869c7e commit e5a09a5
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 20 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
7 changes: 4 additions & 3 deletions doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ 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 })
<
- 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 })

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 })
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 })
-- 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 })
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 })
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 })
or util.find_git_ancestor(fname)
end,
single_file_support = true,
Expand Down
3 changes: 2 additions & 1 deletion lua/lspconfig/configs/tailwindcss.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ 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) or util.find_package_json_ancestor(fname) or
vim.fs.find('node_modules', { path = fname, upward = true }) or util.find_git_ancestor(
fname
)
end,
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 })
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 @@ -247,14 +247,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 @@ -401,4 +393,9 @@ function M.find_mercurial_ancestor(startpath)
return vim.fs.find('.hg', { path = startpath, upward = true })
end

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

return M

0 comments on commit e5a09a5

Please sign in to comment.