Setup Neovim for Kotlin Development
https://www.youtube.com/watch?v=v94B4BzCphU
无作者
Intro
what is up my neovim friends I hope you’re having a fantastic, day because I’m going to show you how to set up cotlin, inside of neovim, I personally use cotlin for work, and so I wanted to see if I could, actually make neovim, on par with how intellig, works there’s a lot of history that I’ll talk about in this video, and this is kind of a segue into setting up Java, in neovim, because I’m going to explore, that next so stay tuned and hit, that subscribe button if you’re, interested in seeing, a Java setup video in neovim, if this is your first time on the channel welcome, and I hope you stay around and look at all the awesome neovim, goodies that I’ve outlined for, you hit that subscribe button, if you want to see more content, I’m also going to do some other videos outside of neovim, so stay tuned for more of that rewinding.
Background on Kotlin Language Server
back a little bit if you’ve done any cotlin development, then you’re probably doing it inside of the intellig, IDE, which is provided by the company Jeet brains, now Jeet brains was the company that created the language, but they aren’t really interested in separating the language server from the IDE, if you don’t quite understand the differences between an LSP, or a language server, check out this video in the top, right and it’ll help you understand, the basics of what’s going on, there generally a language server, can help you utilize, autoc completion, goto definition, and other features that we’ve come to rely on inside of our editors, now if you have a language server, then any client that supports the language server protocol, can actually use that language server, unfortunately, like I mentioned before jet brains has both of those coupled, and this post right here shows kind of the history of what’s going on, and when the request actually happened, I don’t believe any movement, has been on this there’s actually.
been some talks about using an, embedded neovim, inside of jet brains, but if you want to do any Vim development inside of intell, then you’re going to have to use the ID ofm, I also have a video on configuring that in this top right hand corner, but let’s talk about how to set up neovim.
Create sample Kotlin app
before we get started in the config in neovim, we should have a way to test our configuration, so let’s create a sample cotlin project, that way we can have something as a baseline to see how our syntax highlighting, autoc completion, and things like go to definition and go to reference are working, for that I’m going to be using sdkman, which is on this page if you haven’t installed it already check it out, there’s other options like ASDF, or or installing it manually, but I prefer sdkman, for managing my cotlin and for my Java projects once you have it installed, you can use SDK, install and cotlin, once you hit that this will install the latest version, if you want to pin it to an exact version, then the version I’m using in this video is 1.19.22, so if we do this then we should see that that’s already installed, now we will also need the build tool that builds our, projects and that is called Gradle, so if we do SDK install, Gradle, then we should see that this is already installed with version 8.6.
now that we have our tools installed we can create a new project using Gradle, so let’s make a new directory called neim example, we’ll go into that directory, and then from there I’m going to clear this so we can see a little better, and we’re going to use this command called Gradle init, and this will generate a new, project, now I want to create a sample application so I’ll hit two, then we want to use cotlin because we’re not using any other languages, and I don’t want to do multiple sub projects so I’m going to hit no here, and then I’m more familiar personally with the build.gradle script, being in groovy, so I’m going to leave it as that just for personal preference, project name I’ll leave it as default, and the Java minimum version I have it as default because that’s a good LTS, or long-term support, version so let’s just leave it as that and then we’ll have one final question, which is do we want to generate, using new apis and behavior I’m, going to leave this as, no, all right now if we clear the screen.
and we do an LS, you should have a project that looks like this, I’m a big fan of the EXA command so if you do xa—, tree, then you should see a project that looks something like this.
Kotlin config options
in researching, for this video there’s two main options that you can use to configure neovim, for cin development, and the first is using the built-in LSP, which is what we’re going to do today and the other option is to use COC, and that is a whole different way to configure tools inside of neovim, it’s not using the built-in LSP, there’s this page right here that outlines, some of the differences between different LSP options, it seems like the built-in LSP is a little bit more performant, so that’s why I’ve opted for, this and that’s how I’ve configured, everything else in my setup, I’ll leave a link to this in the description, so you can check it out yourself, and kind of decide which path, you want to go down we’re going, to do the built-in LSP, option so let’s get started with that for our setup.
Configure Kotlin in Neovim
I’m going to use a base installation, of Kickstart inm, which you can find here definitely, a great way to start your neovim, config if you haven’t already, gotten something or you’re not using a distribution, so if we open this up, then I have this Alias as km, which if you do alas, km here you can see that I can launch different neovim installations, using this invm, app name something to really check out if you want to have multiple, distributions, or multiple versions of your neovim config, now let’s swap over to our project, and we’ll see how this looks, without any cotlin setup, so if I hit command K this is something that I have set up to swap between, different t-u sessions quickly, so if I jump to the neovim example, then we’re back here and I can do km Dot, and we have our different files and setup here if we open up and we look at, the app.KT, then we should see that there’s a lot of different, errors or different things going on, and we don’t really have any syntax highlighting.
we don’t have any kind of autoc completion so if I did Val, then we get some buffer complete comption but nothing else, now let’s configure our neovim setup inside of our Kickstart inm directory, so we’ll open this up and we’ll go down to the init.lua, file, open that up and this is where, all the main things are happening, inside of Kickstart, you can see a nice explainer at the top here, we want to go down to something called, servers, now there’s a couple of ones that are here but we want to find, this block where it’s talking about configuring different servers, inside of here we will add the, cotlin, language, server, and we don’t need it to have any different config, so we’ll leave it like this add a comma at the end, and this should install the server so this gets picked up down below, inside, of, this block which is a table where LSP config, is going to configure our servers and automatically install them, now the pieces of this are using Mason, for grabbing and installing the server.
and then Mason LSP config and then invm LSP to configure, both of those servers, from the Mason side and from the invm side or neovim, side like we talked about earlier in the video, the cotland language server is not supported by jet Brains, it’s actually open source and it uses the cotlin compiler, like it references here on the GitHub page, and if you’re interested, in helping support it then this repo is always looking for more people to help, update and maintain This Server, so because of this I think that there’s an upper bound or upper threshold, to where cotlin can only be so good, because it’s not getting the support it needs or using a real language server, from the ones that are creating the language, all right now that we have our LSP configured, with our cin language server and it’s getting automatically installed, then let’s go back to tree sitter, which is going to give us our, syntax highlighting, so if you look for tree sitter, then you’ll see a block that looks something like this which is inure installed.
we want to add cotlin to this, list, if we do that then we should have both cotlin syntax highlighting, and also our cotlin LSP configured let’s jump back over to our project, and we’ll go to our NE ofm example, open up this, and you should see at this point the language server and treesitter installing, if we search for our app.KT, then we should see some really great syntax highlighting, and we should be able to do different commands with our LSP, now if you hit GD to go to definition, then you should see a jump here and from here if you do gr, to go to reference you should see this nice search window, and you can go up and down to see the different references, and go directly to that, awesome.
Setup ktlint
one of the other tools that people like to have on hand, whenever they’re doing cotlin development, is KT lint, which is going to format your cotlin code and lint it if you do format, which is built into Kickstart, then you get some nice formatting, here if you want to have KT lint installed explicitly, I’ll show you how to do that now, so let’s jump over to our config again this is our init.lua directory, and if we paste in another tool that is the Mason tool installer, this is what we need in order to install PT lint for us automatically, Mason you can configure to install different language servers, but you need something different to do the tools, let’s jum down to where we have Mason set up here and now we’ll configure, our Mason tool, to, install, the KT lint, tool for us so that we have this automatically, and we don’t have to fiddle with, whether it’s installed or whether, it’s not there’s a lot of different, options that you have to be able to run KT lint, my favorite personally is use conform, invm.
and so we’ll install that tool, now so if we go back up to where, we have Mason installed, here, let’s paste another block, if you’re interested, in learning a little bit more then check out this video about configuring, linters, and formatters, and this is highlighted in that video basically what we’re configuring, is some formatters by file type so we have KT lint used for cotlin, and then we have a nice key map where we’re configuring, leader L which for this configuration is space l, that way you can trigger the formatting whenever you want, and the LSP is a fallback, once we save this we should open backup neovim, in our example project, and we should see conform get installed as well as the Mason tool installer, as well as KT Lin all right we’re back over here in our example project, and you can pop open lazy to see that conform, actually got installed, looks like it’s not loaded yet because we aren’t in a buffer, but that’s okay, we’ll close this, and then if we want check that, our KT lint tool got installed.
then we can do Mason, and we can see that KT lint is loaded and installed here now, let’s open up our app.KT file and we can see that if we, do this and we hit lader L, then we get it nicely formatted, just like we would if we were using KT lint on the command line.
Outro
awesome, now you should have Colin all set up and ready to go, again I’ll caveat this with, it may not be as great as intell J so I personally use that as a fallback, whenever things aren’t working correctly, I’ve seen that this starts to slow down in larger projects, but I’m going to keep testing it and we’ll report back on what I find, thank you again for watching, if you have other neovim, content or items that you want me to cover in the future, leave a comment down below, also check out the description, with all the links of things, that I talked about today, and I will see you in the next one thanks everybody.