Skip to content

Commit 2a65174

Browse files
authored
fix(configs): eliminate some usages of root_pattern #3820
Problem: `root_pattern` is not necessary for non-glob patterns. Solution: Replace non-glob cases with `vim.fs.root()`..
1 parent e4d1c8b commit 2a65174

File tree

9 files changed

+22
-34
lines changed

9 files changed

+22
-34
lines changed

lsp/elmls.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
--- npm install -g elm elm-test elm-format @elm-tooling/elm-language-server
88
--- ```
99

10-
local util = require 'lspconfig.util'
1110
local api = vim.api
1211

13-
local elm_root_pattern = util.root_pattern 'elm.json'
14-
1512
return {
1613
cmd = { 'elm-language-server' },
1714
-- TODO(ashkan) if we comment this out, it will allow elmls to operate on elm.json. It seems like it could do that, but no other editor allows it right now.
@@ -20,7 +17,7 @@ return {
2017
local fname = api.nvim_buf_get_name(bufnr)
2118
local filetype = api.nvim_buf_get_option(0, 'filetype')
2219
if filetype == 'elm' or (filetype == 'json' and fname:match 'elm%.json$') then
23-
on_dir(elm_root_pattern(fname))
20+
on_dir(vim.fs.root(fname, 'elm.json'))
2421
return
2522
end
2623
on_dir(nil)

lsp/intelephense.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@
2525
--- }
2626
--- ```
2727

28-
local util = require 'lspconfig.util'
29-
3028
return {
3129
cmd = { 'intelephense', '--stdio' },
3230
filetypes = { 'php' },
3331
root_dir = function(bufnr, on_dir)
3432
local fname = vim.api.nvim_buf_get_name(bufnr)
3533
local cwd = assert(vim.uv.cwd())
36-
local root = util.root_pattern('composer.json', '.git')(fname)
34+
local root = vim.fs.root(fname, { 'composer.json', '.git' })
3735

3836
-- prefer cwd if root is a descendant
39-
on_dir(vim.fs.relpath(cwd, root) and cwd or root)
37+
on_dir(root and vim.fs.relpath(cwd, root) and cwd)
4038
end,
4139
}

lsp/kotlin_language_server.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
--- For faster startup, you can setup caching by specifying a storagePath
1717
--- in the init_options. The default is your home directory.
1818

19-
local util = require 'lspconfig.util'
20-
21-
local bin_name = 'kotlin-language-server'
22-
2319
--- The presence of one of these files indicates a project root directory
2420
--
2521
-- These are configuration files for the various build systems supported by
@@ -37,9 +33,9 @@ local root_files = {
3733
return {
3834
filetypes = { 'kotlin' },
3935
root_markers = root_files,
40-
cmd = { bin_name }, -- kotlin-language-server
36+
cmd = { 'kotlin-language-server' },
4137
init_options = {
4238
-- Enables caching and use project root to store cache data.
43-
storagePath = util.root_pattern(unpack(root_files))(vim.fn.expand '%:p:h'),
39+
storagePath = vim.fs.root(vim.fn.expand '%:p:h', root_files),
4440
},
4541
}

lsp/lean3ls.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
--- that plugin fully handles the setup of the Lean language server,
1515
--- and you shouldn't set up `lean3ls` both with it and `lspconfig`.
1616

17-
local util = require 'lspconfig.util'
18-
1917
return {
2018
cmd = { 'lean-language-server', '--stdio', '--', '-M', '4096', '-T', '100000' },
2119
filetypes = { 'lean3' },
@@ -33,8 +31,7 @@ return {
3331
end
3432

3533
on_dir(
36-
util.root_pattern 'leanpkg.toml'(fname)
37-
or util.root_pattern 'leanpkg.path'(fname)
34+
vim.fs.root(fname, { 'leanpkg.toml', 'leanpkg.path' })
3835
or stdlib_dir
3936
or vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
4037
)

lsp/phan.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
---
55
--- Installation: https://github.com/phan/phan#getting-started
66

7-
local util = require 'lspconfig.util'
8-
97
local cmd = {
108
'phan',
119
'-m',
@@ -25,9 +23,9 @@ return {
2523
root_dir = function(bufnr, on_dir)
2624
local fname = vim.api.nvim_buf_get_name(bufnr)
2725
local cwd = assert(vim.uv.cwd())
28-
local root = util.root_pattern('composer.json', '.git')(fname)
26+
local root = vim.fs.root(fname, { 'composer.json', '.git' })
2927

3028
-- prefer cwd if root is a descendant
31-
on_dir(vim.fs.relpath(cwd, root) and cwd or root)
29+
on_dir(root and vim.fs.relpath(cwd, root) and cwd)
3230
end,
3331
}

lsp/phpactor.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
---
55
--- Installation: https://phpactor.readthedocs.io/en/master/usage/standalone.html#global-installation
66

7-
local util = require 'lspconfig.util'
8-
97
return {
108
cmd = { 'phpactor', 'language-server' },
119
filetypes = { 'php' },
1210
root_dir = function(bufnr, on_dir)
1311
local fname = vim.api.nvim_buf_get_name(bufnr)
1412
local cwd = assert(vim.uv.cwd())
15-
local root = util.root_pattern('composer.json', '.git', '.phpactor.json', '.phpactor.yml')(fname)
13+
local root = vim.fs.root(fname, { 'composer.json', '.git', '.phpactor.json', '.phpactor.yml' })
1614

1715
-- prefer cwd if root is a descendant
18-
on_dir(vim.fs.relpath(cwd, root) and cwd or root)
16+
on_dir(root and vim.fs.relpath(cwd, root) and cwd)
1917
end,
2018
}

lsp/rust_analyzer.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
--- Note: do not set `init_options` for this LS config, it will be automatically populated by the contents of settings["rust-analyzer"] per
2222
--- https://github.com/rust-lang/rust-analyzer/blob/eb5da56d839ae0a9e9f50774fa3eb78eb0964550/docs/dev/lsp-extensions.md?plain=1#L26.
2323

24-
local util = require 'lspconfig.util'
25-
2624
local function reload_workspace(bufnr)
2725
local clients = vim.lsp.get_clients { bufnr = bufnr, name = 'rust_analyzer' }
2826
for _, client in ipairs(clients) do
@@ -64,12 +62,12 @@ return {
6462
return
6563
end
6664

67-
local cargo_crate_dir = util.root_pattern 'Cargo.toml'(fname)
65+
local cargo_crate_dir = vim.fs.root(fname, { 'Cargo.toml' })
6866
local cargo_workspace_root
6967

7068
if cargo_crate_dir == nil then
7169
on_dir(
72-
util.root_pattern 'rust-project.json'(fname)
70+
vim.fs.root(fname, { 'rust-project.json' })
7371
or vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
7472
)
7573
return

lsp/smarty_ls.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@
1010
--- npm i -g vscode-smarty-langserver-extracted
1111
--- ```
1212

13-
local util = require 'lspconfig.util'
14-
1513
return {
1614
cmd = { 'smarty-language-server', '--stdio' },
1715
filetypes = { 'smarty' },
1816
root_dir = function(bufnr, on_dir)
1917
local fname = vim.api.nvim_buf_get_name(bufnr)
2018
local cwd = assert(vim.uv.cwd())
21-
local root = util.root_pattern('composer.json', '.git')(fname)
19+
local root = vim.fs.root(fname, { 'composer.json', '.git' })
2220

2321
-- prefer cwd if root is a descendant
24-
on_dir(vim.fs.relpath(cwd, root) and cwd or root)
22+
on_dir(root and vim.fs.relpath(cwd, root) and cwd)
2523
end,
2624
settings = {
2725
smarty = {

lua/lspconfig/util.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ local function escape_wildcards(path)
2424
return path:gsub('([%[%]%?%*])', '\\%1')
2525
end
2626

27+
--- Returns a function which matches a filepath against the given glob/wildcard patterns.
28+
---
29+
--- Also works with zipfile:/tarfile: buffers (via `strip_archive_subpath`).
2730
function M.root_pattern(...)
2831
local patterns = M.tbl_flatten { ... }
2932
return function(startpath)
@@ -44,6 +47,11 @@ function M.root_pattern(...)
4447
end
4548
end
4649

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`.
52+
---
53+
--- NOTE: this does a "breadth-first" search, so is broken for multi-project workspaces:
54+
--- https://github.com/neovim/nvim-lspconfig/issues/3818#issuecomment-2848836794
4755
function M.insert_package_json(config_files, field, fname)
4856
local path = vim.fn.fnamemodify(fname, ':h')
4957
local root_with_package = vim.fs.find({ 'package.json', 'package.json5' }, { path = path, upward = true })[1]

0 commit comments

Comments
 (0)