IntelliJ Platform Plugin SDK Help

Kotlin for Plugin Developers

Why Kotlin?

Using Kotlin to write plugins for the IntelliJ Platform is very similar to writing plugins in Java. Existing plugin developers can get started by converting boilerplate Java classes to their Kotlin equivalents by using the J2K compiler bundled with the IntelliJ Platform (versions 143.+), and developers can easily mix and match Kotlin classes with their existing Java code.

In addition to null safety and type-safe builders, the Kotlin language offers many convenient features for plugin development, which make plugins easier to read and simpler to maintain. Much like Kotlin for Android, the IntelliJ Platform makes extensive use of callbacks, which are easy to express as lambdas in Kotlin.

Likewise, it is easy to customize the behavior of internal classes in IntelliJ IDEA, with extensions. For example, it is common practice to guard logging statements to avoid the cost of parameter construction, leading to the following ceremony when using the log:

if (logger.isDebugEnabled()) { logger.debug("..."); }

We can achieve the same result more succinctly in Kotlin, by declaring the following extension method:

inline fun Logger.debug(lazyMessage: () -> String) { if (isDebugEnabled) { debug(lazyMessage()) } }

Now we can directly write logger.debug { "..." } to receive all the benefits of lightweight logging, with none of the verbosity. With practice, you will be able to recognize many idioms in the IntelliJ Platform that can be simplified with Kotlin. To learn more about building IntelliJ Platform plugins with Kotlin, this tutorial will help you get started.

Adding Kotlin Support

IntelliJ IDEA bundles the necessary Kotlin plugin, requiring no further configuration. For detailed instructions, please refer to the Kotlin documentation.

Kotlin Standard Library

Since Kotlin 1.4, a dependency on the standard library stdlib is added automatically (API Docs). In most cases, it is not needed to include it in the plugin distribution as the platform already bundles it.

To opt out, add this line in gradle.properties:

kotlin.stdlib.default.dependency = false

If a plugin supports multiple platform versions, it must either target the lowest bundled stdlib version or provide the specific version in plugin distribution.

IntelliJ Platform versionBundled stdlib version
2021.21.5.10
2021.11.4.32
2020.31.4.0
2020.21.3.70
2020.11.3.70
2019.31.3.31
2019.21.3.3
2019.11.3.11

See Dependency on the standard library for more details.

Kotlin Gradle Plugin

Plugins using the Gradle Build System use the Kotlin JVM Gradle plugin.

Your build.gradle file may look like so:

plugins { id "java" id "org.jetbrains.kotlin.jvm" version "1.5.31" id "org.jetbrains.intellij" version "1.1.4" } apply plugin: "kotlin" apply plugin: "org.jetbrains.intellij" group "com.example" version "0.0.1" sourceCompatibility = 1.8 targetCompatibility = 1.8 compileKotlin { kotlinOptions.jvmTarget = "1.8" } compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } repositories { mavenCentral() } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" } intellij { version = "2020.2.4" pluginName = "Example" updateSinceUntilBuild = false }

Use Kotlin to Write Gradle Script

Gradle also supports using Kotlin in build scripts: build.gradle.kts.

There are many good resources for learning how to write build scripts for an IntelliJ Platform plugin with Kotlin script, like intellij-rust, julia-intellij, covscript-intellij or zig-intellij.

Additionally, explore IntelliJ Platform Explorer's list of open-source plugins using Gradle KTS.

build.gradle.kts basically looks like:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id("java") id("org.jetbrains.kotlin.jvm") version "1.5.31" id("org.jetbrains.intellij") version "1.1.4" } group = "com.your.company.name" version = "0.1-SNAPSHOT" tasks.withType<JavaCompile> { sourceCompatibility = "1.8" targetCompatibility = "1.8" } listOf("compileKotlin", "compileTestKotlin").forEach { tasks.getByName<KotlinCompile>(it) { kotlinOptions.jvmTarget = "1.8" } } repositories { mavenCentral() } dependencies { implementation(kotlin("stdlib-jdk8")) } intellij { version = "2020.2.4" pluginName = 'Example' updateSinceUntilBuild = false }

UI in Kotlin

The best way to create user interfaces with Kotlin is to use a type safe DSL for building forms. Using a GUI designer with Kotlin is currently not supported.

Handling Kotlin Code

If a plugin processes Kotlin code (e.g., providing inspections), it needs to add a dependency on the Kotlin plugin (Plugin ID org.jetbrains.kotlin) itself. Please refer to Plugin Dependencies for more information.

Depending on exact functionality, a plugin can also target UAST (Unified Abstract Syntax Tree) to support multiple JVM languages, including Java and Kotlin.

Kotlin Code FAQ

How to shorten references

Caution

Plugins must use Kotlin classes to implement declarations in the plugin configuration file. When registering an extension, the platform uses a dependency injection framework to instantiate these classes. For this reason, plugins must not use Kotlin objects to implement any plugin.xml declarations.

Examples

There are many open-source Kotlin plugins built on the IntelliJ Platform. For a readily available source of up to date examples and applications of the Kotlin language for building developer tools with the IntelliJ Platform, developers may look to these projects for inspiration:

Last modified: 20 September 2021