-
Checked other resources
DescriptionI'd like rustaceanvim to format my Rust buffers on save, which was the behaviour I had when using Also, on a tip on the README.md it says
However, I don't exactly know how to "use the vim.g.rustaceanvim.server.on_attach function", but I would like to get the formatting mapping that usually comes with Any help? Example Code-- Astronvim config: lua/plugins/rustaceanvim.lua
vim.g.rustaceanvim = {
server = {
on_attach = function(_, bufnr)
local utils = require "astrocore"
local prefix = "<Leader>r"
-- mappings...
end,
},
tools = {
test_executor = "background",
float_win_config = {
auto_focus = true,
},
},
}
---@type LazySpec
return {
"mrcjkb/rustaceanvim",
version = "^5", -- Recommended
lazy = false, -- This plugin is already lazy
} Neovim version (nvim -v)v0.10.3 Operating system/versionUbuntu 22.04 Output of :checkhealth rustaceanvim==============================================================================
rustaceanvim: require("rustaceanvim.health").check()
Checking for Lua dependencies ~
- OK [mfussenegger/nvim-dap](https://github.com/mfussenegger/nvim-dap) installed.
Checking external dependencies ~
- OK rust-analyzer: found rust-analyzer 1.83.0 (90b35a6 2024-11-26)
- OK Cargo: found cargo 1.83.0 (5ffbef321 2024-10-29)
- OK rustc: found rustc 1.83.0 (90b35a623 2024-11-26)
- OK cargo-nextest: found cargo-nextest 0.9.87 (fee71ad7b 2024-12-17)
- OK debug adapter: found codelldb
Checking config ~
- OK No errors found in config.
Checking for conflicting plugins ~
- OK No conflicting plugins detected.
Checking for tree-sitter parser ~
- OK tree-sitter parser for Rust detected.
- OK .vscode/settings.json loaded without errors. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Realized that I can add something like if client:supports_method "textDocument/formatting" then
-- Format the current buffer on save
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = bufnr,
callback = function() vim.lsp.buf.format { bufnr = bufnr, id = client.id } end,
})
-- Add mapping for formatting
utils.set_mappings {
n = {
["<Leader>lf"] = { function() vim.lsp.buf.format { async = true } end, desc = "Format buffer" },
},
}
end on my However, I still wonder if there's an easy how to keymaps and other capabilities that lspconfig sets up automatically? I'd rather not have my config file be so long. |
Beta Was this translation helpful? Give feedback.
-
neither rustaceanvim, lspconfig or mason implement format on save. It must be another plugin, like lsp-zero. See also: |
Beta Was this translation helpful? Give feedback.
-
You're right! Hadn't noticed that it was AstroNvim that was configuring the format on save feature (I'm a bit rusty when it comes to nvim config). For anyone that might find themselves in my situation, simply use AstroNvim's on_attach = function(client, bufnr)
local astrolsp = require "astrolsp"
-- Run AstroNvim default `on_attach` function to get regular LSP config
astrolsp.on_attach(client, bufnr)
-- other config... |
Beta Was this translation helpful? Give feedback.
You're right! Hadn't noticed that it was AstroNvim that was configuring the format on save feature (I'm a bit rusty when it comes to nvim config).
For anyone that might find themselves in my situation, simply use AstroNvim's
on_attach
function in rustaceanvim's config, like so: