Add rust-analyzer

This commit is contained in:
Jan Hamal Dvořák 2024-12-29 23:45:13 +01:00
parent c9c3b5678e
commit 1ab09ff0dd
Signed by: mordae
GPG key ID: 1782BCC23EE007B9
2 changed files with 40 additions and 0 deletions

View file

@ -7,6 +7,7 @@ require("config.lsp.pyright")
require("config.lsp.clangd")
require("config.lsp.vls")
require("config.lsp.lua_ls")
require("config.lsp.rust_analyzer")
local api = vim.api
local keymap = vim.keymap.set

View file

@ -0,0 +1,39 @@
local bin_name = "rust-analyzer"
local cmd = { bin_name }
local config = vim.fs.find("Cargo.toml", {
upward = true,
stop = vim.loop.os_homedir(),
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
})
if next(config) then
vim.api.nvim_create_autocmd("FileType", {
pattern = "rust",
callback = function()
vim.lsp.start({
name = "rust-analyzer",
cmd = cmd,
root_dir = config[1],
settings = {
["rust-analyzer"] = {
checkOnSave = {
command = "clippy"
},
check = {
command = "check"
},
},
},
capabilities = vim.lsp.protocol.make_client_capabilities(),
})
end,
})
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.rs",
callback = function()
vim.lsp.buf.format({ timeout_ms = 2000 })
end,
})
end