Update neovim instructions for neovim 0.11+#2814
Update neovim instructions for neovim 0.11+#2814erfanio wants to merge 5 commits intorust-lang:mainfrom
Conversation
|
Thanks for the PR. If you have write access, feel free to merge this PR if it does not need reviews. You can request a review using |
|
r? rustc-dev-guide |
src/building/suggested.md
Outdated
| local default_root_dir = vim.lsp.config['rust_analyzer'].root_dir | ||
| local default_before_init = vim.lsp.config['rust_analyzer'].before_init | ||
|
|
||
| vim.lsp.config('rust_analyzer', { |
There was a problem hiding this comment.
I'm not very familiar with Lua, so is it correct that there is a difference between _ and - in vim.lsp.config('rust_analyzer', {, cmd = { 'rust-analyzer' },, config.settings["rust-analyzer"] = json.lsp["rust-analyzer"] and vim.lsp.enable('rust_analyzer')?
There was a problem hiding this comment.
Yeah the _ and - here do matter but I agree it's confusing which is which but unfortunately we have to follow the pattern used in https://github.com/neovim/nvim-lspconfig/blob/master/lsp/rust_analyzer.lua unless we want to reimplement everything there.
rust_analyzeris the name of the LSP config. They're using_for consistency across all thenvim-lspconfigconfigs.cmd = { 'rust-analyzer' },is the name of the binaryconfig.settings["rust-analyzer"]does feel arbitrary and looking at the some of the other configs innvim-lspconfigI think it's probably evolved organically without a real convention.
EDIT: config.settings key mirrors the name rust-analyzer uses to query for client configurations https://rust-analyzer.github.io/book/configuration.html
|
fyi, neovim 0.12 (released last week) added skip_comments, so you don't have to manually strip out comments out of the zed json file. I made #2822 before I realized this PR existed! sorry |
|
Thanks @khyperia. I've incorporated your suggestion as well but since 0.12 is very recent, I left a comment for people still running the older version. |
|
@rustbot ready |
There was a problem hiding this comment.
LGTM! but let's wait for confirmation from a Neovim user @ShoyuVanilla, thanks
(I'm not ShoyuVanilla, but) notes from my own testing:
For the second one, slapping this in seems to fix it (probably want to clean this up a bit though to do it a bit more nicely) json.lsp["rust-analyzer"].initialization_options.rustfmt.overrideCommand[1] = init_params.rootPath .. "/" .. json.lsp["rust-analyzer"].initialization_options.rustfmt.overrideCommand[1]for what it's worth, here's my personal neovim config - it has some things that shouldn't be included by default (such as disabling flycheck, due to personal preference of mine), but might be good inspiration for anyone wanting to further customize: click to expandlocal old_rust_before_init = vim.lsp.config.rust_analyzer.before_init
vim.lsp.config('rust_analyzer', {
cmd = function(dispatchers, config)
local exe = "rust-analyzer"
if config.root_dir:find("^/d/rust") then
if not config.cmd_env then
config.cmd_env = {}
end
config.cmd_env["RUSTUP_TOOLCHAIN"] = "nightly"
end
return vim.lsp.rpc.start({ exe }, dispatchers, {
cwd = config.cmd_cwd,
env = config.cmd_env,
detached = config.detached,
})
end,
root_dir = function(bufnr, on_dir)
-- fully override root dir here, do not call the old root_dir
-- reuse existing RA if it exists
local clients = vim.lsp.get_clients { name = 'rust_analyzer' }
if #clients > 0 then
local existing_root = clients[#clients].config.root_dir
if existing_root then
on_dir(existing_root)
return
end
end
-- only scan for .git
local gitroot = vim.fs.root(bufnr, '.git')
if gitroot then
on_dir(gitroot)
end
end,
before_init = function(init_params, config)
if init_params and init_params.rootPath and init_params.rootPath:find("^/d/rust") then
if not config.settings then
config.settings = {}
end
if not config.settings['rust-analyzer'] then
config.settings['rust-analyzer'] = {}
end
local config_path = vim.fs.joinpath(init_params.rootPath, "src/etc/rust_analyzer_zed.json")
local file = io.open(config_path)
if file == nil then
vim.print("missing config file: " .. config_path)
else
local file_contents = file:read("*a")
local json = vim.json.decode(file_contents, { skip_comments = true })
json = json.lsp["rust-analyzer"].initialization_options
json.check.overrideCommand = { "DISABLE" };
json.checkOnSave = false
json.rustfmt.overrideCommand[1] = init_params.rootPath .. "/" .. json.rustfmt.overrideCommand[1]
config.settings['rust-analyzer'] = vim.tbl_deep_extend('force', config.settings['rust-analyzer'],
json)
end
end
if old_rust_before_init ~= nil then
old_rust_before_init(init_params, config)
end
end
}) |
|
I think that's a good point about the toolchain. We could include something like this to override the default toolchain. vim.lsp.config("rust_analyzer", {
cmd_env = { RUSTUP_TOOLCHAIN = "nightly" },But I guess a meta point, wouldn't this affect other editors too? I just assumed everyone is using Re rustfm: I'm a bit confused because this working for me on both macos and linux. The logic seems to be if there are path separators in the rustfmt cmd, join with the |
added some debug printlns to figure out what's going on my system (linux, fwiw) - my debug log prints which then causes this line to be hit and attempting to trace the execution of
when rustfmt is not an absolute path (i.e. removing
|
|
Ah yeah that totally makes sense. I was using I have a PR rust-lang/rust-analyzer#22010 to fix this. Although while looking at this I also discovered that rust-analyzer is not working for any of the files in |
rustand neovim 0.11This doc points to rust-analyzer book instructions for nvim lsp which is also being updated in rust-lang/rust-analyzer#21924