Skip to content

Commit d95c5d9

Browse files
committed
refactor!: deprecate util.root_pattern
The primary (and potentially breaking) change is that `util.root_pattern` traverses once per provided file pattern, while `vim.fs.find` only traverses once. This means that the search will no longer be prioritized by file order, which may break user configurations who unkowingly rely on this behavior. This will also be breaking for language servers that rely on code dependencies that is archived but still needs to be read. More context: #1687. Work on #2079.
1 parent 9ee2e7d commit d95c5d9

File tree

346 files changed

+971
-1042
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

346 files changed

+971
-1042
lines changed

.github/ci/run_sanitizer.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANC
1515
exit 1
1616
fi
1717

18-
SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.path\.join|util\.path\.iterate_parents|util\.path\.traverse_parents|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor)'
18+
SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.path\.join|util\.path\.iterate_parents|util\.path\.traverse_parents|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor|util\.root_pattern)'
1919

2020
if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then
2121
echo

CONTRIBUTING.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ Additionally, these options are often useful:
5353
An example for adding a new config is shown below:
5454

5555
```lua
56-
local util = require 'lspconfig.util'
57-
5856
local function organize_imports()
5957
local params = {
6058
command = 'pyright.organizeimports',
@@ -67,14 +65,14 @@ return {
6765
default_config = {
6866
cmd = { 'pyright-langserver', '--stdio' },
6967
filetypes = { 'python' },
70-
root_dir = util.root_pattern(
71-
'pyproject.toml',
72-
'setup.py',
73-
'setup.cfg',
74-
'requirements.txt',
75-
'Pipfile',
76-
'pyrightconfig.json',
77-
),
68+
root_dir = function(fname)
69+
return vim.fs.dirname(
70+
vim.fs.find(
71+
{ 'pyproject.toml', 'setup.py', 'setup.cfg', 'requirements.txt', 'Pipfile', 'pyrightconfig.json' },
72+
{ path = fname, upward = true }
73+
)[1]
74+
)
75+
end,
7876
single_file_support = true,
7977
settings = {
8078
python = {

doc/lspconfig.txt

+17-13
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ ADDING NEW SERVERS *lspconfig-new*
245245

246246
The steps for adding and enabling a new server configuration are:
247247

248-
1. Define the configuration (see also |vim.fs.root()|): >lua
248+
1. Define the configuration: >lua
249249
local lspconfig = require 'lspconfig'
250250
local configs = require 'lspconfig.configs'
251251

@@ -256,7 +256,7 @@ The steps for adding and enabling a new server configuration are:
256256
cmd = {'/home/neovim/lua-language-server/run.sh'},
257257
filetypes = {'lua'},
258258
root_dir = function(fname)
259-
return lspconfig.util.find_git_ancestor(fname)
259+
return vim.fs.root(fname, '.git')
260260
end,
261261
settings = {},
262262
},
@@ -295,34 +295,38 @@ below returns a function that takes as its argument the current buffer path.
295295
the patterns are specified. >
296296
root_dir = util.root_pattern('pyproject.toml', 'requirements.txt')
297297
298+
- WARNING: `util.root_pattern` is deprecated and will be removed in the future.
299+
Instead, use >lua
300+
vim.fs.dirname(vim.fs.find({ 'pyproject.toml', 'requirements.txt' }, { path = fname, upward = true })[1])
301+
<
298302
- Locate the first parent dir containing a ".git" file or directory: >lua
299-
vim.fs.dirname(vim.fs.find('.git', { path = root_dir, upward = true })[1])
303+
vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
300304
<
301305
If you have Nvim 0.10 or newer then >lua
302-
vim.fs.root(root_dir, ".git")
306+
vim.fs.root(fname, ".git")
303307
<
304308
can be used instead.
305-
- Note: The old `util.find_git_ancestor` API is deprecated and will
309+
- WARNING: The old `util.find_git_ancestor` API is deprecated and will
306310
be removed.
307311
<
308312
- Locate the first parent dir containing a "node_modules" dir: >lua
309-
vim.fs.dirname(vim.fs.find('node_modules', { path = root_dir, upward = true })[1])
313+
vim.fs.dirname(vim.fs.find('node_modules', { path = fname, upward = true })[1])
310314
<
311315
If you have Nvim 0.10 or newer then >lua
312-
vim.fs.root(root_dir, "node_modules")
316+
vim.fs.root(fname, "node_modules")
313317
<
314318
can be used instead.
315-
- Note: The old `util.find_node_modules_ancestor` API is deprecated and will
319+
- WARNING: The old `util.find_node_modules_ancestor` API is deprecated and will
316320
be removed.
317321

318322
- Locate the first parent dir containing a "package.json" dir: >lua
319-
vim.fs.dirname(vim.fs.find('package.json', { path = root_dir, upward = true })[1])
323+
vim.fs.dirname(vim.fs.find('package.json', { path = fname, upward = true })[1])
320324
<
321325
If you have Nvim 0.10 or newer then >lua
322-
vim.fs.root(root_dir, "package.json")
326+
vim.fs.root(fname, "package.json")
323327
<
324328
can be used instead.
325-
- Note: The old `util.find_package_json_ancestor` API is deprecated and will
329+
- WARNING: The old `util.find_package_json_ancestor` API is deprecated and will
326330
be removed.
327331
<
328332
Note: On Windows, `lspconfig` always assumes forward slash normalized paths with
@@ -354,8 +358,8 @@ for some project structures. Example (for Kotlin): >lua
354358
'build.gradle.kts', -- Gradle
355359
}
356360
root_dir = function(fname)
357-
local primary = util.root_pattern(unpack(root_files))(fname)
358-
local fallback = util.root_pattern(unpack(fallback_root_files))(fname)
361+
local primary = vim.fs.root(fname, root_files)
362+
local fallback = vim.fs.root(fname, fallback_root_files)
359363
return primary or fallback
360364
end
361365
<

lua/lspconfig/configs/ada_ls.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'ada_language_server' },
64
filetypes = { 'ada' },
7-
root_dir = util.root_pattern('Makefile', '.git', '*.gpr', '*.adc'),
5+
root_dir = function(fname)
6+
return vim.fs.dirname(vim.fs.find({ 'Makefile', '.git', '*.gpr', '*.adc' }, { path = fname, upward = true })[1])
7+
end,
88
},
99
docs = {
1010
description = [[

lua/lspconfig/configs/agda_ls.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'als' },
64
filetypes = { 'agda' },
7-
root_dir = util.root_pattern('.git', '*.agda-lib'),
5+
root_dir = function(fname)
6+
return vim.fs.dirname(vim.fs.find({ '.git', '*.agda-lib' }, { path = fname, upward = true })[1])
7+
end,
88
single_file_support = true,
99
},
1010
docs = {

lua/lspconfig/configs/aiken.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'aiken', 'lsp' },
64
filetypes = { 'aiken' },
75
root_dir = function(fname)
8-
return util.root_pattern('aiken.toml', '.git')(fname)
6+
return vim.fs.dirname(vim.fs.find({ 'aiken.toml', '.git' }, { path = fname, upward = true })[1])
97
end,
108
},
119
docs = {

lua/lspconfig/configs/alloy_ls.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ return {
33
cmd = { 'alloy', 'lsp' },
44
filetypes = { 'alloy' },
55
root_dir = function(fname)
6-
return vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
6+
return vim.fs.dirname(vim.fs.find({ '.git' }, { path = fname, upward = true })[1])
77
end,
88
single_file_support = true,
99
},

lua/lspconfig/configs/anakin_language_server.lua

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'anakinls' },
@@ -12,8 +10,7 @@ return {
1210
'requirements.txt',
1311
'Pipfile',
1412
}
15-
return util.root_pattern(unpack(root_files))(fname)
16-
or vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
13+
return vim.fs.dirname(vim.fs.find({ unpack(root_files), '.git' }, { path = fname, upward = true })[1])
1714
end,
1815
single_file_support = true,
1916
settings = {

lua/lspconfig/configs/angularls.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local util = require 'lspconfig.util'
2-
31
-- Angular requires a node_modules directory to probe for @angular/language-service and typescript
42
-- in order to use your projects configured versions.
53
-- This defaults to the vim cwd, but will get overwritten by the resolved root of the file.
@@ -25,7 +23,9 @@ return {
2523
-- Check for angular.json since that is the root of the project.
2624
-- Don't check for tsconfig.json or package.json since there are multiple of these
2725
-- in an angular monorepo setup.
28-
root_dir = util.root_pattern 'angular.json',
26+
root_dir = function(fname)
27+
return vim.fs.dirname(vim.fs.find({ 'angular.json' }, { path = fname, upward = true })[1])
28+
end,
2929
},
3030
on_new_config = function(new_config, new_root_dir)
3131
local new_probe_dir = get_probe_dir(new_root_dir)

lua/lspconfig/configs/ansiblels.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'ansible-language-server', '--stdio' },
@@ -24,7 +22,9 @@ return {
2422
},
2523
},
2624
filetypes = { 'yaml.ansible' },
27-
root_dir = util.root_pattern('ansible.cfg', '.ansible-lint'),
25+
root_dir = function(fname)
26+
return vim.fs.dirname(vim.fs.find({ 'ansible.cfg', '.ansible-lint' }, { path = fname, upward = true })[1])
27+
end,
2828
single_file_support = true,
2929
},
3030
docs = {

lua/lspconfig/configs/antlersls.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'antlersls', '--stdio' },
64
filetypes = { 'html', 'antlers' },
7-
root_dir = util.root_pattern 'composer.json',
5+
root_dir = function(fname)
6+
return vim.fs.dirname(vim.fs.find({ 'composer.json' }, { path = fname, upward = true })[1])
7+
end,
88
},
99
docs = {
1010
description = [[

lua/lspconfig/configs/apex_ls.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
filetypes = { 'apexcode' },
6-
root_dir = util.root_pattern 'sfdx-project.json',
4+
root_dir = function(fname)
5+
return vim.fs.dirname(vim.fs.find({ 'sfdx-project.json' }, { path = fname, upward = true })[1])
6+
end,
77
on_new_config = function(config)
88
if not config.cmd and config.apex_jar_path then
99
config.cmd = {

lua/lspconfig/configs/arduino_language_server.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
filetypes = { 'arduino' },
6-
root_dir = util.root_pattern '*.ino',
4+
root_dir = function(fname)
5+
return vim.fs.dirname(vim.fs.find({ '*.ino' }, { path = fname, upward = true })[1])
6+
end,
77
cmd = {
88
'arduino-language-server',
99
},

lua/lspconfig/configs/ast_grep.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'ast-grep', 'lsp' },
@@ -18,7 +16,9 @@ return {
1816
'dart',
1917
'lua',
2018
},
21-
root_dir = util.root_pattern('sgconfig.yaml', 'sgconfig.yml'),
19+
root_dir = function(fname)
20+
return vim.fs.dirname(vim.fs.find({ 'sgconfig.yaml', 'sgconfig.yml' }, { path = fname, upward = true })[1])
21+
end,
2222
},
2323
docs = {
2424
description = [[

lua/lspconfig/configs/astro.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local util = require 'lspconfig.util'
2-
31
local function get_typescript_server_path(root_dir)
42
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
53
return project_root and (project_root .. '/typescript/lib') or ''
@@ -9,7 +7,11 @@ return {
97
default_config = {
108
cmd = { 'astro-ls', '--stdio' },
119
filetypes = { 'astro' },
12-
root_dir = util.root_pattern('package.json', 'tsconfig.json', 'jsconfig.json', '.git'),
10+
root_dir = function(fname)
11+
return vim.fs.dirname(
12+
vim.fs.find({ 'package.json', 'tsconfig.json', 'jsconfig.json', '.git' }, { path = fname, upward = true })[1]
13+
)
14+
end,
1315
init_options = {
1416
typescript = {},
1517
},

lua/lspconfig/configs/atlas.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'atlas', 'tool', 'lsp', '--stdio' },
64
filetypes = {
75
'atlas-*',
86
},
97
root_dir = function(fname)
10-
return util.root_pattern('atlas.hcl')(fname)
8+
return vim.fs.dirname(vim.fs.find({ 'atlas.hcl' }, { path = fname, upward = true })[1])
119
end,
1210
single_file_support = true,
1311
},

lua/lspconfig/configs/autotools_ls.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
local util = require 'lspconfig.util'
2-
31
local root_files = { 'configure.ac', 'Makefile', 'Makefile.am', '*.mk' }
42

53
return {
64
default_config = {
75
cmd = { 'autotools-language-server' },
86
filetypes = { 'config', 'automake', 'make' },
97
root_dir = function(fname)
10-
return util.root_pattern(unpack(root_files))(fname)
8+
return vim.fs.dirname(vim.fs.find({ unpack(root_files) }, { path = fname, upward = true })[1])
119
end,
1210
single_file_support = true,
1311
},

lua/lspconfig/configs/azure_pipelines_ls.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'azure-pipelines-language-server', '--stdio' },
64
filetypes = { 'yaml' },
7-
root_dir = util.root_pattern 'azure-pipelines.yml',
5+
root_dir = function(fname)
6+
return vim.fs.dirname(vim.fs.find({ 'azure-pipelines.yml' }, { path = fname, upward = true })[1])
7+
end,
88
single_file_support = true,
99
settings = {},
1010
},

lua/lspconfig/configs/bacon_ls.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'bacon-ls' },
64
filetypes = { 'rust' },
7-
root_dir = util.root_pattern('.bacon-locations', 'Cargo.toml'),
5+
root_dir = function(fname)
6+
return vim.fs.dirname(vim.fs.find({ '.bacon-locations', 'Cargo.toml' }, { path = fname, upward = true })[1])
7+
end,
88
single_file_support = true,
99
settings = {},
1010
},

lua/lspconfig/configs/ballerina.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local util = require 'lspconfig.util'
2-
31
return {
42
default_config = {
53
cmd = { 'bal', 'start-language-server' },
64
filetypes = { 'ballerina' },
7-
root_dir = util.root_pattern 'Ballerina.toml',
5+
root_dir = function(fname)
6+
return vim.fs.dirname(vim.fs.find({ 'Ballerina.toml' }, { path = fname, upward = true })[1])
7+
end,
88
},
99
docs = {
1010
description = [[

0 commit comments

Comments
 (0)