aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2025-05-10 07:42:35 -0700
committerGitHub <noreply@github.com>2025-05-10 07:42:35 -0700
commitc671605ad09997c552848048b83bb79af0fb150b (patch)
treec0490c30237ec388609d7ec94293d2f344dce494
parentfeat(tailwindcss): add detection for Phoenix projects #3831 (diff)
downloadnvim-lspconfig-c671605ad09997c552848048b83bb79af0fb150b.tar
nvim-lspconfig-c671605ad09997c552848048b83bb79af0fb150b.tar.gz
nvim-lspconfig-c671605ad09997c552848048b83bb79af0fb150b.tar.bz2
nvim-lspconfig-c671605ad09997c552848048b83bb79af0fb150b.tar.lz
nvim-lspconfig-c671605ad09997c552848048b83bb79af0fb150b.tar.xz
nvim-lspconfig-c671605ad09997c552848048b83bb79af0fb150b.tar.zst
nvim-lspconfig-c671605ad09997c552848048b83bb79af0fb150b.zip
refactor: generalize insert_package_json() #3833
-rw-r--r--lsp/tailwindcss.lua2
-rw-r--r--lua/lspconfig/util.lua44
2 files changed, 17 insertions, 29 deletions
diff --git a/lsp/tailwindcss.lua b/lsp/tailwindcss.lua
index 7f9af21a..700ddf85 100644
--- a/lsp/tailwindcss.lua
+++ b/lsp/tailwindcss.lua
@@ -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,
}
diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua
index 979766ae..580beaa4 100644
--- a/lua/lspconfig/util.lua
+++ b/lua/lspconfig/util.lua
@@ -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.