Skip to content

Commit c671605

Browse files
authored
refactor: generalize insert_package_json() #3833
1 parent af4a9f5 commit c671605

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

lsp/tailwindcss.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ return {
117117
}
118118
local fname = vim.api.nvim_buf_get_name(bufnr)
119119
root_files = util.insert_package_json(root_files, 'tailwindcss', fname)
120-
root_files = util.insert_mix_exs(root_files, 'tailwind', fname)
120+
root_files = util.root_markers_with_field(root_files, { 'mix.lock' }, 'tailwind', fname)
121121
on_dir(vim.fs.dirname(vim.fs.find(root_files, { path = fname, upward = true })[1]))
122122
end,
123123
}

lua/lspconfig/util.lua

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,34 @@ function M.root_pattern(...)
4747
end
4848
end
4949

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

59-
if root_with_package then
60-
-- only add package.json if it contains field parameter
61-
for line in io.lines(root_with_package) do
63+
for _, f in ipairs(found or {}) do
64+
-- Match the given `field`.
65+
for line in io.lines(f) do
6266
if line:find(field) then
63-
config_files[#config_files + 1] = vim.fs.basename(root_with_package)
67+
root_files[#root_files + 1] = vim.fs.basename(f)
6468
break
6569
end
6670
end
6771
end
68-
return config_files
69-
end
7072

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

80-
if root_with_mix then
81-
-- only add package.json if it contains field parameter
82-
for line in io.lines(root_with_mix) do
83-
if line:find(field) then
84-
config_files[#config_files + 1] = vim.fs.basename(root_with_mix)
85-
break
86-
end
87-
end
88-
end
89-
return config_files
76+
function M.insert_package_json(root_files, field, fname)
77+
return M.root_markers_with_field(root_files, { 'package.json', 'package.json5' }, field, fname)
9078
end
9179

9280
-- For zipfile: or tarfile: virtual paths, returns the path to the archive.

0 commit comments

Comments
 (0)