Skip to content

Writing an MPS Plugin with Kotlin

When you use Kotlin to write an MPS plugin, the guidelines for the IntelliJ Platform SDK ⧉ apply.

Folder Structure

The folder structure should look like this:

├── build.gradle.kts
└── src
    └── main
        ├── kotlin
        └── resources
            └── META-INF
                ├── plugin.xml
                └── pluginIcon.svg

build.gradle.kts

The file build.gradle.kts should look something like this:

plugins {
    id("org.jetbrains.intellij")
    id("org.jetbrains.kotlin.jvm")
}

repositories {
    mavenCentral()
    maven { url = uri("https://projects.itemis.de/nexus/content/repositories/mbeddr") }
}

val intellijVersion = "2021.1"
val mpsVersion = "2021.1.4"
val targetJvm = "11" // the target Java version

version = "$intellijVersion.4"

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.0")
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.4.0")

    compileOnly("com.jetbrains:mps-workbench:$mpsVersion")
    compileOnly("com.jetbrains:mps-core:$mpsVersion")
    compileOnly("com.jetbrains:mps-platform:$mpsVersion")
    compileOnly("com.jetbrains:mps-openapi:$mpsVersion")
    compileOnly("com.jetbrains:mps-editor:$mpsVersion")
    compileOnly("com.jetbrains:mps-editor-api:$mpsVersion")
}

intellij {
    version.set(intellijVersion)
}

java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(11))
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
    kotlinOptions.jvmTarget = targetJvm
    kotlinOptions.apiVersion = "1.4"
}

tasks.getByName("buildSearchableOptions").enabled = false

The Gradle plugin org.jetbrains.intellij is needed for the IntelliJ plugin development, org.jetbrains.kotlin.jvm is needed for Kotlin support. We also add the itemis repository for the MPS dependencies. The dependency section contains dependencies for Kotlin and MPS. The available MPS dependencies are in the group JetBrains on mvnrepository.com. The rest of the code is boilerplate code for setting the correct versions.

plugin.xml

The file plugin.xml ⧉ is the plugin configuration file and usually is automatically generated by the MPS build language. In this file, you have to register, for example, tool windows, settings, and actions. Look at existing plugin.xml files ⧉ to figure out how they work.

Don't forget to add dependencies to the MPS plugins in this file:

1
2
3
4
5
6
7
<idea-plugin>
...
<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.mps</depends>
<depends>jetbrains.mps.core</depends>
...
</idea-plugin>

For everything else, look at the extension point and listener list ⧉.

Building the Plugin

When ready, you can build the plugin by executing ./gradlew buildPlugin. Gradle IntelliJ plugin ⧉ explains all the other tasks. Now there should be a folder build/distributions which contains the zipped plugin that you can install through the MPS plugin manager.

Running the Plugin

Open the file build.gradle.kts in the ide-plugin folder and change the intellij block to use a local path, and turn off the instrumentation of the code, e.g.:

1
2
3
4
intellij {
    localPath.set(PATH_TO_MPS_FOLDER)
    instrumentCode.set(false)
}

Then you must ensure that you set the system variable idea.platform.prefix to 'Idea'. Workaround: Create a file with extension .sh or .bat in the bin folder of the MPS installation with the following content: -Didea.platform.prefix=Idea.

You can now open MPS with the plugin installed by calling ./gradlew runIde.

Comments