The perfect Neovim setup for Rust.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
as much as I like near them I'll admit it can be difficult to know how to set up and configure a productive environment for writing code so on this video I'm going to share some plugins and configuration to supercharge any of them for working with rust by the end of this video you'll have native auto code completion automatic code formatting whenever you save built-in debugging and a powerful tool for managing versions of your cargo crates for this video I'm using Envy chat as my base configuration this means that all of the configuration code will be written in Lua and the product structure will be tailored to nvchat if you're using another neovim configuration such as Astro you should be able to achieve the same thing with a little bit of translation if you're working from a base implementation however I'd recommend using nvchat fortunately I already have a video on getting set up with nvchat which you can go and watch now if you want to get started make sure you're using the latest version of neovim which as of the time we're recording is version 9.0 I'm using Arch by the way so I'll install it using Pac-Man you can check the running version of new them using the nvim dash dash version command to make this video easier to follow I'm going to start with the base installation of nvchat to do so is as easy as cloning the nvchad repo into our neovim configuration directory now let's open the oven and make sure to enter n for no when it asks you if you want to install the example config you then should see the default plugins being installed after it's done installing the base packages I'm also going to set the theme to Capuchin because I'm addicted to those pastel colors now that we have our base configuration set up the first thing we're going to want to do is to configure an LSP server to use with rust this will provide contextual aware code completion and other tooling the recommended rust LSP server is rust analyzer let's go ahead and install it we can install this using rust up if we want to which is typically my preferred approach however for this video we're going to install it within neovim using Mason to do so navigate over to your freshly downloaded neovim configuration and open it up in Vin you can open up the file Tree by pressing Ctrl and N now let's open up the chatrc.lure file in our custom directory if you don't have one create this yourself and open it up in Vin in here we'll import our custom plugins file which we'll create in this directory using the name plugins.lure next up we can open up this new file and return an empty lure map inside which we'll use for adding our plugin information inside of this map We'll add in Mason again which will extend the default configuration then we'll add the insure installed option for Mason to add rust analyzer to our configuration now let's close and reopen neovim and then call the Mason install all command a window will pop up showing rust analyzer being installed next up we want to change our LSP config so that we load and run rust analyzer heading back over to our custom plugins.luifier let's add in an override for the LSP config so that we can load our custom LSP server what we're doing here is specifying the package name that we're overriding which is the neovim LSP config package and adding in a new configuration function which will load the default NV chat config and our own custom LSP config next let's create a conflicts directory in our custom folder and add in a file called LSP config.lure in this file we're going to set up rust analyzer to work with Neil them we'll first need to import the on attach and capabilities values from the nvchad LSP config to use with our LSP servers then we'll need to import the neovim LSP config I also like to pull in the LSP config utilities package as well now we can add rust analyzer to the LSP config table and using the setup function let's assign the on attach and capabilities values we can also constrain this LSP to only run on Rust file types and to set the project root for any cargo projects to be where the cargo.com lives additionally I like to set the cargo to all features value to true in the configuration as this can help with autocomplete on cargo crates now if I open a project that has the round crate added we can see autocomplete works as expected once I type in the module name one feature I really find helps to speed up my rust development is to enable code formatting whenever I save my file this feature is provided by the official rust Vim plugin to add this plugin let's jump back into our custom plugins lure file and add it to our plugins table we'll also set this to load on file type of rust as well as specifying the plugin's name we should add an init function to the plugin configuration to run lower code when the package is loaded here's the line to enable rust Auto formatting on Save if we jump over to another project here we can see some code that is poorly formatted now I'm going to save the file the code is reorganized as per rust format as well as code completion and auto formatting any decent IDE also provides the ability to debug code from within the text editor we could achieve this in Rust and neovim using another plugin the first thing we need to do is jump over to our custom LSP config and delete our rust analyzer entry yeah sorry about that but if we have both configured then it'll just end up causing conflicts now let's jump on over to our custom plugins file and add in the rust tools plugin just like the others we'll set the file type for this to be rust the rust tools plugin provides a lot of available features for working with rust so I recommend reading the documentation for what else you can achieve with this plugin however for this video we're just going to focus on debugging back in our plugins lure file we're also going to want to set up some custom options so let's go ahead and add in our function to our Ops value for the plugin table in this function we're going to return an import of some custom options we're about to create let's create a new file in our custom configs directory for rust Tools in this file we're going to set up any custom options for the rust tools package especially the LSP server which is just rust analyzer under the hood let's go ahead and import The Familiar on attached and capabilities values and add them to the server block of our options table jumping back over to our custom plugins files let's go ahead and add a config function to the rust tools table where we just call this setup function with the newly provided options whilst setting this up I kept running into an issue where the default LSP config wasn't loaded before the rust tools package was to fix this we can add a dependency of LSP config to our rust tools plugin entry which will force the package manager to load rust tools after LSP config has been loaded now if we open up a rust file we should see LSP is working as it was before and with some more features compared to our previous setup in order for debugging to be enabled we need to install another neovim plugin called the neovim debug adapter protocol let's add this to our custom plugins table and then reinstall our packages finally all we need to do is install the lldb package on our system in order to debug our code I'll install this using Pac-Man because well you know why after everything is installed we can go ahead and debug our code the debug code first open up a file you want to test then we'll call the rust debuggables command which will present a menu for our build and run options let's choose to run our test Target and nothing happens this is because we haven't set up any break points so the code is just running and exiting let's go ahead and add a breakpoint to a line of code we wish to inspect with our debugger selecting the line we care about we can add a breakpoint using the DAP toggle breakpoint command you can see a breakpoint has been added denoted by the capital B next to the line number if we rerun our debug command you'll see we've actually arrived at our breakpoint which is noted by the arrow at the line although this doesn't tell us too much about what is going on in order to inspect our values we can use some of the UI features of nvindap to view what our current variables look like according to the scope entering the following command will open up a sidebar that we can view our locally scoped window with we can then step through our code using the DAP step over command which will allow us to inspect changes to our variables as our code progresses line by line and with that we have some rudimentary debugging and rust although this is a little bit cumbersome to type in any time we want to inspect our local variables to make the debugging experience better we'll go ahead and add some key mappings to open up our windows and add breakpoints heading back on over to our custom config let's add a new file called mappings.lure here we can add in new mappings to simplify our debugging experience let's add in a mapping for toggling a breakpoint using space D and B with this added let's import it into our chat.rc file now jumping back on over to our code we can easily toggle breakpoints using this keybinding let's go ahead and add the same for our Ur sidebar command so we can easily open this up as well we'll bind this to the space d u s command and we'll add a function which calls the same lower code we were using earlier back in the code we're debugging we can now press this key bindings to easily open up a sidebar to inspect the values of variables in our debugging scope there's many more UI options in nvdap that you can use and I'd recommend reading the documentation to see what works for you alternatively if you'd like me to do a more detailed video on nvdap then let me know this next plugin adds greater information and utilities for working with crates via the cargo.tomel with this plugin we're able to see create versions update or upgrade creates to the latest version get autocomplete for create versions and jump to the documentation for a crate with a single command to install this plugin first jump on over to the plugins.lure file and add in the crates plugin entry let's set it to Lazy load on Rust and toml files now we're going to want to override some configuration let's create a config function require the package and call the setup function I was having issues getting create to work when opening up the cargo.com first so I added in a call to the crates.show function to resolve it to get autocomplete to work you'll need to override the default sources for the comp package we can do this easily with another plugin entry and adding a function for the options where we load the default values and add crates to the plugin sources now we have a super powered cargo.com which will give us autocomplete for crate versions and a bunch of other utilities if we jump over to our mappings file we can add a mapping to automatically upgrade our crates and bind it to the space RC U key with this setup you can turn any of them into a Powerhouse for writing rust code I hope this video helps you to have a better rust developer experience and inspire you to try configuring near them for yourself as always thank you for watching and I'll see you next time
Info
Channel: Dreams of Code
Views: 89,015
Rating: undefined out of 5
Keywords: rust, rustlang, rusty, neovim, nvim, vim, neo vim, text editor
Id: mh_EJhH49Ms
Channel Id: undefined
Length: 11min 4sec (664 seconds)
Published: Sun Apr 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.