rust better

This commit is contained in:
RingOfStorms (Joshua Bell) 2023-07-16 19:38:55 -05:00
parent 8ce76c7c91
commit f059534f62
4 changed files with 323 additions and 270 deletions

View file

@ -1,18 +1,21 @@
function prereqs()
local output = vim.fn.system({
"which",
"rust-analyzer",
"&&",
"rust-analyzer",
"--version",
})
if output == nil or output == "" or string.find(output, "not installed for the toolchain") then
print("Installing rust-analyzer globally with rustup")
vim.fn.system({
"rustup", "component", "add", "rust-analyzer"
})
end
end
local servers = {
rust_analyzer = {
-- rust
-- to enable rust-analyzer settings visit:
-- https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/generated_config.adoc
["rust-analyzer"] = {
cargo = {
allFeatures = true,
},
checkOnSave = {
allFeatures = true,
command = "clippy",
},
},
},
-- rust_analyzer = USES RUST_TOOLS INSTEAD, SEE BOTTOM OF THIS FILE
tsserver = {
-- typescript/javascript
},
@ -67,47 +70,6 @@ local servers = {
-- LSP config
-- Took lots of inspiration from this kickstart lua file: https://github.com/hjr3/dotfiles/blob/main/.config/nvim/init.lua
return {
{
-- Autocompletion
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
},
},
{
"williamboman/mason.nvim",
cmd = {
"Mason",
"MasonUpdate",
"MasonInstall",
"MasonInstallAll",
"MasonUninstall",
"MasonUninstallAll",
"MasonLog",
},
build = ":MasonUpdate",
opts = {},
},
{
"williamboman/mason-lspconfig.nvim",
},
{ "folke/neodev.nvim", opts = {} },
{
"neovim/nvim-lspconfig",
dependencies = { "nvim-telescope/telescope.nvim" },
config = function()
local config = require("lspconfig")
local util = require("lspconfig/util")
local mason_lspconfig = require("mason-lspconfig")
local cmp = require("cmp")
local luasnip = require("luasnip")
-- LSP
-- This function gets run when an LSP connects to a particular buffer.
local on_attach = function(client, bufnr)
local nmap = function(keys, func, desc)
@ -139,6 +101,50 @@ return {
end
end
return {
{
"lvimuser/lsp-inlayhints.nvim",
},
{
-- Autocompletion
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
--"Saecki/crates.nvim", -- SEE plugins/rust-tools.lua
},
},
{
"williamboman/mason.nvim",
cmd = {
"Mason",
"MasonUpdate",
"MasonInstall",
"MasonInstallAll",
"MasonUninstall",
"MasonUninstallAll",
"MasonLog",
},
build = ":MasonUpdate",
opts = {},
},
{
"williamboman/mason-lspconfig.nvim",
},
{
"neovim/nvim-lspconfig",
dependencies = { "nvim-telescope/telescope.nvim" },
config = function()
local config = require("lspconfig")
local util = require("lspconfig/util")
local mason_lspconfig = require("mason-lspconfig")
local cmp = require("cmp")
local luasnip = require("luasnip")
-- LSP
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)
@ -155,7 +161,7 @@ return {
mason_lspconfig.setup_handlers({
function(server_name)
require("lspconfig")[server_name].setup({
config[server_name].setup({
flags = flags,
capabilities = capabilities,
on_attach = on_attach,
@ -207,6 +213,7 @@ return {
{ nane = "buffer", priority = 7 },
{ name = "luasnip", priority = 6 },
{ name = "path" },
{ name = "crates" },
},
sorting = {
priority_weight = 1,
@ -225,4 +232,18 @@ return {
})
end,
},
{ "folke/neodev.nvim", opts = {} }, -- lua stuff
{ -- Rust tools
"simrat39/rust-tools.nvim",
build = prereqs,
opts = {
server = {
on_attach = on_attach
},
},
--config = function(_, opts)
--require('rust-tools').setup(opts)
--end
},
{ "Saecki/crates.nvim", tag = "v0.3.0", dependencies = { "nvim-lua/plenary.nvim" }, opts = {} },
}

View file

@ -23,14 +23,42 @@ return {
-- config variable is the default definitions table for the setup function call
local null_ls = require("null-ls")
-- Custom rust formatter: genemichaels first, then rustfmt, nightly if experimental
local rust_formatter_genemichaels = {
name = "rust_formatter_genemichaels",
method = null_ls.methods.FORMATTING,
filetypes = { "rust" },
generator = null_ls.formatter({
command = "genemichaels",
args = { '-q' },
to_stdin = true,
}),
}
-- ╰─ cat src/main.rs| rustfmt --emit=stdout --edition=2021 --color=never
local rust_formatter_rustfmt = {
name = "rust_formatter_rustfmt",
method = null_ls.methods.FORMATTING,
filetypes = { "rust" },
generator = null_ls.formatter({
command = "rustfmt",
args = { '--emit=stdout', "--edition=$(grep edition Cargo.toml | awk '{print substr($3,2,length($3)-2)}')",
'--color=never' },
to_stdin = true,
}),
}
null_ls.register(rust_formatter_genemichaels)
null_ls.register(rust_formatter_rustfmt)
-- Check supported formatters and linters
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics
config.sources = {
-- Set a formatter
null_ls.builtins.formatting.prettier, -- typescript/javascript
null_ls.builtins.formatting.stylua,
null_ls.builtins.formatting.rustfmt,
null_ls.builtins.formatting.stylua, -- lua
--null_ls.builtins.formatting.rustfmt, -- rust
rust_formatter_genemichaels, -- order matters, we run genemichaels first then rustfmt
rust_formatter_rustfmt,
null_ls.builtins.formatting.black, -- python
-- null_ls.builtins.code_actions.proselint, -- TODO looks interesting
null_ls.builtins.code_actions.cspell.with({

View file

@ -1,5 +0,0 @@
return {
"simrat39/rust-tools.nvim",
event = "BufEnter *.rs",
dependencies = { "mason-lspconfig.nvim", "lvimuser/lsp-inlayhints.nvim" },
}

View file

@ -0,0 +1,9 @@
return {
{
"simrat39/rust-tools.nvim",
event = "BufEnter *.rs",
dependencies = { "mason-lspconfig.nvim", "lvimuser/lsp-inlayhints.nvim" },
opts = {},
},
{ "Saecki/crates.nvim", tag = "v0.3.0", dependencies = { "nvim-lua/plenary.nvim" }, opts = {} },
}