fix(lsp)!: pass buffer number to root_dir function

Problem:

The root dir function is not passed any context and can only assume the
current buffer is the one being attached.

The main use case is for getting the path of the buffer using
`nvim_buf_get_name`.

Solution:

Pass the buffer number as the first argument.
This commit is contained in:
Lewis Russell
2025-03-08 09:30:36 +00:00
committed by Lewis Russell
parent 3b0fe2659e
commit debabaf884
3 changed files with 13 additions and 10 deletions

View File

@ -678,10 +678,11 @@ Lua module: vim.lsp *lsp-core*
the LSP server will base its workspaceFolders, the LSP server will base its workspaceFolders,
rootUri, and rootPath on initialization. Unused if rootUri, and rootPath on initialization. Unused if
`root_dir` is provided. `root_dir` is provided.
• {root_dir}? (`string|fun(cb:fun(string))`) Directory where the • {root_dir}? (`string|fun(bufnr: integer, cb:fun(root_dir?:string))`)
LSP server will base its workspaceFolders, rootUri, Directory where the LSP server will base its
and rootPath on initialization. If a function, it workspaceFolders, rootUri, and rootPath on
accepts a single callback argument which must be initialization. If a function, it is passed the
buffer number and a callback argument which must be
called with the value of root_dir to use. The LSP called with the value of root_dir to use. The LSP
server will not be started until the callback is server will not be started until the callback is
called. called.

View File

@ -285,10 +285,11 @@ end
--- rootUri, and rootPath on initialization. Unused if `root_dir` is provided. --- rootUri, and rootPath on initialization. Unused if `root_dir` is provided.
--- @field root_markers? string[] --- @field root_markers? string[]
--- ---
--- Directory where the LSP server will base its workspaceFolders, rootUri, and rootPath on --- Directory where the LSP server will base its workspaceFolders, rootUri, and
--- initialization. If a function, it accepts a single callback argument which must be called with --- rootPath on initialization. If a function, it is passed the buffer number
--- the value of root_dir to use. The LSP server will not be started until the callback is called. --- and a callback argument which must be called with the value of root_dir to
--- @field root_dir? string|fun(cb:fun(string)) --- use. The LSP server will not be started until the callback is called.
--- @field root_dir? string|fun(bufnr: integer, cb:fun(root_dir?:string))
--- ---
--- Predicate used to decide if a client should be re-used. Used on all --- Predicate used to decide if a client should be re-used. Used on all
--- running clients. The default implementation re-uses a client if name and --- running clients. The default implementation re-uses a client if name and
@ -462,7 +463,7 @@ local function lsp_enable_callback(bufnr)
if type(config.root_dir) == 'function' then if type(config.root_dir) == 'function' then
---@param root_dir string ---@param root_dir string
config.root_dir(function(root_dir) config.root_dir(bufnr, function(root_dir)
config.root_dir = root_dir config.root_dir = root_dir
vim.schedule(function() vim.schedule(function()
start(config) start(config)

View File

@ -6348,7 +6348,8 @@ describe('LSP', function()
vim.lsp.config('foo', { vim.lsp.config('foo', {
cmd = server.cmd, cmd = server.cmd,
filetypes = { 'foo' }, filetypes = { 'foo' },
root_dir = function(cb) root_dir = function(bufnr, cb)
assert(tmp1 == vim.api.nvim_buf_get_name(bufnr))
vim.system({ 'sleep', '0' }, {}, function() vim.system({ 'sleep', '0' }, {}, function()
cb('some_dir') cb('some_dir')
end) end)