Neovim Builtin LSP Setup Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
is it uh acceptable that i'm still on 0.5 no god please no no what's up youtube it's me tj devries here with the co-host of our new series bash to basics bash bunny hello my name is bash uh yeah we are basically doing a collab on a monthly basis so the second wednesday of every month we are doing a collab where teach is going to walk me through some neovim stuff some different nerd stuff pretty much answering a lot of the questions and maybe pain points for beginners getting into neovim getting into coding so yeah we're kind of collaborating to make your lives a little easier in that regard yeah and in this episode we actually worked on built-in lsp for neovim so we start with no lsp no plugins associated and we end up at a place where able to jump to definition do references integrated with telescope completion all that sort of good stuff so i hope you stick around for the video and really enjoy it thanks everybody go into the coding window so i can share my screen okay good the right screen's sharing this is a success the very first thing we need to do obviously is get that updated neovim yes this is just the neovim that we did during telescope isn't it like it's just an exact one this is true nice i love it okay cool so download the new app image oh we can just you've already you've already got uh the repo somewhere i think i must and i yeah cause i think that's what we did last time we just we just pulled and then hit make because it looks like i have i might have it here oh yeah nice cd get pull easy cool get get get pool cool but with an accent yeah connie connie's in chat telling you that telescope doesn't even support the version that you're on anymore so i'm glad to see it didn't completely bust oh that's why it keeps giving me errors whoopsies we've actually released oh you don't uh no then just after you did pull just do uh make uh we actually like have released a whole another version since so you're on that's hilarious of 0.5 we released a 0.5 that we released 0.6 and now we're on oh man yeah yes um i i hope nothing else breaks it shouldn't it shouldn't break if things are mostly working for you right now except think people are telling you that your neovim's too old then then we're good yeah neovim uh we are on 0.7 right now as in it hasn't been released nyx not yet if it ain't broke wait for it to break there you go um yeah but sweet okay so and then just sudo make install all right so now let's just type nvm and hope that uh things still open uh coc perfect you can just delete that from your life anyway so that's no problem all right cool good okay cool so we're at least we're at least still you know able to to open neovim and have it not uh not be terrible and we're gonna remove coc anyways today so that's good yes all right cool so maybe before we get like too far into actually like setting it up why don't you tell me what you understand about lsp like generally first bash like explain lsp to me like i've never heard about it because a bunch of people in chat obviously they don't know anything classic chat uh yeah so for sure so what i've what i kind of understand about it it's language server something and protocol indeed protocol okay cool uh that was my first guess actually why do i have double that wait okay um what's going on i only have one mouse i don't know what's going on uh anyway this green chair and discord you only have you land with mouses i love it i love it i told you i'm really good at breaking things um but yeah basically it's like for it seems to me it's like for each language that you use you can like download the like language server for that and that allows you basically like apparently this is something that a lot of ides kind of handle in the background for you um and it's what allows you to have like syntax like syntax highlighting well i just want to say one quick thing about this while it's true lsp can provide syntax highlighting which is generally called semantic tokens or similar for lsp it is not usually what provides syntax highlighting for your neovim experience in general it's either the old regex engine or the new tree sitter engine anyways everything else bash says here is literally perfect so let's get back to her explanation and uh aaron give you error messages different things like that related to that specific language um and of course like each the syntax and like the syntax errors and stuff like that the highlighting is all going to look the same for every language and that's why it's like a separate uh kind of a unit that's like handled by the ide generally yeah yeah i think that's a really good like fundamental understanding what's happening an important thing to remember is like installing the language server that you need right so like you'll need a different one for a go lang than you would for like rust right you'd need two separate ones but the cool part about it right the idea is sort of like you know before to implement language support in editor you had to re-implement that support every single time and the idea of lsp right is you kind of like neovim has an lsp client built into it now so like if you type help lsp inside of neovim even like a just brand new default new of him it has that information about the lsp like inside of it right and so we have like okay you could do all of these things right away so this is basically a client that knows how to speak that protocol right that makes sense yeah yeah that's cool um and so the cool part is right like this lets you more or less have like control over what's happening with those different language servers and it doesn't matter what language you're actually working on everything happens exactly the same and so the adoption has been pretty good for lsp because what happens is people are just like oh i want to be able to have this work in a ton of different editors well i'll just make a language server for my language and boom i support a bunch of editors that's awesome yeah totally that's awesome yeah so it's similar so like for anybody who's on coc right now it's like somewhere how you how you would do like coc install and then like coc go or whatever exactly i mean it's not quite that for go but you know yep and so a lot of those for coc are like they are just almost exactly whatever the normal like language server is for that right so for coc-go it installs go please for you or something like that or adds it to some special path or these other things right but it's still for that aspect of coc it's basically just talking the language server protocol now coc has additional things built into it that i think are like i'm not saying they're bad or good or whatever they're just different than the way that like built-in lsp does it yeah i mean for some people they really like like that strategy and it works really well but uh like the built neovim's built-in lsp is primarily focused on only doing things related to lsp right so like coc has stuff like oh i want to install something like help with spelling or something like that well that's not really like an lsp thing right so that's that's why a lot of people get confused i think about coc because it has like a large overlap with lsp yeah but it also has it's like a superset of lsp because it has its own sort of like interface and way that you can write plugins for it and all this other stuff that that makes sense that does make sense yeah so it's basically it's well i've heard a lot of people describe it as like a lot kind of like clunkier like heavier than uh using native lsp because it's got like as you mentioned it's like it's doing kind of maybe more than just language server but i mean in the the way that i'm using it is like really just as an lsp so yeah yeah cool so yeah and i think the other thing too like there are pros and cons to the way that the coc setup is like uh coc runs in a separate process in node which is like that's just it's just the way it is which is not like objectively bad right it's just that that's what it's doing so some things like there's a lot of serialization or whatever you may not have exactly the same feeling of like snappiness or whatever it is with built in lsp but like coc basically can't like block your editor from doing something bad which like can happen with native ls because it's running inside so it's sort of like it's a pro and con sort of situation um obviously i like uh built in lsp because i helped write some of it and it's good but i don't think coc is like inherently bad so if someone tells you that on reddit you can tell them that they're dumb so perfect i love doing that on reddit yeah it is it is 90 of my reddit time it's just telling other people they're done you're done keck w yeah uh bash blanco you know yes exactly cool so that's that's sort of just the intro to like what is lsp i want to make sure that that part's clear before we get too far into like what's going on with neovim's lsb because some parts of it people are i think somewhat confused by when they start yeah so like for example there's a plugin that we'll install shortly called nvim lsp config which i can send a link for in the chat for people who want it and people are confused by this because they're like i thought lsp was built in what the heck is this thing like why do i have to install plugin well the answer is technically you don't have to install this plug-in but what it does is it helps you manage and connect to different languages so like certain languages need like a bunch of different arguments to start their lsp up right it's not just like that you run like go please necessarily and it understands like where your project root is and what files to watch and like all this other stuff right and so envelopes pconfig is just literally like configurations only that's all it is it's just configurations for different languages to make it much simpler to basically like attach and manage those language servers as opposed to having to write all of this code for yourself or like read a bunch of extra docs that you don't need to know about like go for example yeah um yeah so that's actually the first thing that we should probably do is install this plug-in and i guess remove coc because we're not going to use that all right let's do it config there we go auto complete we'd love to see it yup yup give me an error about coc which is fine just perfect which is perfect because we're gonna we're gonna say bye bye cool so um i think probably a a pretty helpful way for us to get started here is if you go back to help lsp we'll be able to start um we'll be able to see some code sort of like uh detailing how how things are starting if we just scroll down just a bit in this uh in this file and this is for future reference too like this file should give you the information that you need uh to know to generally like get started with things and so this example right here this uh like lua code here is basically the way that i i recommend people sort of think about how their lsp is going to work and basically the architecture inside of neovim is more or less like we especially when you're using lsp config which simplifies some of the configuration is you tell lspconfig hey i've got this server and it's associated with like these languages please run it when i open up files of this type right so that part makes sense right like when i open up a go file i want it to like run the go one following me cool so then what we what you can do is you can pass a function to the lsp configuration that basically says hey i want to do these things when i attach to a buffer that has an lsp server right so notice how there's this function here in the docs called custom lsp attach and it's got function client here so like it's a function it takes in the client so you can inspect things about what this client is like what's the client name uh what file type all this other kind of stuff like that so you could do special things for different file types like maybe you want to turn on auto formatting stuff for go but not for typescript or something because maybe you shell out to prettier for that instead of uh instead of asking the language server to do that that part makes sense yeah yeah cool so what we'll do is we'll actually basically attach different key bindings in this uh function just like to this local buffer so that we can do stuff like go to definition or you know ask for hovers or whatever it is we do that inside of this function that means if you're not inside of uh like an lsp buffer right or a buffer with an lsp attached maybe i should say then it won't do anything which is good right like you that way like if you change what also sorry i'll give a give a simple example like capital k is the uh keyboard shortcut for basically like asking for man pages right so it's kind of like ask for documentation okay cool well when you're in like a vim help file capital k sends you to the help of like that thing so if you if you even if you moved around in this like help file here and you went to where it says like i underscore or control x control o just like up a few lines from where you were uh in the beginning oh right here yeah yeah and you press like shift k inside of that thing i think you need to do inside of the uh like right here like right here like try shift k right now i feel like that takes you to that aspect of the help right so there's sort of this theme within neovim or within like vim right that capital k means show me the documentation right does that make sense that's like at least in my mind that's how i like to think about what capital k does yeah so inside of buffers that you have an lsp server attached to i think it's really helpful to basically say like okay well i already have in my brain when i'm inside of in capital k means show me the documentation okay so what if i make i ask the lsp server to show me the hover documentation when i press capital k right so it's still like always the same like idea but you're just using a different technology to sort of like make that happen does that make sense yeah yeah it does so so the reason i prefer um to only override like the key maps and i like to do it inside of this lsp attach is because then when i don't when i don't have an lsp buffer my key maps remain unchanged which is good so i can keep sort of this like idiomatic key bindings but it works inside of lsp so that's sort of what this idea of this lsp attach function is and we're going to use that uh very shortly cool okay cool so then how do i like collapse this i do like like how do i get rid of it that dialogue uh oh this is uh you're just inside of the help thing right now so you can press ctrl o and you should jump back out of here uh probably one more time again oh okay okay that's very cool then yeah i think other people will probably encounter the same thing they're like okay cool i'm in here now how do i see it so actually when we do the lsp one it'll do it in a little floaty window that like disappears if you move your cursor so it's pretty it's pretty cool when we do that part shortly but okay so let's i think now that you sort of understand the general concept of how we're going to do this um maybe we should just try and set up golang does that seem good yeah that sounds good okay cool so let's make you remember how we made uh lua and then was it did we make it in like bash bunny slash telescope or something like that it's in uh lua it's uh it's in a folder config neofim lua and then it's uh the file is bashbunny.lua ah okay yes let's go to that money.lua file right i guess i could probably uh close the yeah we can close the help is fine too i think you're still like in your home directory we can cd oh yeah i should yeah or something i was like why yeah there's nothing here yes oh wait i'm doing all right sweet okay so what we'll do inside of here is like we can just have one part of it that's setting up telescope in one part that's setting up uh built in lsp right okay well that's no no big deal um the way that we start setting this up is you'll basically do and i'll just paste actually i'll send the link in chat so people can find this as well to get go please working we basically need to do like two things the first thing is we need to install go please somewhere on your path this is pretty easy and we'll do this outside of neovim um and then the second thing that we need to do is we just need to basically tell knee of him hey i would like to uh i would like to use go please right so i think um and i'll paste this in the in the chat as well to install go please all you have to do is literally go install and then the link to the go please like tool lovely so so that one's really easy uh hold on let me just double check that this isn't um ask for anything or docks you in some way yeah like does it show my location on this one i don't know uh okay cool and then so this is this is the like server part right in the lsp equation like this is the server that we're going to run so you could like liter you can type go please just g-o-p-l-s i actually don't know if it's actually supposed to be but you like hit this and you can run it and it starts like that's a server yeah but obviously like uh you you're not gonna tell it anything here so it's waiting for you to send it a message that says i'm initialized yeah but we're not gonna do that here but that just shows okay sweet so we have gold please running that's like the first part of what we needed and now we basically need to tell neovim hey how do we find this go server and then what do we do when we're like attached to it good yeah cool so that part of the the information is on is here and the mlsp config already sort of like understands go please and so it can uh help like you literally all you have to do to get it to set up initially is just basically say hey okay i'm going to add this to some place that gets executed and i'm going to require lspconfig dot go please dot setup right so we can go into that lua file and we can do this and then we can maybe open up a like go file in a separate tab or something uh so that we can sort of open and close that file a few times while we're in exploring uh our lsp configuration stuff okay cool i can pretty much just copy what you sent me right that's exactly right so now lsp config says okay i see that you want to run go please yeah i can set up the configuration stuff for you to do that at this point it's not going to do like default key maps or any of these other sorts of ideas this is just going to basically connect the server and we'll be able to check that the server is connected uh pretty easily in like a new tab or a new nvm instance more or less gotcha so i can close yeah asking about the unattached that's what we're gonna do after this uh yeah so if you just like cd somewhere else into like a project that you have where you're writing go code okay that is uh okay to be shown to the internet yeah yeah then we can we can you know check to make sure that it's that it's running basically projects been a while since i've been on this computer uh we'll just do a project sorry i'm getting like scam calls uh yeah this one yeah this one's good this one's good like old version but it's fine am i muted no can i open you want me to open something in chat so yeah you can just open up the main.go file is good and then type colon and then let's do capital l so a bunch of there are some commands that happen automatically so then lowercase sp yeah and then we should be able to hit tab here and hopefully there's a few options so we can hit lsp info uh lsp start should already happen hit lp info and then we run that it'll say hey look one client is attached to this buffer you have go please attached to this buffer ah so that's that's good that means we've installed it already and it's running and it's able to communicate between each other that's awesome so that's as a good first step so this tells us okay we're going to do this and in fact you can even see in what situations um like go please would start like if you open up a go mod file or some of these other files like okay cool that's that makes sense this is when it'll get attached and i can see what buffers it's attached to this that's good so that's our step one all good go ahead i was just saying nice oh nice thumbs up yeah sweet it is nice i'm excited so that's good first try uh actual first try oh we should be taking first try bets in the chat of course oh this is a good idea but uh but okay one of the greatest emotes of all time yeah okay so okay so that part's cool right so we can just quit out of this neovim so that when we come back again we'll be able to restart with a fresh slate um and let's go back to your knee of him where we're configuring so now here is where we're gonna start basically doing the like actual actual configuration of of uh things that are like interesting okay right and i think the first one that would be probably like pretty good to do well let's actually do um the hover one because i think the hover one is is pretty cool okay yeah so should i do split window to like show my old setup like my old settings um did you have a did you have a keyboard shortcut for showing covers of things no it just didn't hurt automatically oh do you know what or that's what i guess that's what i'm asking did you like ever press something that basically showed you the documentation of like whatever the current symbol under your cursor was uh no i actually no i didn't actually i didn't have a shortcut for that but i i did have one for like formatting and stuff like that but i guess yeah for the go please does that one like auto i had before it was auto formatting for me and i would save yeah we can make it uh do that part as well i think um first let's check that one's a bit that's a bit more involved let's first just get like our first key mapping setup and then it's super easy to add the other ones as we go so the way that we can do that uh is with that like on attach function that i was talking about before so inside of setup uh this like setup table here yeah um we'll need to make a new key right so we can basically like say on on so on underscore attach uh and then that equals a function so just equals function yup and this takes in client but we're not going to use it right now so you can just do like just function transparent and then uh i don't remember how much lewis syntax you remember so this is just like you can do a new line and then end and then a comma and then that basically says like okay i'm done with this function and that's this key so this will run this function gets run inside of every buffer when they first get attached um to the lsp okay so it's just little code we can do any of the things that we want so the uh so like a really simple thing is like you could say print like i'm attaching or something like that you know what i mean and like we can we can show that this is running yeah um but oh uh not inside of not inside outside of uh yeah oh right it's right like here i could do it like looking further down to realize that that's where i can do it okay cool yeah if you haven't written lua in a little bit it'll be it's we'll do we'll do some refresher along the way yeah so now we just go back to your other tmux pane and then you just like open that go file again we should probably see that printed i think yeah it's there perfect i mean so so that makes sense so like this just gets run there's nothing magical happening there it's just like okay i see a buffer that has an lsp attached i run this function gotcha that's all that's going on yeah yeah yeah that's awesome cool so there are a ton of things built into neovim that do like common and sort of like useful lsp stuffs right uh that we like already handle for you so you that you don't have to know how to do them right so um and we can actually read about about a bunch of these uh in the help of course my favorite my favorite location as everyone knows um so if we do help uh vim dot lsp dot buff oh sheesh yeah and then yeah and you can hit tab here and there's there's a ton of them so these are all things that like neovim core has like written and we make sure that they work um while talking to the lsp and they handle the fact like there's different messages that could come or like all this other stuff etc and hover actually is the one that we want to do first so you can just press enter here so we can see this uh this help and it will just give you some information about what this function does right so it's like oh displays hover information about the symbol under the cursor in a floating window awesome that's that's good that's what we want to do right we just want to be able to like show the documentation of whatever thing is under our cursor yeah and that's what this does so uh you can quit out of this help file i just wanted to show that like you can read about these and what they do inside of there and it's it's all sort of like built in so there's uh should okay there's there's a cool new feature that's in neovim 0.7 only so if you're uh a scrub on 0.5 or a kind of cool person on 0.6 you won't have this feature but since bash is a really awesome person who just installed the latest nightly she's got this excellent feature which is a ability to really easily set uh key mappings from lua so we can just like do that and it's pretty cool um so there's a basically a new thing and you can do vim.keymap.set um and this will let you write sort of like much more concise uh oh basically i don't know how to how how to say this really concise like lua key maps right so this is a function that you can call the first parameter of this function uh is the mode that you want to be able to call this in so like uh you can just do quote n quote because we're just going to do in normal mode for this one ah yep and so this actually you can pass in ways to say like i want to do this in normal mode and insert mode etcetera etcetera it doesn't super matter at this point for i like i would be insert mode and uh yeah but you put it so if you only have one mode you want to do you pass it as a string if you want to do multiple modes you pass the first parameter as a table so we're saying okay right now it's just normal mode and we want to do um so now you pick the left hand side of the map so for this one it's going to be the capital k right so just like pass in another string that's capital k because that's what we're going to do perfect and then this part is the right hand side of the mapping so that just basically means what you want to have executed and i just want to make sure that this is uh yeah this is all documented with exactly the words that i'm saying um so in this case we actually want to execute that lua function vim.lsp.buff so you can just literally type without quotes vim.lsp.buff.hover perfect but don't call the function we're basically passing a function reference right yeah yeah that makes sense that makes sense yeah so this would actually set this globally but we don't actually want to set it globally so in the last parameter we're going to make a new table and the what the table's key will have um it will be buffer and then you can say equals zero so i don't i don't know how familiar you are with this but basically uh is that basically yeah with actually like literally an equal sign okay yeah perfect so zero sort of just means whatever the current buffer is okay so this says hey please map in normal mode capital k to call the function vim.lsp.buff.hover only for this buffer okay so other buffers won't have this mapping and we i can show you how that's happening like when we open up the other um neovim again which i think probably now is a good time to do so like after we save this we can open up the other knee of him and hopefully capital k will now do this cool little hover that shows us information about something so if you go to like a print line or something yeah okay let's go and six and then capital k yeah right and so now if you like move your cursor that'll just disappear so this is just like a really easy way for you to get the and we can configure some of the highlights later that's uh something that we can do because the highlights are all configured but you just press capital k and you'll and you'll get that right uh one second i need to let ember out i've been coding without that for so long that's so good thank you that like makes me realize i've been cody without that for so long like why am i such a pleb yo prime thank you so much for the tier one i appreciate it thank you thank you oh hey there the name's the prime gin boy do i love v-s uh we had a question as well about if you can scroll through that pop-up yes you can so if you press capital k once yeah you're here now press capital k again now you're inside of that so now you can do whatever you want there's also other ways to do it like you can map some stuff to scroll up and down in there but uh i don't think i i never do that i prefer to just go in and then when you're done you can just press uh like you can just quit out of there i did ctrl o and it got me out that worked that works too so so that's that's the like first thing so now we've just done it like your lsp is actually running and you're able to get info about the stuff that's in your buffer very cool and if you just did something like if you did colon v new to like open up a new uh oh sorry like v new one command sorry oh gee yeah yeah you just do that this will just open up an empty split basically just so you can just press enter and now you do something like colon and map so just nmap one word nmap space capital k yeah and then enter no mappings right but if we go back to your go your go buffer and we run that same command yeah i was nmap okay tap okay yep it says hey look you've got this function here that's a little function i can actually show you later how we can add uh you can actually add descriptions which is kind of cool so you can get more information than just do a function 25 but that doesn't super matter for this exact moment right but that's basically like okay look on inside of your go we actually ran this function here to attach but other files we don't run this so capital k works exactly the same way that it would have before yeah cool yeah that makes sense it's like it's not it's not attached to that uh exactly yeah yep um yeah so i guess the next thing that we could do is probably add um we can add go to definition that's probably another one that you use a lot from coc i'm assuming uh uh whatever's under your cursor like you navigate i actually don't but like that'd be very useful oh yes it is very useful um [Laughter] okay cool well yes this will be a great feature okay yes let's do this okay cool so basically you can just like copy this line and then just paste it below and we'll just change some of the things inside of here right so instead of k i recommend like g d like go to definition that's how i like to think about it uh-huh and then instead of vim.lsp.buff.definition or uh hover whoops i accidentally dang it i was just i was going to say what what you should guess that it was going to be um and so now you have definition of something cool so if we open up your other neovim thing again and we just yeah perfect and we go to something like uh like if you go down to like line 20 or something yep and you see how it's got like create project or something if you go on top of that and press gd it'll take you to where that like field is defined right because that's where that's the definition of that field yes so and then you can press ctrl t this is just built in to jump back oh sheesh i didn't know that one yeah that's a good one so ctrl t is actually just built in for tag stack and like this is sort of a theme that we try really really hard to to do inside of built-in lsp which is that like if there's some vim idiom that we can uh like continue to use right then we're gonna try and use that inside of here so when you jump to definition it adds something to the tag stack tag stack is a specific idea inside of neovim and it's different than like the jump list so people are in the chat asking what's the difference between ctrl t and control o the short answer is control t operates on the tag list control o operates on the jump list exactly yes as prime said tag stack has its own time which it adds which is generally when you're jumping to other definitions and jump list is based on like movements so this means like if you jump to the definition of something inside of uh inside of a project and you do like you do some large movements and things like that then normally you'd have to press ctrl o like 10 times to jump back to each of the different spots along where you've moved but if there's only one definition between the two you can actually just press ctrl t and you just jump directly back to the definition where you were before which is really nice so i use ctrl t quite a bit because it works well with moving around in a file that you're exploring but now it's like i'm done exploring so i jump back to where i was before okay cool yep that's awesome okay so so just to make sure that i'm understanding this so ctrl t is basically like you're jumping you're jumping by definition generally at first it's like control o is like jumping back to like each significant spot that you've navigated to like what so like where does it have a good intuition um you can like find out the exact definition of both by like reading help jumpless and help tag list but jump list is the one that's control o and control i and that's basically like uh just like you can think about it where you've jumped in a file so like significant movements add a new entry to the jump list so if i were like at the top of this file and i went to like the last line of the file does that count as a jump or is it let's find out press control o okay yeah sure nice yep that's pretty cool normally you'd have to like press ctrl o like three times if you had done that but in to go back to where you had asked the definition but now you can just press ctrl t and you jump back very cool and there's a bunch of bunch of info both about jump list and tag list uh here and so like this is sort of the th like a theme within the built in lsp like i said those like okay you're jumping to a definition we'll add an entry to the tag stack so that you can press ctrl t to go back notice we did not add a mapping for ctrl t right that just reuses the built-in behavior of neovim you following me yeah yeah that's really cool i like that i like it a lot as well okay so that's the that's the first sort of like really basics there's a bunch of other um sort of like built-in stuff within uh lsp another one that i actually use quite a bit is um go to type definition i think i i think it's actually called i think that's what it is i gotta i guess i gotta make sure m.i.l speed up dot type definition yep um which i i find pretty uh pretty helpful oh so if you wanted to just call it you'll have to do colon lua and then you can actually uh you can run it without adding a mapping i'm trying to like lower it uh we can we can just uh we can just skip that part for now but the one that i do think is actually pretty interesting is type definition though oh yeah just blame it on the lsp it's go user area yeah probably it's probably us that's okay so type definition is one that i like quite a bit uh so instead of just definition at the end it's uh type definition type underscore definition sorry uh i like this one too because if you're on a variable so like and we should make this a different uh thing i think i have mine to be like uh g like capital t or something like that yeah i mine to be g capital t because like go to type you can have it obviously whatever will be good for your memory but either one uh yeah so this one's cool because if you're on a variable you don't like know what the like fields of this variable are or whatever go to type definition takes you to whatever types is under your cursor so if you open back up to like main here and you go to like pr or something right instead of print yeah you're like right here and you do your gt that takes you to whatever that thing is which is really helpful for like understanding what like what does this thing actually do um yeah and funny enough since we're since we're doing since we're here um and you just opened up an interface on the last one uh you there's another thing called go to implementations i have this for gi for implementation so instead of definition it just says implementation this is actually really useful for go um so maybe we should add this one as well um and by the way while we're just like thinking of this i hope the like pattern is sort of obvious of how we can do a bunch of these things it's basically like oh i find out that there's this new function inside of here i add it to my on attach and now i have this for my lsp stuff so yeah it's just implementation yep perfect and so now if we go back and we uh go to like pr again yep and we hit gi instead um yeah yeah um or do you not have any implementations in this project uh do it on print projects maybe instead do gi when you're on there or is all are all of these defined oh perfect wonderful okay so this is this this is cool especially for a go so if you like uh if you press ctrl t to go back to where we were before and you press ctrl d or sorry not ctrl d sorry uh gd um for where where we were before uh in maine yeah yeah sorry i told you to do the wrong thing yeah to go to the definition of this this takes you to the interface which makes sense right because that's where this method is defined for go but like most of the time what you actually want when you're doing this for go is uh wait bash can they can they not hear you wait can you guys not hear bash sorry my bad i was muted uh okay wait hold on i need to close my door she's muted she says she's fixing it apparently now all right this is my stream now this is my stream now oh hey bash uh never mind oh hey uh i see i see nothing was going on while i was gone no absolutely nothing was going on no scheming yeah um okay so for go though right like when you this is the definition of that method but what you want to usually find out is actually like what's the code that runs for this thing right because that's that's actually what you're looking for so if you uh press ctrl t to hop back to where we were and then you do gi this will take you to the implementations of this and actually could be multiple so yeah i think there is oh sorry we're not wait but but either way so that that part's pretty pretty cool i think and uh telescope just works with this now and we we might maybe we'll save how telescope integrates with this for for another time because i don't know how much uh how much time we have for that but so so that's the basic idea of how you can get a bunch of the different uh key maps sort of like going for the basic lsp things right which is pretty cool i'll show you maybe do you have time to do maybe like two more interesting key maps and then yeah for sure we'll talk uh i don't know if we we'll see if we have time for to talk about completion completion is a bit more complicated uh because you'll need to have some other completion uh but that's that's okay uh to talk about so okay that's okay so uh let's go back to your config and we'll add two other uh i think pretty cool movement commands so i have um if you i don't know what a mapping a good mapping for you would be [Music] but for me i have i have two mappings i think i have it like space d n and space d p for diagnostic next and diagnostic previous but this this command i find just incredibly helpful because it helps you move um forward and backwards to wherever the next error is in your file you probably don't want to just do like d though because that's going to mess with delete because it's in normal mode right so you probably want to do something like space in front of it and then you could do df yes this is good yeah i think that i think it'll just work if you do just like that um and so this is actually i'll just paste this one in the chat because otherwise i think it's uh it's kind of complicated to do otherwise uh i guess actually it's not that common okay so it's just vim.diagnostic so not this one's actually not lsp specific uh vim.diagnostic.goat and this one should probably be next yeah you can do well you don't i bashed you have space as your leader yeah okay then that works great yeah you can do leaders it's okay yeah um so we're just gonna do vim.diagnostic dot go to next go to underscore next okay perfect okay so and then we can uh we can just open up the your other name again let's quit let's open up your main.go again as good and then let's just like make something break inside of here like you can just add some perfect so now i go back to the top of this file and now do uh space df moves you right to where that is and then gives you a little information about what the error is on that line okay which i this is one that i love doing because for example if you change like the return type of project prompt right like that might make a bunch of errors in this current file you can actually do you can just like keep on moving to the next error by doing space or space df space df and like go until you fix all of the problems in your file that's awesome is there a way to like list all of those as well there is and in fact so let's uh let's make a few errors in this file get at that yeah just like your regular pretends your regular stream yeah pretty much yeah that's perfect um cool so i think we should be able to do now um telescope and then is uh hopefully hopefully i remember which one this actually is uh because i think it's just telescope diagnostics now yeah yep now you actually have a list of all of them here and you can fuzzy find over of them and then go go between them and in fact since you've like broken multiple things now with changing this file right it's like oh well i can't use this because it's changed over here well it actually shows you the other files too where you've broken things which is pretty cool that's awesome i usually break things and i don't know what else broke until i and then i just i'm like oh let's just run the code and just see what breaks yeah because now if you like type expression here or something like that right it's gonna filter it down to the ones that have exp like it's just you can do the fuzzy finding over this this just comes for free if you have telescope and built-in lsp set up right because this is just like you can fuzzy find over all of those yeah that's awesome um yeah so so that's just there and so now we probably want to also do like the i guess prev for you probably all right the uh wait i'm just gonna change this i'm gonna add one for wait to and then this can i can i make this like uh the telescope diagnostics command so if you just paste in a if you put a string inside of there instead then it will run it like a normal command so if you do like colon telescope i can't type don't mind me yeah diagnostics uh yep and then you'll have to do uh like the carriage return thing oh right you're all right yep and actually probably the better way to do this instead of a colon at the beginning i think it's nicer to write uh like command with the brackets around it so like bracket command that's that's a yep that's a good way to write it yep okay learning best practice diagnostic list i love it i love it yeah we should probably also add d like p i guess for previous uh so you have goat diagnostic forward and died or i guess maybe you want back forward and back i don't know how you like to think about i don't know i think uh i think i might actually change this to dj and then the next one came dk i like it yeah and then this would be i guess is it go to previous or go to just go to yeah perfect so then those are all set up so next time you open up a go file you can just do exactly that alrighty um oh yeah as oscar mentioned uh this is a cool thing you can do is if you do so do space dl right to list all of the just all the diagnostics if you press control q this actually will put all of them into your quick fix list and so you can keep those around and you can like navigate to those which is a nice uh a nice thing if you're used to using quick fix yes yeah uh cool okay so those are so that's sort of like that's that part's pretty interesting there's a bunch more that you could read about inside of vim.diagnostic which is sort of like a newish feature of neovim and it's basically like a unified way of handling diagnostics within the of him so like these like messages and everything you can actually configure how those messages are displayed whether how you want the highlighting for those whether you want under you know like all these other kinds of things you can actually uh configure those with vim.diagnostic we don't have time to do that uh today but those are actually like is actually bigger than just lsp anything can put stuff inside of m.diagnostic which is really cool so like if you had other linters that were running like maybe you have like golang ci lint or something yeah you could have you could integrate that with it and then those mappings that you had for like diagnostic next and diagnostic previous those would still work for those even though it's coming from a completely different source awesome right um so that's all of those so then the last the last thing i think that's probably worth showing oh well one thing that would be cool i guess to show as well would be lsp buff dot rename uh we might as well show how to do that um today as well and so it's just still the same idea of vim.osp.rename and this will let you rename a variable and lsp will go through and like fix the places that you're changing the name yeah uh i think i have mine why do i have it to be space c r i don't know i have a weird i have a weird one that you should do something different for i think i might just do leader r yeah leader r seems good perfect yep uh and then we probably need to like reset your project here uh the not in the go one oh yeah everything go won't know how to rename everything when it's uh broken um yeah perfect cool so if you go down to like input here on line 14 and you do uh what do we say leader r yeah it'll prop you for a new name so you can just type something new and then press enter whoa and then it does that and the really cool thing right is like it only changes like since it's the language server doing this it only changes it for the ones that make sense so if you have like another variable named input somewhere else in this file it's it doesn't change that thing like it's not a search and replace it's like a actual understanding of what's going on with uh with the code that's cool yeah so i use that a lot it even works for functions so if you like change the name of project prompt to be something completely different like you you hover on top of that you press rename call it whatever you want um and so then that will even change it in other files so sometimes after you run this you have to do colon wa for write all because like it changed it in another buffer um if if this function was called other places but it will go to the other files and it will update those files for you as well these are all my other errors that are unrelated to the changes that we made yeah perfect nice a good example of how we could use that to move around and uh find other things so but anyways this is like really like where the power of lsp becomes very cool right because you can actually rename those um a bunch yeah another cool another cool one to show as well um especially for go uh if you just add a import that you're not going to use like let's say we just go up there and we add like net slash http or something one of the imports for this oh yeah because usually it would just like remove it yep for me i don't actually even remember if um if this happens automatically via formatting for go please or what but uh so now if you do so let's uh just do lua so cold and lua vim.lsp.code underscore action uh oh sorry vim.lsp.buff i forgot to go wow got him and then uh run this function yeah you have to still do the parentheses when we call it oh right i always forget sorry i'm used to it you're good easter vib script yep oh no okay you're gonna go i think it's everywhere else okay whatever okay hey we're sorry lsp m dot lsp.buff dot code underscore action and then parentheses parentheses and then yep so this says hey uh would you like me to organize your import so you can type one and then press enter which is pretty cool that is very cool if you instead were let's say like inside of uh inside of your project prompt function um oh by the way for people wondering i will upload a shortened version of this video later yeah it'll be on youtube uh on the youtube so that it'll be there forever yes so there you can actually use telescope for this as well but we'll get to that part later but another cool thing is like let's say you um did let's make a new var like x you know colon equals uh or like yeah whatever you want to do and then uh i think you can just do http dot it's like client or something like that i don't remember what the actual i got to think of something that you could import easily oh actually no i've got a better idea let's uh move down to that diagnostic with space dn oh yeah yeah with space to end yeah we got to practice we got to practice oh wait it's d dj something for you yep just delete this line is fine we actually don't need this let's go up to like line four where you import format and just delete it yep so now if we do space the end to move down to where format is it's like hey you haven't oops i'm saying my thing but it's not your thing okay so um run code actions again so just colon lua and then you should probably have this one yep hit enter and says hey would you like to add this import so you can do one and then enter what if i just hit organize imports what what if i just hit organize imports uh i think maybe for go so code actions are specific to the server so they're not like not like all of them have organized imports it may auto import it for you uh if you if you do that as well i just don't remember i just did it and it did yeah okay sweet there you go uh yeah so that code actions are super cool and some language servers implement just tons and tons of really cool um cool code actions uh and go please has some pretty good ones most of them around like imports and making and like fixing things that are annoying to fix manually in go but uh are good for a computer and i think what we'll do is we'll probably just copy in the like basically the just the default configuration more or less with some stuff deleted um just so that it's pretty simple later we could come back to it or you could watch my really cool take tuesday video about how to set up this plugin um but um but with with this plugin it basically provides the like auto complete stuff that uh you are used to from coc yeah that's awesome um so that's sort of like the last step once we do these then i think we should be really really close to like the experience that you had pretty much with um with coc for like the basics and we could tweak some more maybe like in when we do our next collab hashtag every month this is what we're gonna do yes this is true i hope you're having fun um we could do that we can do that maybe next month where you've had a little chance to sort of explore and test it out and then we can sort of fix those edges that you want to customize would be pretty fun i think yeah for sure i agree so uh let me i'll do a really brief sort of introduction to the idea of nvim comp and then we'll sort of just copy and paste some stuff and we'll make more sense of that later since we we don't have unlimited time for that yeah um basically nvm comp is like a completion engine so it it basically like provides a bunch of other plugins ways to easily um add completion candidates i guess it's like a good way to say it which is basically like any time you're typing and vim comp will be like hey uh does anybody have any good suggestions for for like what you might want to complete here and then it says uh yes i actually do have a few of these and then nvm comp like manages collapsing all those sorting them doing all this other stuff making it look pretty blah blah blah and so that you can basically write little plugins uh to provide completion without having to like re-create the wheel every single time right so that's sort of like that's sort of what's going on there so there exists a plug-in and uh in the setup part for envimcomp on on the github page there's like a list of several different basically like sources that feed into the nvim comp engine um and so the ones that we would need are like comp nvim-lsp that makes sense because that's going to connect with mmlsp and you probably also want like buffer and path and i wouldn't do command line right now because it changes a lot so i would just do like the nvmlsp buffer and path ones those three um are good i got a question in chat as well uh am i going to push these edits yes i will update the uh i will update my dot files nice yes uh okay so that part is so that so that's basically why we're installing these several other plug-ins though is because right nvmcomp is just like an engine and these guys are all different sources that go into this engine okay so that's why we've got a bunch of these other sort of like random things like why do we have to install four things for one thing well that's why right there they're basically separation of concerns for this idea and that's the only way that uh you can install a bunch of them yeah so then further down in the setup there's a big sort of block of stuff that you can copy and paste um and we'll just probably delete a few of the a few of the things from inside of here but we can basically copy everything from that set complete opt to the bottom of this bottomless and we'll just paste this in and we'll delete a few things out of here that you don't need uh today basically okay and we can maybe revisit those uh another time you can just put that at the bottom of your init.bin problem oh oh i can or it could just go in is it lua or is it uh yeah we let's just paste it we can paste it in this one okay uh there's we'll just yeah yeah we're good we're good we're good um but it just has to be you have to change where that return mappings is right before that okay okay i'll put it i'll put it up here then perfect i think that's good yeah just before your telescope setup is good sweet uh and then we might need to delete those yeah perfect good all right so you can delete that eof there on line 75 and then there's probably another one up towards the top perfect we'll delete that uh and then instead of uh set complete opt like the way that it is right now you can just say uh well we'll do it the easy way right now which is you can just say vim dot command at the front of this line oh sorry oh not delete those okay okay oh i'm not being specific enough let's uh uh well let's actually do this the cool way i'll show you the cool way how to do this we have we have time we have time to do the cool way okay um oh opti just opted just got me oh nice or did we get a few of these they don't know how to use telescope oh my gosh okay well i need to save sorry one second let me quickly uh save this image that's awesome and i need to uh tweet this really quick one second everybody i'll do it after i'll do it after i don't get too distracted here i'll get too distracted otherwise okay um okay so so the cool there's a cool new way that you can make some options in lua so let's just start on the line beneath this uh line okay yeah that it that has thumbnail possibilities it definitely has thumbnail possibilities thank you chad um so instead of set there's we made this new thing vim dot opt in lua which is sort of like a helper function to set vim options so you can do vim dot opt dot complete opt okay so this is basically like a sort of equivalent to the um like lua or the sorry the vim script way of setting options uh no no underscore is it just lower you can just set this uh yep all lowercase right just just exactly like uh the way that set complete opt looks but you're gonna put it for vim.op and then you're gonna say this equals and so the cool thing for this is you can actually make a list of these so you can just do like a left uh left curly brace right to open it up uh because tables and lists are the same thing and then just do like quote menu quote comma quote menu one quote so you just basically set this option like uh like this that's pretty cool yeah so this is like our attempt to sort of like bring the vim like dsl right like the domain specific language of like set options because it is like its own little mini language just trying to bring that inside of lua but without uh like lua only has so many like language features so it's like our closest way of doing that but yeah that's basically what's going on there and so then you'll need to just delete the line above this uh scrabblemess what's the difference between vim.o and vim.opt bim.opt has a bunch of things that are a lot nicer about it than vim.o like you can't do this with vim.o to set like a list of things that's not a that's not a possibility vim.opt allows for these sort of like more idiomatic ways of doing it but so i'm going to have you install to two other plugins okay um back in the in the setup there's a mention of for lua snip users we're just going to install luisnip because i trust that after i make my next um my next youtube video about this you're going to want to do this to use this more but for now we'll just make it work with uh compy uh oh did we have some sort of oh we had an error inside of uh bash money let's can we open up the lua file again i guess before we get too far yeah we must have uh so oh wait i can see what the error is again if you oh yeah i think i saw it it says that you don't have comp nvm lsp did we install everything and then plug um did we install everything install again uh that's a good idea just in case i thought i did but no i didn't okay cool all right so we'll just quit and then start again sweet that's good um that error we just haven't gotten far enough down yet to to fix so the two plug-ins that i'll just put them in in chat they need to install um this is another sort of idea within uh nvm comp is it's not it's not trying to do everything so there's these two plugins these are basically snippet expansion plugins which you won't use for anything right now basically explicitly but effectively what happens is the lsp sometimes sends you a completion result back so it says hey here's here's a thing that might complete what you're currently typing except it has like a bunch of sort of meta characters that tell the editor to do bonus stuff like oh also like expand this to be the current file name or like put these other things inside of this or so it's basically just like a way for the lsp to tell the editor to do bonus things and these snippet like managers these snip these snippet plugins like understand how to do that and then comp asks them to figure it out instead of like trying to write all of that logic in envim comp does that make sense yeah it does it does sorry i got i saw the glimpse of the double cursors again and it just it really got really it is good okay sweet so now we can go back to that lua file that we were editing before and then okay oh yeah it's it's weird right now because of we haven't finished removing all the settings for some stuff so okay um we need to all right so now we're inside of this nvme comp area you can delete the comment that says required oh yes online 22. and then we want to uncomment the line 24 because that's the lewistown button we can just delete all the other stuff in there so delete the snippy and delete the vsnip anonymous thing on line 23 as well this is basically just saying okay pick which uh snippet plug-in you have okay you are picking lewisnip here's what you need to do so that's all that's going on there okay um here are some mappings that basically like uh allow you to do different stuff within comp and comp will handle like making sure that these uh get called at the right time what do you what button do you like to use for completion like to ask for completion or to like select stuff to use tab yeah usually it's tab oh no casual tab user okay um so i think on the wiki it has a bunch of stuff for things that you could paste in for this um i don't know if we want to we probably don't even need to to do it right now i think we can just do one of those users tab everyone knows in chat they're already like man's game chat they know they know how i feel about it um i think we can just change tab to be i actually don't even know i don't even remember teaching i like genuinely don't care it's just what coc made me do okay um so let's do let's let's wait a second and why we'll get to choosing a mapping for that in a sec uh control y for selecting stuff so let's um let's um let's delete uh actually i think all of these are pretty much these are pretty much fine we'll get there shortly we don't need this down in the sources so down on line 39 let's delete v-snip because we didn't install v-snip so we don't need that um and then yeah we can do that and then let's keep going down i would delete this comp setup command line so just delete this whole area here and the next one as well beautiful and so now here's where here's where uh things get somewhat they get a little bit confusing if you're not understanding what's happening so i'll explain what's going on here um we actually want to put this capabilities this this variable at the top of this file this basically uh capabilities in this sense are a way of saying um to a language server what your client is able to do okay right so like the clients and servers tell each other at the beginning hey um i would like to be able to complete stuff can you do completion and they're like yes oh i can do completion so this is basically a way of telling like the client and server synchronizing at the beginning to understand uh what like they can both do so we need to pass these updated capabilities to go please so if we just like delete line 46 move it to the top of the file we can pass um this to go please uh so should we should i put it in the setup just put this like on line one basically just paste it at the very top of the file yep and then inside of go please dot setup we'll pass in a new key so like before or after the function uh but not but not inside the function uh oh right this is inside the function okay right uh before yep and so you can just say capabilities equals capabilities yeah and then a comma cool so that's that basically set up and now at towards the bottom of this area here there was like a lsp config thing um oh sorry beneath your like comp.setup which was just some example code of how you would pass this uh like i think it's a lot like around 50 or so 950. yeah we can just delete that last little block of code that doesn't actually oh this one makes sense that's what we did with go please right all right cool perfect so we can save this um and then we can just quit out of this or we can just open up your go we can open up the example go file and we can just test to see if things are working the way that you're hoping that they work so like just start typing something like on the next line and see if you're getting any completions sweet yep and so then i we have it currently mapped to i think enter to yeah select something yep and then you can start typing inside there first try chat copy it does need to get past every server yes that's cool that's okay cool so there you go so now we've we've set up you can go to definition you can get documentation you can um ask for implementations uh one thing we didn't do was references but it's basically like the same ideas like your gi and stuff like that so like you could do gr for go to references and that'll show you all the places that something's done we set up diagnostics renames and now completion so i think that's basically like you are set to the point basically of where you were with coc yeah this was better my coc like my auto complete and stuff was always broken and this is way easier sweet very cool and uh the thing that we didn't go over a bunch yet as well is like telescope has a bunch of uh like things with out the built in lsp right away so if we go back to like your main.go file and we like go and we like move to wherever you had uh like pr project reference or whatever like any of these like variables yep um and we go to like print projects or something like that i think is probably like we go here and then we do telescope sorry colon telescope uh space lsp underscore references yep yep that one hit enter this actually shows you all the places that this thing is referenced throughout your project oh that's sick yeah and then of course this does fuzzy finding stuff as well so you could like type project i think or like main and then it'll like filter things down all those goodies right so like uh and you can discover the rest of those on your own and see what they all do by just doing like telescope space lsp underscore press tab and find all of the lsp things that are just built into telescope like by default so i really hope that this helps anyone else who maybe is getting stuck or just getting used to neovim anyone like myself uh yeah teej thank you so much for taking the time to really explain like i'm five this is great uh i'm super excited to get into next month's content as well yeah and just really appreciate everybody being here of course if you're still here in the video why don't you just leave a kappa down in the comments so that you know we watch to the end and we'll see you next month i'll also be posting the q a that we did at the end of the session and so you'll see that shortly uh being posted on youtube maybe even when it happens it'll be linked right here i don't know smash that like and subscribe true thanks bash all right see you later everybody bye
Info
Channel: TJ DeVries
Views: 83,288
Rating: undefined out of 5
Keywords:
Id: puWgHa7k3SY
Channel Id: undefined
Length: 76min 32sec (4592 seconds)
Published: Fri Jan 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.