Skip to content

refactor: generalize insert_package_json() #3833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lsp/tailwindcss.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ return {
}
local fname = vim.api.nvim_buf_get_name(bufnr)
root_files = util.insert_package_json(root_files, 'tailwindcss', fname)
root_files = util.insert_mix_exs(root_files, 'tailwind', fname)
root_files = util.root_markers_with_field(root_files, { 'mix.lock' }, 'tailwind', fname)
on_dir(vim.fs.dirname(vim.fs.find(root_files, { path = fname, upward = true })[1]))
end,
}
44 changes: 16 additions & 28 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,46 +47,34 @@ function M.root_pattern(...)
end
end

--- Appends package.json-like files to the `config_files` list if `field`
--- is found in any such file in any ancestor of `fname`.
--- Appends `new_names` to `root_files` if `field` is found in any such file in any ancestor of `fname`.
---
--- NOTE: this does a "breadth-first" search, so is broken for multi-project workspaces:
--- https://github.com/neovim/nvim-lspconfig/issues/3818#issuecomment-2848836794
function M.insert_package_json(config_files, field, fname)
---
--- @param root_files string[] List of root-marker files to append to.
--- @param new_names string[] Potential root-marker filenames (e.g. `{ 'package.json', 'package.json5' }`) to inspect for the given `field`.
--- @param field string Field to search for in the given `new_names` files.
--- @param fname string Full path of the current buffer name to start searching upwards from.
function M.root_markers_with_field(root_files, new_names, field, fname)
local path = vim.fn.fnamemodify(fname, ':h')
local root_with_package = vim.fs.find({ 'package.json', 'package.json5' }, { path = path, upward = true })[1]
local found = vim.fs.find(new_names, { path = path, upward = true })

if root_with_package then
-- only add package.json if it contains field parameter
for line in io.lines(root_with_package) do
for _, f in ipairs(found or {}) do
-- Match the given `field`.
for line in io.lines(f) do
if line:find(field) then
config_files[#config_files + 1] = vim.fs.basename(root_with_package)
root_files[#root_files + 1] = vim.fs.basename(f)
break
end
end
end
return config_files
end

--- Appends mix.exs files to the `config_files` list if `field`
--- is found in any such file in any ancestor of `fname`.
---
--- NOTE: this does a "breadth-first" search, so is broken for multi-project workspaces:
--- https://github.com/neovim/nvim-lspconfig/issues/3818#issuecomment-2848836794
function M.insert_mix_exs(config_files, field, fname)
local path = vim.fn.fnamemodify(fname, ':h')
local root_with_mix = vim.fs.find({ 'mix.lock' }, { path = path, upward = true })[1]
return root_files
end

if root_with_mix then
-- only add package.json if it contains field parameter
for line in io.lines(root_with_mix) do
if line:find(field) then
config_files[#config_files + 1] = vim.fs.basename(root_with_mix)
break
end
end
end
return config_files
function M.insert_package_json(root_files, field, fname)
return M.root_markers_with_field(root_files, { 'package.json', 'package.json5' }, field, fname)
end

-- For zipfile: or tarfile: virtual paths, returns the path to the archive.
Expand Down
Loading