How To Setup Linting And Formatting In Neovim To Replace null-ls

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be sharing with you how to set up linting and formatting in your neovim config I'm going to be using conform envm for formatting and enm lint for linting in this video as many of you already know nonetheless which is the plugin I had been previously using to do this has been recently archived this won't be a complete replacement for nulles as nulles has some extra features to offer but it still should work pretty well I'll have a link in the description to my updated neovim config with the changes applied as well as to a blog post with all of the code you might need to follow along all right with that said let's get started all right so I've opened up my neovim config here if you haven't seen my previous videos this file you're looking at is the init.lua file which is the primary file that neovim looks at whenever it starts the second line loads hosan la which corresponds to this lazy. L file you'll see here on the left hand side I'm going to open this up if you haven't used the lazy. and B plug-in manager before I'd highly recommend you check out my video on everything you need to know to set this up for a quick recap these first 11 lines here bootstrap lazy so that when neovim starts up if lazy. envm is not already installed it'll get automatically installed and then down here I'm first loading the lazy. mm plugin and then I'm calling it setup function and the first argument here is a table with two different folders that each contain plugins spec so configurations for all of my different plugins if I open up the file explorer and I open up this plugins directory and you'll see here A bunch of different files for different plug-in configurations and then I have this other LSP folder that has files for setting up and configuring LSP related plugins in this video I'm going to be swapping out nulls so I'm going to delete this nulls file I'm going to go ahead and do that here I use the DC command built into nbim tree to do this now I'm going to open up this mason. file in my previous video on setting up LSP for neovim I showed you guys how to put this file together first thing here is the short plugin URL that tells lazy. enim to install this plugin for us essentially we use Mason to install language servers but you can also use it to install linters and formatters which we'll be needing for this setup now if I open up the Mason UI by doing colon and Mason you'll see here that if I press two you'll see a list of installed language servers which I'm using in my setup if I do four you'll also see a list of installed linters and if you press five then you'll see a list of installed formatters and available formatters as well I'm going to close this by pressing Q now down here I'm also installing Mason LSP config which makes it easier to automatically installed language servers that we need and I'm using Mason nulls to automatically install linters and formatters that we needed for nth Less in this config function we set all of this up we load Mason and then we load Mason LSP config and Mason nonetheless then down here in the setup function I configure some icons for the Mason UI and then in this Mason LSP config do setup I use insure installed and a list of language servers that I want Mason to automatically install whenever neovim starts up now I do the same thing down here with Mason NLS you'll see here a list of insure installed with formatters and linters that we want Mason to automatically install but because I'm not going to be using nulls anymore I'm going to have to remove this I'm going to remove these nine lines and then go back up here and I'm going to remove m nth less and this import statement as well and I'm going to save this file now I'm going to open up the lazy UI by doing Poland lazy and you'll see here under clean lazy. NM notices that we need to remove NS and Mason Ness to remove this we can do uppercase X now to close this UI I'm going to execute Q now I'm going to open up the file explorer I'm going to close this LSP directory I'm going to add a new file under plugins I do this by pressing a which is a built-in envm tree command and I'm going to call this formatting. bla and press enter I'm going to open up this file and close the file explorer and I'm going to return a table here to set up the plugin spec for conform envm the first thing we're going to need here is the short plugin URL for this plugin which looks like this and then I'm going to add an event property to Lazy load this plugin on a specific set of neovim events this is because we don't don't really need formatting as soon as neovim starts but rather we need it when we're actually working inside of a buffer so the first thing I'm going to add here is buff read pre this essentially gets executed whenever we're opening an already existing file and I also want to add buff new file which will trigger when we're opening a buffer for a file that doesn't already exist so whenever these two events trigger the conform envm plugin will load when this plugin loads we want to run a config function and the first thing I want to do here is load the conform plugin after I require this plugin I'm going to call its setup function like so and inside of the setup function we want to pass a Lua table inside of this table we'll have a property called four Matters by ft which will be assigned to a Lu table and inside we'll specify file types so for example I'm going to add JavaScript here and this is going to be equal to another table and inside I want to specify the name of a formatter that conform supports so for JavaScript I want to use prettier I'm just going to put prettier in here all right so I've opened up the conform enm repository so you guys can see a list of all of the different formatters that we have available if I scroll down here you'll see here a list of all the different formatters we have available for example we have the prettier formatter which we just configured for JavaScript so if you're wondering what formatters you can use you can take a look at this list and use the name that shows up here now each of these formatters have a specific configuration to see the configuration itself we can go up here and you can go into this lu/ conform folder and then open up this formatters folder you can see examples of the configurations for all of the different formatters we have available for example for python projects you can check out black. Lua and this is the configuration for that specific formatter you can also Define your own custom configuration if I go back up here to the home and go down here to the read me over here in this options section you'll see that one of the properties in the setup function is this four matters property and inside of here you can define a custom formatter configuration where you define the command that will get executed to run the format and a bunch of other options that you can set up as well I'm not using this feature but just know that you can also set up a custom configuration for a formatter that isn't already supported by conform envm I also use Pryer to format a bunch of other different types of files that are related for working on web development projects so I'm going to go ahead and add those down here I also want to add formatting for my Lua files so down here I'm going to add Lua is equal to sty Lua which is a formatter that is already supported by con. envm and then for another example I'm going to add python this is going to be equal to a table and then I'm going to add two formatters for python I'm going to add isort to help with organizing python Imports and I'm going to add black as well how this works is that conform n we'll execute iort first and then we'll execute black If instead I were to do something like this and make this a subtable what conform envm will do instead is choose the first available formatter out of the two I don't want to do that so I'm going to undo this I'm going to save this file now I need to make sure that I've installed these formatters with Mason so I'm going to open up the Mason UI with colon Mason and you'll see here that I already have black iort prettier and sty Lua installed if you're missing one then you can go down here to available hover over one and then press I to install it it'll get added to the installed list I'm going to uninstall it by doing uppercase X and then I'm going to close the Mason UI by doing Q now the next thing I want to do is set up formatting on Save conform makes this really easy we just need to add a new property down here it's going to be called format on Save equal to a table down here you can add some options I'm going to add LSP fallback is equal to true this means that if a formatter isn't available for the current file type it'll try to fall back to formatting through the LSP and then in the documentation they also recommend to set async to false this means that formatting won't be a synchronous and because it won't be asynchronous we'll specify a timeout in milliseconds and I'm going to set this to 500 the default is 1,000 which is 1 second if you set async to true then the timeout value will be ignored I'm going to save this now the last thing I want to do here is to add a key map for formatting so down here I'm going to do vm. keymap doset and then the first argument will be a table here we'll do n for normal mode and V for visual mode and then I'm going to do leader m for the key map you can do whatever you like here I'm doing lader MP short for make pretty and then I'm going to define a function that will execute whenever we trigger this key map in this function we'll do conform do format and we'll pass some options to this I'm going to do the same as before so LSP fallback is equal to True async is equal to false and timeout milliseconds is equal to to 500 now in normal mode this key map will apply to the whole file but because conform enim supports range formatting in visual mode the keymap will apply formatting to whatever you have selected the range formatting has been a bit Hit or Miss for me and it can depend on whether or not your formatter supports it but it's a feature that is available even though I don't use it as much I'm also going to add a description to this this will be format file or range and the range will be in visual mode and I'm going to save this now so that we don't need to exit out of neovim I'm going to open up the lazy y by doing colon lazy you'll see here that conform NVM isn't installed so I'm going to press capital I to install it I'm going to press Q to close the UI and then I'm going to do colon lazy load con. envm so that we load this plugin and the config function runs now you'll see here that for example if I add some extra lines here and then I save this file conform enm will automatically format it with this styu a formatter which we've specified as the formatter for Lua files these formatters should respect configuration files as well I'm going to open up a new vertical split here and then open the file explorer down here you'll see that I have a style lu. toml file which I use for configuring the formatting for my Lua config I'm going to open this up and close the file explorer let's say for example that I change this inet width to four and I save this file and then I go over here and I save it I'm going to get the new indentation applied I'm going to change this back to two save this file go back here and let's say I want to use my keymap I can do leader MP and it'll run the formatter on the current file I'm going to save this file now for another example I've opened up a react project here with typescript I'm going to open up the prettier config file for this project I'm going to do a split here open the file explorer and down here you'll see a prettier RC file I'm going to open this up you'll see here that I set up single quotes to false so everything is using double quotes by default if I change this to True save this file move over here and then select one of these lines with visual mode and then apply the key map leader MP it'll apply the formatting only to that line and that's the range formatting at work if I do it without a selection and I do leader MP it will apply to the whole file I'm going to change this back to true to false and then I'm going to save this file right you guys now let's add linting to the config as well I'm going to close this split and open the file EXP Explorer then I'm going to go up here to the plugins folder I'm going to add a new file by pressing a and I'm going to call this lining. Lua I'm going to open this up close the file explorer and then in here I'm going to return a table the first thing we're going to add is the short plug-in URL for enim lint I'm going to add an event property here and again this will have buff read pre and buff new file because we only need linting when we're working on a buffer and then I'm going to add a config function first thing I'll do here is load the plugin like so and then to specify the file types where we want to use linting we can do lint do linters uncore byor ft for file type and this will be equal to a table and then we'll list out all of the file types so for example example again I'm going to do JavaScript this will be equal to another table and I want to use eslor d as the linter for JavaScript files as you can see this is a very similar format to what we did with conform envm and that's because the conform envm config and API is inspired by envm lint again I've opened up the repository for envm lint and in here you'll see a list of all of the different linters that you can use are supported by default again each of these will have a default configuration that is set up by envm lint for example we'll have one for eslor D and if we want to use it we'll have to use the name that you see here on the right hand side if I go up here and I go to Lua lint linters and then in here again you'll have a list of all of the different configurations for our linters and I click on let's see let's find eslin here it is um es linore D I'm going to click on it and then you'll see in here a table with all of the different properties that you can use to configure a linter with envm lint this CMD property will Define what needs to be executed for the linter to run if I go back to the home and we take a look at the readme you'll see in the section for custom linters that you can Define your own configuration with all of these different properties to set up a linter if it isn't already supported by envm lint you probably won't need to do this as a lot of linters are already supported if you want to customize a built-in linter you can take a look at this section here and you'll see that you have to load the linter and then modify one of its properties like the args property for example all right so now I'm going to add the other file types I'm going to use eslint with of course this will vary depending ending on what type of projects you work on and then I'm also going to add linting for python so I'm going to do python is equal to Pi lint there's several different linters you can use with python I don't work with python very often I just wanted to add another example for you guys and I'm going to save this file again you need to make sure that you've installed these linters through Mason some of the linters supported by envm lint might not be in Mason and you'll have to install it in your system through another method ES _ D and pilent are both available through Mason so if I open the Mason UI with colon Mason and I press 4 you'll see that I have ESN D and pilent already installed again you can look for a lter you might be missing and you press I to install it once it's done if you want to remove it you can press capital x I'm going to press Q to close this UI now the next thing we need to do is set up an auto command that will execute on different neovim events and Trigger linting to do this the first thing I want to do is to create an autoc command Group which are used for grouping together Auto commands in neovim I'm going to call this lint uncore a group and it's going to be equal to vm. api. envm create enmore create all group and then the first parameter is the name I'm going to call this lint and then I'm going to pass a table with options I'm going to set clear to true so that when we get this autoc command group any pre-existing Auto commands within it will get cleared now after defining the group we can create the auto command this will be vm. api. enmore create autoc CMD and then the first parameter will be a list of neovim events that we want to use to trigger linting if you're wondering where to find a list of different neovim events you can use you can do colon H events and in here you'll see the help page with a list of all of the different neim events you can use I'm going to close this in here the first event I'm going to use is buff enter this means that whenever we open a new buffer or we move the cursor into it it'll trigger linting I also want to trigger linting after I write to a file so I'm going to do buff write post and I also want to trigger linting whenever I exit insert mode so I'm going to do insert leave then I'm going to pass a another table as an argument and the group is going to be the lint all group and then we'll Define a callback function that will execute whenever one of the events are triggered in this function we'll call lint do TR lint so the lint plugin which we load loed we'll try to execute linting for the current buffer and this will happen when we enter a buffer when we write to the buffer or we exit insert mode you could even be more aggressive than this by adding something like text Chang but do note that some linters require the file to be saved before executing so these insert leave and text changed events won't work with every linter to check for this you can again go to the repository go to Lua lint linters and let's open up es lore D and you can check here if standard in is set to true or false if it's set to true then the file doesn't need to be saved for the linter to work so with eslor d we can run linting before saving to the file but if I open up pilent so it's right here I'm going to open that that up you'll see that standard in is set to to false and so pilot will need you to save the file in order for the linting to work so going out of insert mode or changing text won't work with this linter you could add a pattern here and add a list of file types by doing something like star. TS and star. JS so that the auto command only applies to certain file types and you can Define different events that you want to use for different files depending on the linter you're using but for this case I'm just going to Define a single Auto command and I'm going to remove the pattern I'm also going to remove the text changed event so linting runs a little less often but it's still enough I'm going to save this file and the last thing I want to do is add a key map to trigger linting manually I'm going to do vi. key. set this will work in normal mode the keymap will be leader L and it'll run a function and then here we'll do lint do tror lint I'll also add a description to it this will trigger linting for current file I Define these descriptions for the wit key plugin which I have added to my config so that those of you that are using my config can get recommendations whenever you start typing out a keymap now to show you an example I've open up a spell kit project here and this is a typescript file this is the project I have for my blog one rule I've configured with es lint here is to prefer defining variables with const so if I change this to a let whenever I exit out of insert mode the linting will fire and I get this warning to use const instead I'm going to open up the config file for Le eslint it's down here you'll see that I have this rule for prefer const and it set to warn if I turn this off and save this file you'll see that I still have the warning on the left hand side but whenever I move into it which will trigger buff enter it'll trigger linting and it'll go away if I move into it I'm going to maximize this you'll see that it goes away I'm going to do that again I'm going to change this to error for example save this file move into the typescript file and it'll trigger linting and show up as an error now again linting will fire as we've configured it whenever we move into a buffer exit out of insert mode or write to the file now for another example with python you'll see that pilent is giving me some errors for example this T1 variable it's saying that it should be in snake case so let's say I change this to snake case you'll see that the error still shows up because pilent needs me to save the file in order to rerun the linting so if I save the file now the linting error goes away before you finish I want to point out that after putting all this together I found a plugin formation that we can use to automatically install our linters and formatters whenever we start neovim up as an alternative to Mason null Ls I'm going to be including this in my config which I'll have Linked In the description it'll look something like this so I'm going to open up the file explorer I'm going to go over to LSP and bason I've added the plugin here it's called Mason tool installer I've set it up as a dependency for Mason down here I'm loading it with a requir statement and then at the bottom I'm calling its setup function and I'm including an Ure installed list like you would do with mason null LS and in here I'm including the formatters so prettier sty Lua isort and black as well as the linters so pilent and Es lore D this should be JavaScript linter you might have noticed a different color scheme in my setup I've been trying out something different here let's open up the file explorer and go down to color scheme. Lua and open this up you'll see that I've added Tokyo KN to my setup I really like a lot of what the Tokyo KN color scheme supports but I find it to be a little too purple for my liking so I've made it a little more blue with these changes I defined these variables here for some custom colors and then I'm calling the Tokyo night setup function I'm using the Knight version of Tokyo Knight and then I'm changing some of these properties here with my custom colors to make changes to the color scheme I've just been testing this out it's not fully fleshed out or anything I've been playing around with it you can try it out if you like but do note that there might be some colors that are a little off as this is just something I've been experimenting with I'm going to be adding this to my config on GitHub and I'm going to leave this nightfly gooey colors in case you want to keep using that I might go back to it in the future all right you guys so that is it for this video I hope you found it interesting and helpful if you did don't forget to leave a like down below let me know in the comment section if you have any questions or feedback for me and don't forget to subscribe to the channel to see more content like this from me see you guys in the next one [Music] peace
Info
Channel: Josean Martinez
Views: 40,528
Rating: undefined out of 5
Keywords: neovim, linting, formatting, null-ls alternatives, replace null-ls, null-ls archived, neovim setup, neovim config, neovim tutorial, step by step guide
Id: ybUE4D80XSk
Channel Id: undefined
Length: 26min 25sec (1585 seconds)
Published: Sat Sep 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.