Skip to content

Commit

Permalink
feat(rust): refactor to allow easy user customization and keymaps
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jul 2, 2023
1 parent 61e3ce8 commit a46d476
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 52 deletions.
118 changes: 67 additions & 51 deletions lua/lazyvim/plugins/extras/lang/rust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ return {
end,
},

{
"simrat39/rust-tools.nvim",
lazy = true,
opts = function()
local ok, mason_registry = pcall(require, "mason-registry")
local adapter ---@type any
if ok then
-- rust tools configuration for debugging support
local codelldb = mason_registry.get_package("codelldb")
local extension_path = codelldb:get_install_path() .. "/extension/"
local codelldb_path = extension_path .. "adapter/codelldb"
local liblldb_path = vim.fn.has("mac") == 1 and extension_path .. "lldb/lib/liblldb.dylib"
or extension_path .. "lldb/lib/liblldb.so"
adapter = require("rust-tools.dap").get_codelldb_adapter(codelldb_path, liblldb_path)
end
return {
dap = {
adapter = adapter,
},
tools = {
on_initialized = function()
vim.cmd([[
augroup RustLSP
autocmd CursorHold *.rs silent! lua vim.lsp.buf.document_highlight()
autocmd CursorMoved,InsertEnter *.rs silent! lua vim.lsp.buf.clear_references()
autocmd BufEnter,CursorHold,InsertLeave *.rs silent! lua vim.lsp.codelens.refresh()
augroup END
]])
end,
},
}
end,
config = function() end,
},

-- Correctly setup lspconfig for Rust 🚀
{
"neovim/nvim-lspconfig",
Expand All @@ -50,7 +85,36 @@ return {
opts = {
servers = {
-- Ensure mason installs the server
rust_analyzer = {},
rust_analyzer = {
keys = {
{ "K", "<cmd>RustHoverActions<cr>", desc = "Hover Actions (Rust)" },
{ "<leader>cR", "<cmd>RustCodeAction<cr>", desc = "Code Action (Rust)" },
{ "<leader>dr", "<cmd>RustDebuggables<cr>", desc = "Run Debuggables (Rust)" },
},
settings = {
["rust-analyzer"] = {
cargo = {
allFeatures = true,
loadOutDirsFromCheck = true,
runBuildScripts = true,
},
-- Add clippy lints for Rust.
checkOnSave = {
allFeatures = true,
command = "clippy",
extraArgs = { "--no-deps" },
},
procMacro = {
enable = true,
ignored = {
["async-trait"] = { "async_trait" },
["napi-derive"] = { "napi" },
["async-recursion"] = { "async_recursion" },
},
},
},
},
},
taplo = {},
},
setup = {
Expand All @@ -63,56 +127,8 @@ return {
vim.keymap.set( "n", "<leader>dr", "<cmd>RustDebuggables<cr>", { buffer = buffer, desc = "Run Debuggables (Rust)" })
end
end)
local mason_registry = require("mason-registry")
-- rust tools configuration for debugging support
local codelldb = mason_registry.get_package("codelldb")
local extension_path = codelldb:get_install_path() .. "/extension/"
local codelldb_path = extension_path .. "adapter/codelldb"
local liblldb_path = vim.fn.has("mac") == 1 and extension_path .. "lldb/lib/liblldb.dylib"
or extension_path .. "lldb/lib/liblldb.so"
local user_rust_tools_opts = require("lazyvim.util").opts("rust-tools.nvim")
local rust_tools_opts = vim.tbl_deep_extend("force", user_rust_tools_opts, {
dap = {
adapter = require("rust-tools.dap").get_codelldb_adapter(codelldb_path, liblldb_path),
},
tools = {
on_initialized = function()
vim.cmd([[
augroup RustLSP
autocmd CursorHold *.rs silent! lua vim.lsp.buf.document_highlight()
autocmd CursorMoved,InsertEnter *.rs silent! lua vim.lsp.buf.clear_references()
autocmd BufEnter,CursorHold,InsertLeave *.rs silent! lua vim.lsp.codelens.refresh()
augroup END
]])
end,
},
server = vim.tbl_deep_extend("force", opts, {
settings = {
["rust-analyzer"] = {
cargo = {
allFeatures = true,
loadOutDirsFromCheck = true,
runBuildScripts = true,
},
-- Add clippy lints for Rust.
checkOnSave = {
allFeatures = true,
command = "clippy",
extraArgs = { "--no-deps" },
},
procMacro = {
enable = true,
ignored = {
["async-trait"] = { "async_trait" },
["napi-derive"] = { "napi" },
["async-recursion"] = { "async_recursion" },
},
},
},
},
}),
})
require("rust-tools").setup(rust_tools_opts)
local rust_tools_opts = require("lazyvim.util").opts("rust-tools.nvim")
require("rust-tools").setup(vim.tbl_deep_extend("force", rust_tools_opts or {}, { server = opts }))
return true
end,
taplo = function(_, _)
Expand Down
2 changes: 1 addition & 1 deletion lua/lazyvim/plugins/lsp/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function M.resolve(buffer)

local function add(keymap)
local keys = Keys.parse(keymap)
if keys[2] == vim.NIL or keys[2] == false then
if keys[2] == false then
keymaps[keys.id] = nil
else
keymaps[keys.id] = keys
Expand Down

0 comments on commit a46d476

Please sign in to comment.