C++ Coding with Neovim - Prateek Raman - CppCon 2022

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone this is a talk on C plus plus coding with neovim I am pratik I work at Bloomberg everything in this talk is based on might not work okay okay they're going to work on it I'll just speak shout louder so everything in this talk is based on the C plus the setup I have at home a close approximation of which I also use that work I'm a regular C plus plus application developer who just happens to be enthusiastic about his Dev setup sometimes overly enthusiastic uh this is the current setup for this talk before we start I have a confession even though I've been using Vim for more than a decade I still use notepad plus plus okay I'm going to pause just a little bit yeah this is the argodox easy mechanical keyboard and okay we are back online okay I'm going to continue with the talk so yeah before we start I have a confession I've been using Vim for more than a decade but I still use notepad plus plus sometimes you know it's fast it's minimal it barely uses any resources but in my opinion it's most important attribute which keeps me going back to it is that it gets out of the way when I'm using it so this is more of an observation I had while preparing the slides for this talk as I feel the current setup on the command line using neov in which I've gravitated towards I've done that for very similar reasons so with that let's get into the actual talk this is what we'll be covering today in the next 50 minutes or so after a brief mention of some housekeeping items I'll set up the context around why this is a talk on neovim as opposed to whim then I'll talk about the command line setup I typically use when working with C plus then we'll spend a lot of time with C plus plus workflows with neovim using some live demos and then finally we'll take a look at how one could get started with neovim to have a similar workflow and I'd prefer to take questions at the end of this talk uh just because of the nature of the talk so please take a note of the slide numbers at the bottom right corner if you have any questions at the end one of the key developments over the past five to six years which enables most of the rich editing experience we'll be covering today is the language server protocol or LSP a language server is a language specific server which understands your project and your code base at an abstract syntax tree and a semantic level so a development tool like neovim can talk to a language specific server like clang D for C plus and ask for things like where's this function defined where's this type declared do you have any completion items for the current cursor location in my editor and then neovim can use that information to provide you with intellisense and neum can do this because it has a built-in LSP client which allows you to have intellisense like you would expect from any IDE you don't have to understand LSP or language server to appreciate the content of this talk I'll be using the terms LSP and language server throughout the talk so that's why we have the slide here I hope by the end of this talk uh the command line feels a bit more familiar friendlier than before I also hope to demonstrate neovim's built-in terminal user interfaces it's Rich LSP integration and a lot of its LSP aware plugin ecosystem which uses the LSP to provide for enhanced functionality whether you consider the command line as an alternative to GUI based IDs so that comes down to preferences along the way we'll take a look at a few things under the hood which is usually hidden away from us when we are using an IDE we don't have to be aware of it understand it fully but being aware of it helps us debug things when things don't work the way they are supposed to I do assume some C plus knowledge and some cmake knowledge and I'm using VC package as my packet manager and everything I'm working with is running in Ubuntu in wsl2 on Windows but it should work the same on any Linux distribution or a Mac foreign so let's set up the context around why the stock is around neovim I'll talk briefly about Bim then how neovim came about and what the status of new is today I'm sure everyone here has already heard of him a lot of people's first interaction with him is unfortunately accidental you know they probably ran git commit and then felt trapped inside of a black screen with a cursor which is not even blinking and the keys don't seem to work and this question on stack Overflow has been viewed at 2.7 million times so this is a bit amusing to me because from where I stand this is if there is a very good reason for why things happen the way they do and it's not wim's fault and if my git commit theory is correct then this happens because of the mismatch between gits default and bims default so let me explain whims key Innovation which it inherits from VI is that it's a modal editor so when we when we press keys on the keyboard and corresponding letters show up on the screen that's what Bim does in insert mode but when we launch Vim we are in normal mode by default so it has like normal more insert mode and a few other modes and in normal mode we speak the vi language to interact with the editor as a whole and we are operating at more than a character level granularity you know where we press a key and it types a text so we have to press one of these keys to go from normal mode to insert mode then we can type whatever we want and it'll show up as a regular editor it'll behave as a regular editor we either have to press escape to get out of the normal mode get out of the insert mode into normal mode and then we have to press a colon command to go from normal mode to command mode and then to save the file we do colon write to write to quit when we do colon quit or we can do WQ for short which does both of them at the same time so I'm not going to be covering the Wim language in this talk because I've been using Vim for more than a decade and I'm still learning and this is one of the best resources I've found to understand whim comprehensively what I will say is that if you're learning Vim on purpose what happens after a while is that you develop muscle memory to interact with the editor and your brain is able to do that passively while you're actively able to just focus on the core at hand so apart from visually not cluttering your vision it also does that with your attention span in focus and there's a reason why vimkey bindings are a feature in almost every other editor I'm aware of including all the web browsers via plugins so if Vim is so good then why is this talk about neovium well I personally don't consider neovim as a completely different thing from them and I don't I'm not being diplomatic here because when you see me in the demos and I'm describing features and neovim everything which comes from Vim I just say this is a built-in whim thing and a lot of that and that's because Wim Bill neopin builds upon the rich lineage of Vim just as whim built upon the rich lineage of VI so but for technical reasons we'll have to revisit the history a little bit so back in 2013 I was using Vim with this syntastic plugin when working with C plus plus and the way it worked was every time I would save a file it would run the compiler on that file in the in it'll parse the output if they're very there were any errors it will show them in line but Vim didn't have a concept of a background job then so every time I would do a w my editor would freeze while the compiler ran on the file because all the plugins ran in the UI thread as well so as you can imagine this was a very disruptive workflow and I was always keeping an eye out for Solutions neovim was announced by Thiago in January 2014 after his efforts to add async support to Vim didn't really go anywhere you can still visit that fork of Wim link to see his original Fork of him with async support so new web is essentially a whim for with modularity and extensibility in mind and not just to add async features there's a neovim core and a well-defined protocol to interact with the core and all the uis foreign are built around it the first stable version of neovim was released in November 2015. Vim eventually added async support next year but for some reason I was already sold on the idea of neovium even though I didn't adopt it immediately today has come a long way because one of the key decisions Thiago made with neovim was that he chose Lua as the scripting language instead of Bim script even though web script still is still supported the LSP client is written in lower and all of the Lua based plugins in neovim feel like first class citizens because they have access to all of the same apis which neovim itself does neovim also has three seater support which is a library for incrementally parsing a file and I'll show why that's useful during the demos so it's no wonder that neovim was voted one of the most not one of the most the most loved editor uh most loved IDE in this year's stack Overflow developer survey and it had the same position last year as well so this is a talk about neovin because that's what I use but I'm not the only one excited by it as you can see all right so let's talk about the command line environment I'll briefly talk about the terminal uh I'll talk about the project we'll be using for the demos and then new of him just a little bit like the basic setup and I'm only covering these parts here just to build familiarity with the demos when I get to them but you don't have to fully understand them right now so this is a typical screen which I spend most of my time looking at when working with working on any project or writing documentation and as you can imagine I would like it to be a bit aesthetically pleasing I'm glad to have found that my wife likes this as well I recently gifted her an RGB mechanical keyboard to see if I can nudge her in this direction not on any special occasion just a random surprise gift so we'll see so let's break down what we are seeing here this is Windows terminal running Ubuntu running a TMax session and I'll get to what tmux is in just a bit but if you were using Mac you would use the Mac terminal if you're using native Ubuntu you'll use the gnome terminal or alacrity or Kitty or wisdom any terminal you want and what we have here are two tmux pane inside of a T-Max window inside of that tmux session we I talked about so the top pin is running near him when I'm working on code I'm always zoomed into that pane so you don't see the bottom part and you can see the majority of that pane is just code with there's a single line at the bottom with some information about the file I'm working on the bottom pane is just the bash shell which I'm hope people are familiar with so tmux is a terminal multiplexer so rather than me running multiple instances of Windows terminal and then running Ubuntu and the new women one and then Ubuntu and Bash another and any other shells I want I let tmux handle all of that via a keyboard driven workflow and in the next two slides I'll just show a brief GIF of that workflow in action and you can see the keys I'm pressing to get that in the top right corner in a yellow background so I'm going to create a new TMax window then I split the pane into two so I have two shells and I'm just using the keyboard for that Ctrl BC to create a new window and then Ctrl b in a pipe to create a split so this is how I would multitask with just staying within the command line in the next slide I'm doing a little bit more so I'm I'm going to the left pane open neovim copy paste a hello world program save it go to the right pane run the compiler on that file and then run the executable so tmux lets me stay within the command line environment and allows me to multitask via a keyboard driven workflow both neovim and Vim have a terminal mode which allows you to stay within neovim completely and just run shells within that but I already had a tmux driven workflow for a long time so I don't have a reason to switch to that yet but if you're not used to tmux then that would come in handy okay let's talk about the C plus plus project I have a really basic C plus project a command line application with just as addition and subtraction uh I have a link to the code which I'll update on GitHub once I'm done with the talk and when you compile and run it doing help gives you the usage add and sub and you run ad and sub like you're seeing on the screen right now so really nothing to think about here really basic application a couple of things to note I'm using cmake As the Bell System generator and Ninja is my build system and I have GCC 11 installed on the machine I'm using VC package to provide my dependencies the two dependencies I'm using here are CLI 11 for command line parsing and fmt for input output and you may have noticed this uh tasks.py file in the directory listing so I'm using a python tool called invoke which works the same as you would expect a make file works but it's in Python and I'm using that to drive my command line workflow for CMA configure Bell install yeah so tasks.beaver achieves that same purpose this is just a reference slide here for completeness I'm using VC package as a sub module so that all of my dependencies are pinned to whatever version they were specified in in the VC package commit I have as a sub module and you have to run a few steps for for the first time to get that working so these are the steps you have to do so the first thing you have to do with cmake is to configure the project which generates the build system for us and I use invoke for that as I mentioned earlier which uses my tasks.py file and that gives me this config command which when I run it it roughly does the following like it creates a unique build directory for my project CDs into it it then runs cmic with a path to my project as the source directory and the tool chain path the VC package tools chain path as the tool chain file and that's how CMA can find my dependencies this is the build folder we'd get once we run that command the build.ninja file is the build system there's a VC package underscore install folder which is where all the dependencies are CLI 11 and fmt there's also this compile commands.json file in the bill folder what's that so that is a Json compilation database produced by cmake if you do either of the things as shown at the top here so if you run cmake with a Dash D flag cmake export compile commands is equal to one or if you run if you set the environment variable cmake export compile comma is equal to one and then one cmake you get that file it's kind of a de facto standard for providing information from the build system on how every file in your project was compiled I'm not going to discuss right now what's in that file but we can get into this if we have time at the end so all we need to do to make the neovim LSP client connect to a clang DC plus language server when we are working on any c plus code base is to have a compile commands.json file in the root of a project and I'm doing that via this invoke config so it not only runs cmake configure for me it also creates a Sim Link in my source directory to the compile commands.json file in my uh Bill directory this is again mostly a reference slide I wanted to highlight a couple of indispensable plugins with neovim if and I'll bring them I'll bring them up again as I'm using them in the next section Mason is the plugin which installs clankdy for me and it does it in a new local fashion and if it wasn't clear until now like when I say clang D server it's not a server running outside somewhere it's just running locally on the same system One Last Thing Before I switch to demos one of the things you learn to do with vim and neovim is to come up with your own key bindings for the most common workflows and you do that by having mnemonics to remember those key bindings you can come up with whatever key bindings you want as long as you remember them and Vim has a concept of a leader key which is like a namespace for your own key binding so that they don't interfere with wimps built in key bindings and my leader Keys space and I'm mentioning this here because in the next section in the demos I'll say leader but I'll type space okay was there anything I should revisit right now in the previous slides no yeah yeah uh so the question was was there any advantage of using invoke rather than running CMA commands directly uh so I'll get to Something in the demo basically if I was using the CMA commands directly I would have to type a really long command but invoke config just does that for me so I don't have to do that all right so I'll move on to the C plus plus workflows with me and this is the part where we get to play donkey every time I'll switch over to my terminal and every time I make a terrible mistake I earn a letter so let's see if I can avoid making an ass of myself interesting and I'll be the one who decides if it's a terrible mistake or a regular mistake I'll also start a program at the to show all the keys I'm pressing here okay okay so you can see the keys at the top right corner all right okay so this is the screen we showed earlier in the introduction of the command line environment section and uh I'm going to cover up a few more things here main is the name of the tmux window I'm in and I have two paints the top pane is where I'll run neovim and the bottom pane is just running the bash cell uh 16 cppcon project is the name of my session and I have a few other windows to the right and because I am zoomed in so much you cannot see that but when I'm working with regular font size I have a lot of space available and I can see a lot more information on the screen so yeah so this is what I wanted to show about in book I have all of these commands configured there and they do what I want them to do I wrote the code for that and one of the things I have done here is LS which will just show me all the files in my current directory so let me clear out the screen and do invoke LS so this is the project we are working on and you can see all the files here there's a source directory is an app directory this is a toddler math directory because it's really basic math and uh I have a bunch of files in there add subtract adder subtractor so I'm going to open up Neo Bim I don't have a compile commands.json file here and I should have asked if is the font size good right now yeah okay okay so neovim has whim has built-in file explorer called net RW it's a built-in plugin but I'll be using the telescope plugin to browse the files in my project and my mnemonic for that is just files in project so I'll type leader FP and when I do that I get a pop-up so this is an example of a terminal user interface it's pretty minimal I get a prompt at the bottom which I can type things in and it'll filter the results at the top I'm going to zoom out just a little bit to show what happens so it also shows me the preview of the current selection in the list of files if I type CPP it shows me all the CPP files and I'll I can move with the arrow keys and it'll show update the preview on the right hand side I type main it shows me the preview for Main I'm going to zoom back in just so that I can see the code better and I press enter okay so the this line here is called status line I'm in normal mode right now as I mentioned when you launch neovim you launch by default in normal mode I'm on the main branch and the file name is app underscore Main and nevim has detected that this is a c plus file and I've configured neobim with clang D so that if I open a c plus file it should attach to clang D for me it'll launch clang D and then attach to it but I've also configured Clank D um before I actually go there I'll just show this so this command tells me what language server is attached to the current buffer and buffer is just a file loaded into memory in vim and it says zero clients attached to this buffer so that's because I've configured Clan D to not attach to a c plus file at all if I if it doesn't file a compile commands.json file because otherwise I'll just get a lot of red squigglies because it won't be able to find all the headers I'm using in my project it doesn't know how to so I'm going to quit this pop-up even without the LSP there are a few things I can demonstrate here so I have the same project checked out in a different location I'm again going to zoom out a bit and this time I'll use the Vim wimps built in netrw plugin because by default telescope only does a fuzzy find for all the files in the current git directory but I have a different uh get directory I have that filing so it's the exact same file same content but different file and I want to demo three sitter syntax highlighting because by default Vim will use regex based syntax highlighting and tree sitter is a incremental parsing Library so it understands our code at a abstract syntax tree level sorry so I've disabled syntax highlighting on the right side and I'm still I still have syntax editing on the left side I've disabled pre-seater syntax editing on the right side so it's doing regular regex based highlighting so you can see on line 15 the add number options functions now it's on white on the right hand side the CLI namespace is white the app classes by the small app object is also white but on the left hand hand side all of those identifiers are being highlighted with different colors so let me do just go down a bit here you can see a lot more information on the left side at a glance visually because things are highly different kinds of identifiers are being highlighted with different colors here so that this is all coming from the three-seater based syntax highlighting so I'm going to close the other file um let's go to here another thing which tree sitter does it it gives us text objects so in Vim this is a whim thing I'm as I said I'm not covering the language but I'll just mention what I'm going to be doing here if I do VW it selects this word so B is Visual and W is word and VW just does visually selects a word and so a word is a object in Vim it's a built-in object but with tree sitter we get even more text objects so I can do vif to select visual inside function so I select I can select the whole function here if I do v a f it'll select the the entire body of the function including the top definition so that all comes from pre-seater so it comes in very handy it can do this with statements as well it can do this with control blocks as well you'll have to configure presetter to be able to do those things but I've just done it for the functions so what I'm going to do is I'm going to uh just checking my notes to see if I'm missing anything yeah I'm going to move to another checkout of the branch where I've already run invoke config so there is a compile commands.json file in the root of the project so invoke LS so here you can see there's a compile commands.json file which is a Sim link to the same file in my build directory which was created for me so now when I open neovim and I perform the same action to open telescope then I go to Main and I press enter on the bottom right corner you can see clang day is indexing the project it happened a bit quickly so let me do this again and I'll have to remove the cache folder which clangdi had created when it was indexing the project so now you can see on the bottom right corner clang D has attached and it's indexing the whole project so it has indexed the all the files in my project and I can confirm that by running the same LSP info command and this time I see one client attached to this buffer and it knows what the root directory of my project is so the next thing I'll do is I'll add multiply as an option to this program right now it just has ADD in sub and I'll do it in two ways I'll do it first without dlsp and then do it again with LSB so without the LSP I'll have to do LSP stop and LSP info will tell me that there are no clients attached to this buffer so I copy this line go to sub change sub to mull come here change it again so I'm not getting any intellisense like completions I should get some buffer based completion because of some plugins I'm using but if I made an error here I would not be informed of that uh I'll have to add num1 and num2 as options to that new sub command so I do that by just calling the same function on the child app I created and then I go to this line I do else so I'm getting some completions but this is not the intelligent completion which you look forward to from an ID it's just coming from the buffer because of the D you see at the end there so that's just text um so I'm going to just copy the code block above and this one is just straightforward okay so if I would have made a mistake here I would not know until I had run the compiler so I'm going to remove everything I did and try to do the same thing with the LSP yes please so now Clank D is attached and the first thing you see is I'm On One identifier and the same identifier is being highlighted again because of the configurations I have I have for LSP if I go to app all the app identifiers would get highlighted and the same for any functions I'm on or anything I'm on uh I'm going to do the same thing copy the same line and then change sub to mult so as soon as I do this I should have got an error here what sorry check I was expecting an error here because I'm creating another variable with the same name maybe I'm going to quit whim and no it's uh it's real time feedback in irrespective of whether I have the buffer saved or not okay so I did get something here it seems I um I didn't save the buffer earlier maybe that's why you're right sorry so let me delete everything I did and try it out again so if I do yyp now I'm getting an error so it says redefinition of something and that's a diagnostic which I'm getting in Vim uh neovim has a unified Diagnostics API if I wanted to see that diagnostic because it's being cut off my mnemonic for that is just a square bracket D like the closing bracket goes to the next Diagnostic and the opening bracket goes to the previous diagnostic so when I press that I get the same thing but in a pop-up this is another example of a terminal user interface I also get yeah I'm just going to quit that so let's change it so the you can immediately see that I'm getting feedback from LSP that this add sub command method on the app sub app object has two arguments I've already typed the first argument and it's giving me it's telling me what the second argument is based on the function documentation so let me do that again but this time type it out completely so app dot add sub command so now you can see on the right hand side everything is coming from LSP and it's giving me all the methods on the app object I'll choose I can choose either of these options via built-in whimkey bindings Ctrl n for next or control P for previous and this is the one I want so I'll press Ctrl Y and that will be selected and here I'm getting there are two overloads to the add sub command the second overload takes another app if you want to keep creating more sub commands I'm using the first one so I'll just start typing that and it knows that I chose the first one because it's just highlighting the second uh parameter in the pop-up so now I go down I copy this line I go to S and come down here I'm just going to copy this whole line so I'm using fmd which uses format strings for formatting the numbers and uh you can type a format here I don't know what that format is but if I were to do something which I shouldn't be doing I'm getting a feedback from LSP again and uh let's look at what that feedback is like it gets it gives me the regular unintelligible C plus plus compiler error but you you get to know these things even before you have run the compiler and this is one of those examples where it would be really difficult to find out if you were not doing it if it was not actually doing what the compiler actually does so it's behaving very much like a compiler but it's just giving you real-time compiler feedback real-time feedback while you're editing your code so when you undo that and be done with it so yeah so that was me using me editing code without the LSP and then with the LSP and there are plenty of ways to do the same thing in neovim all the keys I'm typing to do move around change things there are more efficient ways to do this I'm just doing whatever is coming to me right now as I said like I'm just doing these things I don't I'm not thinking about what keys I have to type so one more thing I wanted to show here was if I were to make this change manually and I was doing it in a half Hazard manner where I'm not taking care of the format I have a clang format file at the root of my project and clang d has integration with Clank format and I have Clank format I'm not sure how it works whether it uses the Clank format I have installed on the system or just it does that on its own but lsp's protocol allows you to format the buffer and my mnemonic for that is just gf go format so when I press I haven't even saved this buffer so leader GF would just format the buffer based on the Clank format file in the root of my project so this is very handy you don't have to Shell out to Clank format and then run it over the entire project and you can just do it as you're coding along so let's create a few more trivial errors just to see how we deal with errors when we have them uh I'm going to remove this function call from here okay so now I have three errors you can see the diagnostic on the left left most column there are two x's and there's also a triangle with an exclamation bar and you navigate between Diagnostics using bracket d if I go one level up I'm getting a diagnostic which is not coming from clang D it's coming from Clank diary that's because I also have clanked a client ID file in the root of my project and client D has integration with Clank diary so it's giving me suggestions Clank tidy suggestions as well via the LSP protocols we'll revisit the fix available thing which you're seeing uh just in just a bit uh before we fix it there's a built-in whim thing called quick quick fix list or location lists and they're really hard to navigate and everyone I'm aware of uses legendary whim plug-in author Tim popes unimpaired plugin to navigate around with quick fix list but there's an amazing Lua based plugin for neovim which just shows you all of your errors and it's called It's Perfectly named Trouble and Monday morning for bringing trouble up is just leader x d x is for errors and D is for Diagnostics so when I press x d I have a buffer at the bottom which is showing me all the errors in my current document and I can use regular wimkey bindings to just navigate around them so I'm going to switch to the buffer above and fix the errors so I'll type colon here and you can see the error disappears from the trouble buffer below that's because like it's using real-time feedback from the language server to provide me with that diagnostic I move to the next diagnostic I fixed that as well and the error disappears and then I finally go to the this one I'm going to fix this in just a bit first let me get rid of the trouble buffer so here I'm getting a fix available from the language server for trivial errors the language server protocol allows the language server to provide some fixes which the editor can just apply and Monday morning for doing that is just CA code actions so when I do leader CA I get another pop-up which says that use a trailing return type so it wants to change end main to Auto main with an arrow and an end at the end and it's showing me a small diff as to what that change would be once it has done that it's showing me diffs in two stages but it's the same it's going to do the work so if I press enter here it is applied that fix for me so this I think is pretty amazing because a lot of small fixes like this if you have a if you have the right Clank ID configuration in your route in the root of your project you can just you you're not going to be needing this only when you're writing new code you might be doing it over an existing code base making small changes here and there and you can just locally do it for a function just to maintain backwards compatibility with the existing standards of that old code base all right so we are halfway through the demo I have some time left um so another thing I want to show is uh if I want to see if I want to explore the project I don't want to do it just by bringing up telescope and then navigating to different files so let's say I want to find where how is run implemented to add two numbers it's pretty obvious but if I was curious I can just go to that definition of that function and it's a very standard mnemonic called GD leader GD will take me to the definition of the function I'm currently on so leader GD will take me to the file where that function is defined I see that it's calling another add method here if I press these are GD again there it takes me to that function definition and I see that I'm using the standard accumulate algorithm and I can press leader GD here as well and it takes me to wherever that is defined in in my system so all of these locations which I've jumped to this is a built-in wind thing called jump list and all of these locations have been put into jump list and you navigate around the jump list using Ctrl o and control I so if I press Ctrl o i jump out of that place to all all the way up to main I can press Ctrl I to just go back there as well there are other ways to do this where it'll show you this in a pop-up so that you don't lose the current buffer you're on I have configured every I have everything configured the way I like to use them because I'm comfortable with this workflow so let's go to the definition again and I'm getting a bunch of Diagnostics here again and they're all Clank ID Diagnostics so I'm going to apply this diagnostic nested name spaces can be concatenated and there's a fix available so if I press Monday morning for that code action it and press enter sorry and press enter it concatenated the namespaces now at the bottom I'm getting another diagnostic which says this is a diagnostic for the comment I have that the comment is referring to the wrong namespace and there's a fix available for that as well and it'll fix the comment as well I'm really impressed with Clank tidy if you're not a if it wasn't not coming across yet uh okay yeah I have a bunch of fixes here I'm not going to get into that but if I were to if I were interested in where this where the header file for the C plus file is so client ID has uh integration for the Clank D has integration for that as well and my mnemonic for that is just GS go switch so if I do GS it takes me to the header file but that's not what I typically do what I typically do is I'll create a split so I have the same buffer in two windows and then I press GS to go to the header file so that I have both the header file and the C plus file in the same location um okay let's go to this m underscore nums so this variable name here M underscore nums if I was interested in finding all the places where this is defined where this is being referenced from there's a there's a way to do that with LSPs bill and my mnemonic for that is just RF references so leader RF brings up a telescope pop-up again and it shows me all the places where that uh variable is being referenced so it's just a header file and the C plus file and I might be interested in doing this because let's say I want to rename it so refactoring is also available via LSP and my I'm not I'm not 100 sure on whether it's just rename or there's uh there are more features available I'm just doing the rename here and rename I'll do via leader RN and when I press that I get a pop-up to change to put a new name there another example of a terminal user interface I type what I want to type and I press enter and it will it would change all the places where that variable is defined now immediately I'm getting an error and it says M underscore number does not name a non-static whatever that's because all the buffers have been updated but they haven't been saved yet and so M underscore numbers even though we had the header file has it changed it hasn't been saved so the language server isn't yet aware of that change to fix that I'll have to save all the buffers and in Vim you do that via write all and when I do that that error goes away okay so let's go back to main if you are working on a really long C plus file which is not uncommon with C plus and you want to just navigate to important bits like functions and definitions there's a way to do that in Via language server which is list all the symbols in the current document you can also do that for entire project but that's a lot of noise for that is just LDS so leader LDS will bring up another pop-up and it just shows me all the symbols in this document so you can imagine that if you were working with a really huge file and you want to just navigate to functions you can fuzzy find over them just quickly jump to that function so these are all the ways in which you would navigate around your project okay so now once I've we have done all of that I'm going to jump to the Shell and as soon as I did that it also zoomed out of the neovium pin I'm going to zoom in into the shell and I'll run invoke build install I blame template meta programming for this delay not sure which one is at fault whether it's CLI 11 or fmd okay so invoke info just tells me some Diagnostics about my project and I have an installed path in there I can just get that one by a topic flag and then I can CD to it and if I do three I have my executable in there use our local is a byte is a default cmake thing which you get if you're using cmake and install so to run the executable and I have my Mull command in there let's see if I have a bug five five so it works great okay um that was all the demos I'm going to switch back to the slides oh there's one thing more which I left so let me quit whim and open it again and go to main so one of the things I mentioned earlier was that the language server attached to a buffer and not the neovim instance and I have a tasks.py file here if I open the task.py file I can see that there's another language server the pyrite language server for python which is attached to this buffer so everything I just showed with C plus all the key bindings in mnemonics all of that would work for python as well at least within the protocol of LSP because clankd has some off protocol extensions as well so if I was interested in finding where all the functions here and just to see how my configuration is being done I do config search for that I see that do config is being called from config I press GB and it takes me to do config and so this is the answer to the question around why I was not using cmake directly because I have a really long CMA command because I'm specifying the build type c mate export compile commands the path to the tool chain file all of that and I'm creating a build directory before I do all of that and then below that I'm also creating a Sim link to the compile commands.json file which I mentioned earlier so okay yeah that's the end of all the demos so I'll switch to the slides so basically the setup Works whether you're working on rust or golang or JavaScript or typescript or Zig it just works the way you expect it to but you know if using if you're using an IDE IDs are usually based around really good ideas usually based on a single language so that's one of the advantages of using neovim before I proceed I'll close Karnak so one thing absent from the whole thing was that I'm not showing any debugging here that's because if I have to debug something I'll just fire up GDB on the command line and do it that way and there are really good Talks by Greg law you can just search on YouTube Greg law cpbcon and a bunch of GDP talks would come in there was a talk by Mike Shaw yesterday on the basics of GDB as well which is really good but like LSP there's another protocol called debug adapter protocol which tries to decouple the debuggers from development tools and there's a plugin called NVM dap which can allow you to have visual debugging in neovim I haven't tried it yet but that's how you would go about it if you wanted to do it in neovim itself all right we are at the final stretch I'm going to jump into the name setup quickly and I'll talk about how you would set up neovim if you were doing it from any if you're coming from any other editor butt whim and what you would do if you are already experienced film user and you won't want to try out new him and then a bit about Lua so when you launch neovim it reads this file called uh in dot config NVM init.lua and uses that to configure itself on Windows that will be in the user profile environment variable which is usually C user your username I'd highly recommend this kickstart.nv project which has a single init.lua file so you can use that and then build upon it over time there's a little more feature-rich LSP project LSP zero project which has a bunch more plugins and provides a bit more features if you were to do this from scratch this is the structure I would recommend you can create a Lua folder and then you can have you can do it in a modular fashion so here the init.lure file just has required weekend in there and that will include the weekend.lua file as a module in your init.l and that's a standard way in which lower modules are imported and if it wasn't clear if you're going this route I would recommend reserving a weekend before you proceed if you don't want to configure anything then you can go with pre-existing nearby distributions which come with their own key bindings their own plugins the whole kitchen sink and there are two of them which are really popular so this is what you would do if you were coming to neovim from any other editor botwim but if you're already an existing Vim user you can still try out neovim with all of your existing Vim configurations and plugins by just having an init.wim file instead of an init.lua file at the same location and having those three lines in there what those three lines do is they make they load all of your Vim configuration and plugins and your neovim should feel the same as Vim does once you are comfortable with this workflow after um after a while you'll start to realize that neoim is not very very different from Vim but it has all of this Rich LSP integration I just talked about and all of the Lua based plugins which behave like first class citizens so you may want to move to Inner wi eventually I personally went the route personally went the same route but at the current moment I have my vim and UV configuration separate my Vim configuration is mostly Frozen in time because I probably continue using any of him going forward I'm pleasantly surprised with how simple it was for me to pick up Lua without any prior experience with it before neoim and so what's happening here is there's a local variable called telescope built-in and I'm saying when I press LDS call this method function telescopebuilding.s telescope built-in dot LSP document symbols and Lua automatically creates a closure around that local variable for me so this is very understandable and I was able to just pick this up on the first try so in all my years of using Vim I never came close to thinking of writing a plugin in winscript but I definitely see myself doing more and more with Lua to personalize neovim and I learned a lot about near him and Lua from TJ debris YouTube channel he's a core developer of neovim who wrote the initial LSP client and also he's the author of the telescope plugin especially the long form ones where he's teaching his friend bashmani who is a newcomer to new him so she's asking the kinds of questions I would have had if I was getting started with it that's what makes those videos so helpful and one interesting thing I learned while learning about Lua discovered by learning about Lua was this is language called fennel which is a flavor of lisp which transpiles to Lua so if you are one of those people who would like to configure your editor using a lisp then neovim is for you that's the end of my talk thank you [Applause] I don't know if you have time for questions maybe one minute uh there is a question uh they said that can I use LSP with regular RV uh no with regular whim there is another ecosystem you have to adopt into called COC or conqueror of completion which is a node.js based ecosystem which just uses all of the vs code things in it but yeah not regular LSP no I haven't that's what I'm that's my next goal uh the question was uh do I have any integration with unit tests Frameworks like Google test I don't have it but that's the next thing I'm going to be working on with some Lua code so that when I'm in a function unit test body I can just choose to run that specific test yeah what was the skin for your Bash what was the what was the skin for your dash oh what was the skin for bash I don't think I have any skin there uh okay so the arrows that's coming from uh just something called power line so you can Google that there are a bunch of Frameworks available to make that happen all right thank you all for coming [Applause]
Info
Channel: CppCon
Views: 45,038
Rating: undefined out of 5
Keywords: neovim, neovim lsp, c++ coding, neovim setup for c++, command line, lsp, terminal user interface, vim, Prateek Raman, neovim c++, neovim cpp, LSP, Language Server Protocol, c++ vim, cpp vim, semantic auto-completion, intellisense, IDEs, C++ Coding with Neovim, modern IDE features, LSP client, c++ talk, cpp talk, c++, cpp, cppcon, c++con, cpp con, c++ con, cppcon 2022, bash films, cpp coding neovim, cpp coding, neovim c++ lsp, programming cpp, productivity cpp, neovim setup cpp
Id: nzRnWUjGJl8
Channel Id: undefined
Length: 59min 49sec (3589 seconds)
Published: Mon Dec 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.