Neovim - Native LSP Setting Up Autocomplete and Language Servers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to be showing you guys how to set up native lsp for neovim so as always you'll be able to find all the configuration over on my blog um along with an explanation for how this all works you can also check out my neovim config over on my github feel free to just you know take inspiration from it or um you know you could just use it we're gonna be looking at two plugins today after i kind of look uh show you guys like how this all works uh one of them will be nvim lsp config and the other one will be envin compy all right so first before i get started like explaining all of this stuff um i'm just going to show you what you're going to be getting at the end of this video if you follow through with the um if you follow through with the setup so the first thing and we're going to be looking at things like code actions we're going to be looking at uh navigating your project um or navigating at project scope not just the file scope we're going to be looking at autocomplete and we're going to be looking at diagnostics all right so the first thing i'll do is go to definition so we see this thing header here it's a component in react you can kind of think of it like a class but i want to see where it's instantiated so what i'll do is over top of it i'll just press g d all right and what i can do to go back would be control o so now over this header it'll take me to the same place if i do gd you could even go and look at react like the actual source code for react if you wanted to but yeah so you can just go to definition pretty much of whatever you say the next thing we'll do is gr and what gr will give us is go to reference so i just did that layout is used all over this project so now you can see down here in the quick fix window all the places where layout is used all right so we'll just press enter this is one of the place it's used then we'll press control o to go back if you want to close this quick fix window you just want to put see close all right so the next thing i'll show you guys is autocomplete so we'll do something like console.log and you can see it just comes up with everything you can even see this little message here um just kind of like explaining what it is i guess that doesn't always come up it's a little bit um here and there with getting that message to come up and i'm assuming that will kind of be fixed and ironed out later but yeah so that's the audit completion there uh some other auto completion that we can get because that's the lsp auto completion but we can get um auto completion from all over the place we can get from like snippets uh spellings all different kinds of stuff right so like if i come over here and uh we'll just try and spell something try and spell something like uh learn learning right now it'll come up with the buffer first and now you can see learn is a spell and then if i put like learning you can see it gets it from spell right same with like implementation that's kind of a hard word to spell i guess now it's getting it right from the buffer so it's kind of cheating but i wonder when the spelling will come up but you can see all these spelling things here like implement at implement eight right um and it'll even try and fix it like if you misspell something like that's not right right but if i put something in there it'll think like maybe you mean something else right so that's just another example of kind of like how autocomplete can work all right so that's autocomplete now we'll look at some diagnostics so we'll come over here let's add something up here like fa i don't know something like this right and you can see how it found that with autocomplete or with autocomplete too right like you can see like okay it knows about all of the um font awesome icons inside of this library all right but it also knows i'm not using this one and that's a diagnostic right there you can also see that little light bulb and that's kind of letting you know that you have a code action available we'll get into that in a second so let's see what we can do here we can also get rid of something like one of these and we can see that we have a bunch of diagnostics that are actual errors down here in red now if you want to cycle through all of the errors and all the diagnostics in general i have a key binding for control n to go forwards and control p to go backwards so control n will just take us through our diagnostics here and control p will take us backwards through our diagnostics um so let's fix that again now another thing we can do up here is uh code action right so what you can do and i have this kind of like built-in helper on top of it but we can do a code action now these are wrappers that i've kind of written uh like let's see lsp and tab um and you can see them on my new on my uh in my config um but like it's it's kind of like you have to call lua and a bunch of stuff i'll show you guys like how to set it up in a minute but so if we call a code action on top of this it'll say like okay remove unused declaration for fa airbnb i can press one and it'll do it right now i'll put it back just really fast and i'm not going to be going over this plug-in in this video but i'll show you another one what you can do is you can do this and you can kind of see like where a lot of the neovim like some pretty creative people making plugins this one's called lsp saga and uh we can just like see this nice uh pop-up window under it and i can press enter and get rid of it like that all right you could also just delete it so that's pretty much it for kind of giving you a demo of like what you're getting out of this you can see how it like kind of you know replaces um things like coc or at least it's a pretty decent um lsp client just to get started and it doesn't really need a whole lot and it's also has the added a benefit it has the added benefit of being built into knee of him right all right so i'll disable that now i wanted to talk about like kind of how to set all this stuff up um and you know what this lsp is and kind of what it isn't and what you'll need to do to get it working so neovim 0.5 will be shipping with an api for language server protocol and language server protocol is essentially like a way for all languages to implement a the same protocol for all text editors and ides to use so that you can get like so like it's a common interface right so that they can all provide things like autocomplete um you know linting and all that kind of good stuff code actions whatever and if they're all providing it the same way then all these text editors and ides can you know just consume it all the same way and then it's just really easy to use it across things like neovim vs code and emacs and so on so forth right like all the big text editors and ides all right so i would recommend moving over to the native lsp over something like coc just because it's built in um it's made by the neovim core team so you know like as long as neovim is going to be around this will be here and you know a lot of people are probably going to shift focus onto this uh pretty soon as soon as uh neovim.5 is the in in like the master stable branch right and that'll probably be pretty soon i think they're supposed to release it in four days but we'll see how that goes uh one thing to note is that the built-in lsp is very general um in in neovim itself so don't expect everything to kind of work out of the box that's what these plugins are down here for um now hopefully this blog will kind of help you get started and understand how all this stuff works now at any moment you can also do h lsp and you can get a bunch of help for lsp this is kind of a deeper dive than probably a lot of you want to go for but you know you can check it out i found some interesting stuff like formatting in here and you know um just different functions that i might not have known about otherwise all right so the two plugins we're going to be looking at are lsp config and lsp or nvim compy and this one will give us the audit complete you saw earlier and the lsp config will essentially give us um like all of the server configurations that i'll show you at the end so to get started let's go look at some of my documentation whoops and let's close these windows all right so you can see that i brought them in through vim plug if we look up compy here i have lsp config and compy you can ignore pretty much all the other plugins they're not really relevant to this video i did show you guys saga a little bit of saga earlier all right so go back over here now you see this first configuration file after setting it up now this stuff is really just built into neovim obviously you're going to need to have the lsp set up through lsp config but you already have access to these commands here so let's go and look at lsp config here and this is just like a vim file that i have um for uh for just configuring this uh you could also do in lua if you wanted to but this is how i was doing the gd for go to definition earlier uh we also have go to declaration go to reference you saw that earlier implementation hover i don't know if i showed hover but essentially hover you know you just press uh capital k over top of something like it'll say like over nno remap here um you know it's like no remap but for normal mode right like i no remap would be for insert mode uh so there's also signature help and then both of these guys control p and control n i showed that earlier when i was jumping through uh diagnostics uh we also have auto format now this didn't work out of the box for python but it did work for js for javascript jsx i don't know if i showed you guys that earlier um i can though really fast uh we'll just open up one of these and we'll show that really fast so if i put that big space there um and i save that should have worked i wonder why it didn't oh i didn't save that's why okay there we go all right so that's like that auto format working we'll go back over here and so that's the auto format here and those were all the commands that i showed earlier all right now the other thing will be for uh and we'll just come over here down back to init.then and i'll show you guys compe so compe all right so this is a lua file right and you can probably see like over here i brought it in with lua file and then pointing to it here uh compy dash config.lua right and so a lot of neovim is going to be lua in the future so you might want to kind of get familiar with uh with lu in general here uh but anyway so you can see like the source paths or just the source source that i showed earlier i showed you guys i probably didn't show you guys path but like if i go like this slash you can kind of see like oh these are directories like bin is it slash home so and so forth right um buffer you probably saw that earlier i think i don't know if calc will work for me like if i do one plus one yeah i mean save finds out that it's two just like that right uh snippets uh obviously nvim lsp and you saw spelling it's also tree sitter too and by the way tree sitter is kind of what's giving me all this really good syntax highlighting along with my color scheme so i also recommend checking that out i'll probably do a video on that in the future alright so this is essentially just the base configuration you can actually find it over here in compy right so it's pretty much just this right there's not much else they also have some other things you can add just to like kind of be able to cycle through things with tabs so if i like start to type i can go like this or i can do shift tab to go backwards and tab to go forwards also you know control j down and control k up all right so we'll come back over here and you're just going to need this file to set up autocomplete all right now we're going to talk about setting up language servers now this is what's in charge of setting up language servers uh that's nvim lsp config right and i showed you that one earlier as well um so there's not really much you have to do to configure this plug-in here per se but you do have to like configure the language servers right so for instance let's go back over here right and say i wanted to set up the python language server right the first thing that i'm going to need to do is install a binary for the language server so npm i and i stands for install flag g for global and then pyrite that's the name of the actual language server and it's provided through uh node package manager npm right a lot of them are provided through mpm um a lot of this stuff comes from a lot of javascript developers believe it or not people who built this stuff into vs code uh but so so since a lot of them are just through mpm it's like really simple to get the binaries and you can tell they're binaries because you could just type pyrite and there you go right and that's what this thing needs it needs a binary like that um to work like if same for the bash bash language server you can just press like start and that'll be working and so that's what this thing's actually doing behind the scenes is it's starting up one of these binaries and then it's providing all the things like autocomplete and linting through that alright so after installing that all you're going to need to do is require it so we'll go over here you can name the file whatever you want but it's literally the simplest file ever you just have to have this like that's really it right you don't really have to do anything else besides this uh if you want some special setup or something like that you can pass some configuration into the setup function here uh probably a lot of people will just use whatever comes out of the box though right um those are ones for like that are easy to install through mpm some of them are not so easy to install like java jdtls uh the java eclipse language server right uh that one is not super easy to install there's other ones that are not super easy to install too but most are and you can see that so if we're here right if you come down you're gonna see a link somewhere to config.md here alright you can see what and i think i have this link in my blog too but you can see all of the language servers that lsp config um like it supports right like kind of out of the box you'll notice a lot of them are through mpm like you can just npm install them like this one here uh for css um go down look through a few other ones docker comes in through npm uh elixir i guess doesn't right so there's a bunch of them in here some of them come down a good amount of them you can install through npm but you can kind of just you know however you need to do it they're going to leave you some documentation here so you can kind of figure it out graphql is also npm i know bash is npm python's npm so a lot of them are easy all right so after you have it set up um i mean that's really kind of it like you don't really need to do anything else besides that right so once that's there and once you have it installed it should just work pretty much right after you have all that configuration done like all this completion configuration and all that it should just work um so i left a couple other links here so this is the link to the language servers that i just showed you this one is for language specific plugins so like there's probably gonna be some wrappers especially for more complicated ones for like java um you know there so like this one's for jd tls flutter tools you know that's probably a little bit more difficult to do so there's like another layer right um it's also if you want to enable snippets i left a link here and then for plugins that will hopefully be easy yeah so essentially what this is is it's a plug-in to hopefully make installing this stuff easier for you in the future so if you want to use this plug-in uh it'll give you a few out of the box right probably a lot of the easier ones that can come down with like mpm right so bash cmake css docker elm you can see that they support a good amount of things so if the languages you use are just right here you could probably just use this and then just lsp install it and you don't have to go through the steps that i showed earlier um you can see that you know they have kind of like a a list here that hopefully they're going to check off all the boxes it'd be kind of cool if this thing did work and then you could just like install everything you needed um even the difficult ones that would be that would be really cool if that worked in the future but you gotta remember using uh the lsp for for um for the newest version of neovim the one where there aren't really a lot of plugins yet i mean you're kind of on the bleeding edge here right all right so let's come back let's come back i also left some links for the plugins here so you can kind of check them out lsp config and nvim compy now i think that's pretty much it for this video you can check me out over on twitter there's a link to my github here on my website obviously my youtube you can ask questions over in the discord and you can donate on patreon if you like this video leave a like and subscribe and i'll see you guys in the next one
Info
Channel: ChrisAtMachine
Views: 43,798
Rating: 4.9146342 out of 5
Keywords: chrisatmachine, neovim, lsp, neovim lsp, ide, neovim ide, vim, coc, native lsp, vscode
Id: NXysez2vS4Q
Channel Id: undefined
Length: 19min 23sec (1163 seconds)
Published: Thu Mar 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.