A plugin to improve your rust experience in neovim.
neovim 0.7
nvim-lspconfig
rust-analyzer
dot
fromgraphviz
(only for crate graph)
using packer.nvim
use 'neovim/nvim-lspconfig'
use 'simrat39/rust-tools.nvim'
-- Debugging
use 'nvim-lua/plenary.nvim'
use 'mfussenegger/nvim-dap'
Look at the configuration information below to get started.
This plugin automatically sets up nvim-lspconfig for rust_analyzer for you, so don't do that manually, as it causes conflicts.
Put this in your init.lua or any lua file that is sourced.
For most people, the defaults are fine, but for advanced configuration, see Configuration.
Example config:
local rt = require("rust-tools")
rt.setup({
server = {
on_attach = function(_, bufnr)
-- Hover actions
vim.keymap.set("n", "<C-space>", rt.hover_actions.hover_actions, { buffer = bufnr })
-- Code action groups
vim.keymap.set("n", "<Leader>a", rt.code_action_group.code_action_group, { buffer = bufnr })
end,
},
})
Expand Macros Recursively
-- Command:
-- RustExpandMacro
require'rust-tools'.expand_macro.expand_macro()
Move Item Up/Down
-- Command:
-- RustMoveItemUp
-- RustMoveItemDown
local up = true -- true = move up, false = move down
require'rust-tools'.move_item.move_item(up)
Hover Actions
Note: To activate hover actions, run the command twice (or your hover keymap if you have hover_with_actions
set to true AND are using vim.lsp.buf.hover()
). This will move you into the window, then press enter on the selection you want. Alternatively, you can set auto_focus
to true in your config and you will automatically enter the hover actions window.
-- Command:
-- RustHoverActions
require'rust-tools'.hover_actions.hover_actions()
Hover Range
Note: Requires rust-analyzer version after 2021-08-02. Shows the type in visual mode when hovering.
-- Command:
-- RustHoverRange
require'rust-tools'.hover_range.hover_range()
Structural Search Replace
-- Command:
-- RustSSR [query]
require'rust-tools'.ssr.ssr(query)
View Crate Graph
-- Command:
-- RustViewCrateGraph [backend [output]]
require'rust-tools'.crate_graph.view_crate_graph(backend, output)
The options shown below are the defaults. You only need to pass the keys to the setup function that you want to be changed, because the defaults are applied for keys that are not provided.
local opts = {
tools = { -- rust-tools options
-- how to execute terminal commands
-- options right now: termopen / quickfix / toggleterm / vimux
executor = require("rust-tools.executors").termopen,
-- callback to execute once rust-analyzer is done initializing the workspace
-- The callback receives one parameter indicating the `health` of the server: "ok" | "warning" | "error"
on_initialized = nil,
-- automatically call RustReloadWorkspace when writing to a Cargo.toml file.
reload_workspace_from_cargo_toml = true,
-- options same as lsp hover / vim.lsp.util.open_floating_preview()
hover_actions = {
-- the border that is used for the hover window
-- see vim.api.nvim_open_win()
border = {
{ "โญ", "FloatBorder" },
{ "โ", "FloatBorder" },
{ "โฎ", "FloatBorder" },
{ "โ", "FloatBorder" },
{ "โฏ", "FloatBorder" },
{ "โ", "FloatBorder" },
{ "โฐ", "FloatBorder" },
{ "โ", "FloatBorder" },
},
-- Maximal width of the hover window. Nil means no max.
max_width = nil,
-- Maximal height of the hover window. Nil means no max.
max_height = nil,
-- whether the hover action window gets automatically focused
-- default: false
auto_focus = false,
},
-- settings for showing the crate graph based on graphviz and the dot
-- command
crate_graph = {
-- Backend used for displaying the graph
-- see: https://graphviz.org/docs/outputs/
-- default: x11
backend = "x11",
-- where to store the output, nil for no output stored (relative
-- path from pwd)
-- default: nil
output = nil,
-- true for all crates.io and external crates, false only the local
-- crates
-- default: true
full = true,
-- List of backends found on: https://graphviz.org/docs/outputs/
-- Is used for input validation and autocompletion
-- Last updated: 2021-08-26
enabled_graphviz_backends = {
"bmp",
"cgimage",
"canon",
"dot",
"gv",
"xdot",
"xdot1.2",
"xdot1.4",
"eps",
"exr",
"fig",
"gd",
"gd2",
"gif",
"gtk",
"ico",
"cmap",
"ismap",
"imap",
"cmapx",
"imap_np",
"cmapx_np",
"jpg",
"jpeg",
"jpe",
"jp2",
"json",
"json0",
"dot_json",
"xdot_json",
"pdf",
"pic",
"pct",
"pict",
"plain",
"plain-ext",
"png",
"pov",
"ps",
"ps2",
"psd",
"sgi",
"svg",
"svgz",
"tga",
"tiff",
"tif",
"tk",
"vml",
"vmlz",
"wbmp",
"webp",
"xlib",
"x11",
},
},
},
-- all the opts to send to nvim-lspconfig
-- these override the defaults set by rust-tools.nvim
-- see https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#rust_analyzer
server = {
-- standalone file support
-- setting it to false may improve startup time
standalone = true,
}, -- rust-analyzer options
-- debugging stuff
dap = {
adapter = {
type = "executable",
command = "lldb-vscode",
name = "rt_lldb",
},
},
}
require('rust-tools').setup(opts)
This plugin draws inspiration from akinsho/flutter-tools.nvim