Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust_analyzer): single file support #2785

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions lua/lspconfig/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lua/lspconfig/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ end
--- @class lspconfig.Manager
--- @field _clients table<string,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({
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lua/lspconfig/server_configurations/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this something that should be done automatically, why is it rust-specific?

end
end,
},
commands = {
CargoReload = {
Expand Down
Loading