diff --git a/doc/lspconfig.txt b/doc/lspconfig.txt index 5a1da207e7..6d30a27e17 100644 --- a/doc/lspconfig.txt +++ b/doc/lspconfig.txt @@ -110,7 +110,7 @@ listed in `:help vim.lsp.start_client()` with the following unique entries: - {on_new_config} - `function(new_config, new_root_dir)` + `function(new_config, new_root_dir, single_file)` Function executed after a root directory is detected. This is used to modify the server configuration (including `cmd` itself). Most commonly, diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua index 532befd10c..e49e75fffa 100644 --- a/lua/lspconfig/configs.lua +++ b/lua/lspconfig/configs.lua @@ -186,7 +186,7 @@ function configs.__newindex(t, config_name, config_def) M.manager = nil end - local make_config = function(root_dir) + local make_config = function(root_dir, single_file) local new_config = tbl_deep_extend('keep', vim.empty_dict(), config) --[[@as lspconfig.Config]] new_config.capabilities = tbl_deep_extend('keep', new_config.capabilities, { workspace = { @@ -195,10 +195,10 @@ function configs.__newindex(t, config_name, config_def) }) if config_def.on_new_config then - pcall(config_def.on_new_config, new_config, root_dir) + pcall(config_def.on_new_config, new_config, root_dir, single_file) end if config.on_new_config then - pcall(config.on_new_config, new_config, root_dir) + pcall(config.on_new_config, new_config, root_dir, single_file) end new_config.on_init = util.add_hook_after(new_config.on_init, function(client, result) diff --git a/lua/lspconfig/manager.lua b/lua/lspconfig/manager.lua index 5d260b414e..0beefdcf64 100644 --- a/lua/lspconfig/manager.lua +++ b/lua/lspconfig/manager.lua @@ -25,11 +25,11 @@ end --- @class lspconfig.Manager --- @field _clients table --- @field config lspconfig.Config ---- @field make_config fun(root_dir: string): lspconfig.Config +--- @field make_config fun(root_dir: string, single_file: boolean): lspconfig.Config local M = {} --- @param config lspconfig.Config ---- @param make_config fun(root_dir: string): lspconfig.Config +--- @param make_config fun(root_dir: string, single_file: boolean): lspconfig.Config --- @return lspconfig.Manager function M.new(config, make_config) return setmetatable({ @@ -200,7 +200,7 @@ end ---@param bufnr integer function M:add(root_dir, single_file, bufnr) root_dir = util.path.sanitize(root_dir) - local new_config = self.make_config(root_dir) + local new_config = self.make_config(root_dir, single_file) local client = self:_get_client_from_cache(root_dir, new_config.name) if not client then diff --git a/lua/lspconfig/server_configurations/rust_analyzer.lua b/lua/lspconfig/server_configurations/rust_analyzer.lua index 6ed471d2d5..e73d25fe4b 100644 --- a/lua/lspconfig/server_configurations/rust_analyzer.lua +++ b/lua/lspconfig/server_configurations/rust_analyzer.lua @@ -70,7 +70,13 @@ return { or util.root_pattern 'rust-project.json'(fname) or util.find_git_ancestor(fname) end, + single_file_support = true, capabilities = register_cap(), + on_new_config = function(config, _, single_file) + if single_file then + config.init_options.detachedFiles = { vim.api.nvim_buf_get_name(0) } + end + end, }, commands = { CargoReload = {