Skip to content

Commit ed2ccea

Browse files
authored
Merge pull request rust-lang#2259 from jyn514/nvim-config
document how to setup RA for nvim automatically
2 parents 342ced9 + 25eac11 commit ed2ccea

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/doc/rustc-dev-guide/src/building/suggested.md

+29-4
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,35 @@ create a `.vim/coc-settings.json`. The settings can be edited with
120120
[`src/etc/rust_analyzer_settings.json`].
121121

122122
Another way is without a plugin, and creating your own logic in your
123-
configuration. To do this you must translate the JSON to Lua yourself. The
124-
translation is 1:1 and fairly straight-forward. It must be put in the
125-
`["rust-analyzer"]` key of the setup table, which is [shown
126-
here](https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#rust_analyzer).
123+
configuration. The following code will work for any checkout of rust-lang/rust (newer than Febuary 2025):
124+
125+
```lua
126+
lspconfig.rust_analyzer.setup {
127+
root_dir = function()
128+
local default = lspconfig.rust_analyzer.config_def.default_config.root_dir()
129+
-- the default root detection uses the cargo workspace root.
130+
-- but for rust-lang/rust, the standard library is in its own workspace.
131+
-- use the git root instead.
132+
local compiler_config = vim.fs.joinpath(default, "../src/bootstrap/defaults/config.compiler.toml")
133+
if vim.fs.basename(default) == "library" and vim.uv.fs_stat(compiler_config) then
134+
return vim.fs.dirname(default)
135+
end
136+
return default
137+
end,
138+
on_init = function(client)
139+
local path = client.workspace_folders[1].name
140+
local config = vim.fs.joinpath(path, "src/etc/rust_analyzer_zed.json")
141+
if vim.uv.fs_stat(config) then
142+
-- load rust-lang/rust settings
143+
local file = io.open(config)
144+
local json = vim.json.decode(file:read("*a"))
145+
client.config.settings["rust-analyzer"] = json.lsp["rust-analyzer"].initialization_options
146+
client.notify("workspace/didChangeConfiguration", { settings = client.config.settings })
147+
end
148+
return true
149+
end
150+
}
151+
```
127152

128153
If you would like to use the build task that is described above, you may either
129154
make your own command in your config, or you can install a plugin such as

0 commit comments

Comments
 (0)