In this article, let's configure Neovim for the Kotlin programming language.

This article is part of the Neovim for Beginners series.

The Neovim configuration files can be found in this repository.

Getting Started

Kotlin command-line compiler

We need to install the Kotlin compiler. For Unix-based systems, such as macOS, Linux, Cygwin, FreeBSD, and Solaris, we can use SDKMAN following the installation guide.

sdk install kotlin

Create a Kotlin Project

To make it easy for demonstration purposes, instead of using an Android application, we will create a Kotlin application using Gradle.

$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 4

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Project name (default: demo):
Source package (default: demo):


BUILD SUCCESSFUL
2 actionable tasks: 2 executed

The project structure of the sample application is shown below.

None
Sample Kotlin Project

Type "./gradlew build" to build the application, and "./gradlew run" to run it.

Syntax Highlighting

We already installed nvim-treesitter and it supports syntax highlighting for Kotlin.

We can use the :TSInstallInfo command to check if the language is installed.

None
Treesitter Installed Languages

Alternatively, we can install the kotlin-vim plugin.

Language Server

In the lua/config/lsp/init.lua file, add the line for the Kotlin language server.

local servers = {
  ...
  kotlin_language_server = {},
  ...
}

Once we restart Neovim, the nvim-lsp-installer plugin will install the Kotlin language server automatically. The automated installation is configured in the lua/config/lsp/installer.lua file.

None
Kotlin Language Server Installation

Alternatively, we can use the :LspInstallInfo command to manually install the language server.

None
Kotlin Language Server Installation

The presence of one of these files indicates a Kotlin project root directory.

  • settings.gradle
  • settings.gradle.kts
  • build.xml
  • pom.xml
  • build.gradle
  • build.gradle.kts
None
Kotlin LSP

Debugging

For debugging, the available debug adapter is kotlin-debug-adapter.

However, I am still unable to make it work correctly with nvim-dap.

kotlin-debug-adapter

We will install the debug adapter under the $HOME/.local/share/nvim/ folder using the following script.

Neovim Configuration

Create the lua/config/dap/kotlin.lua file with the following configuration.

In the lua/config/dap/init.lua file add the new configuration.

local function configure_debuggers()
  ...
  require("config.dap.kotlin").setup()
end
  • We configure the command to use the installed Kotlin debug adapter.
  • The main parameters to configure are projectRoot and mainClass.

If we open the App.kt file, set a breakpoint, and start the debugging by calling the :lua require'dap'.continue()command, the debugger should start.

However, there is an issue with it working correctly with nvim-dap.

None
Kotlin Debugging

Check out Learn Neovim The Practical Way for all the Vim/Neovim articles!

If you are not a Medium member yet and want to become one, click here (A part of the subscription fee will be used to support alpha2phi).