Closed NicolasGB closed 11 months ago
Hey :wave:
The client name in rustaceanvim is rust-analyzer
. So if you change
lsp.format_on_save({
servers = {
["rust_analyzer"] = { "rust" },
}
})
to
lsp.format_on_save({
servers = {
["rust-analyzer"] = { "rust" },
}
})
it should work :smile:
Hi! Thanks for the quick response.
It does work! Didn't notice the name change checked many things but not that.
Thanks for your work on the plugin, the new explain error command is very nice to have!
Thanks for the feedback :smile:
@mrcjkb how do we do format on save with just a standard rustacean config? im not using lsp zero. this is what i currently have
{
"mrcjkb/rustaceanvim",
ft = { "rust" },
init = function()
vim.g.rustaceanvim = {
server = {
on_attach = function(client, bufnr)
local lsp_map = function(mode, keys, func, desc)
vim.keymap.set(mode, keys, func, { buffer = bufnr, desc = desc })
end
-- rust-lsp
lsp_map("n", "K", function() vim.cmd.RustLsp({ "hover", "actions" }) end, "Rust hover docs")
lsp_map("n", "J", function() vim.cmd.RustLsp("joinLines") end, "Rust join lines")
lsp_map("n", "<Leader>ca", function() vim.cmd.RustLsp("codeAction") end, "Rust Code action")
lsp_map("n", "<Leader>rue", function() vim.cmd.RustLsp("explainError") end, "Rust error explain")
lsp_map("n", "<Leader>rud", function() vim.cmd.RustLsp("openDocs") end, "Rust docs")
lsp_map("n", "<Leader>rum", function() vim.cmd.RustLsp("expandMacro") end, "Rust expand macro")
-- copy from lsp_config
lsp_map("n", "gd", vim.lsp.buf.definition, "Goto definition")
lsp_map("n", "gD", vim.lsp.buf.declaration, "Goto declaration")
lsp_map("n", "gI", vim.lsp.buf.implementation, "Goto implementation")
lsp_map("n", "go", vim.lsp.buf.type_definition, "Goto type definition")
end,
},
}
end,
},
@9mm you could call vim.lsp.buf.format
in the callback of a BufWritePre
autocommand.
See also :h lua-guide-autocommands
.
Thanks, if anyone else wants copy paste this is how i did it inside on_attach
local format_sync_grp = vim.api.nvim_create_augroup("RustaceanFormat", {})
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = bufnr,
callback = function() vim.lsp.buf.format() end,
group = format_sync_grp,
})
Neovim version (nvim -v)
v0.9.4
Operating system/version
ArchLinux
Output of
:checkhealth ferris
Checking for Lua dependencies ~ - OK nvim-lua/plenary.nvim installed. - OK mfussenegger/nvim-dap installed. Checking external dependencies ~ - OK rust-analyzer: found rustup 1.26.0 (2023-05-04) - OK Cargo: found cargo 1.73.0 (9c4383fb5 2023-08-26) - OK rustc: found rustc 1.73.0 (cc66ad468 2023-10-03) - WARNING lldb: not found. Install lldb for extended capabilities. A debug adapter (defaults to: LLDB). Required for debugging features. Checking config ~ - OK vim.g.rustaceanvim is set - OK No errors found in config. Checking for conflicting plugins ~ - OK No conflicting plugins detected.
How to reproduce the issue
Open rust file such as main.rs with the following:
Save the file.
Expected behaviour
The println line should be formatted and indented by RustFmt.
In the minimal config I've added Lsp-Zero and rust-tools.nvim, we can verify that when commenting rustaceanvim and uncomment rust-tools, main gets formatted. But when rustaceanvim is active no formatting happens.
vim.lsp.buf.format()
Does work.Actual behaviour
Not auto-formatting when saving.
The minimal config used to reproduce this issue.