0

I'm building a Plugin for the IntelliJ IDE to manipulate Kotlin files in a project. I've been able to write a bunch of tests to take a Kotlin file and generate a new file based on its contents. When I run this plugin in the IDE I'm unable to detect files as Kotlin files. When looking at the debugger my file says that it is a KtFile from the org.jetbrains.kotlin.psi library. But if I try to cast it to a KtFile I get an error:

java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtFile cannot be cast to org.jetbrains.kotlin.psi.KtFile

So apparently the library version is off between runtime and compile time. What do I have to do to configure my plugin to use the correct Kotlin PSI at plugin runtime?

My plugin.xml looks like this:

<idea-plugin>
    <id>...</id>
    <name>...</name>
    <vendor email="..." url="...">...</vendor>

    <description><...</description>

    <depends>com.intellij.modules.all</depends>
    <depends>org.jetbrains.kotlin</depends>

    <actions>...</actions>
</idea-plugin>

My build.gradle.kts looks like:

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

group = "..."
version = "..."

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    implementation(kotlin("compiler-embeddable", "1.3.61"))
    implementation(kotlin("gradle-plugin", "1.3.61"))

    testImplementation(group = "junit", name = "junit", version = "4.12")
}

buildscript {
    repositories { mavenCentral() }
    dependencies {
        classpath(kotlin("compiler-embeddable", "1.3.61"))
        classpath(kotlin("gradle-plugin", "1.3.61"))
    }
}

intellij {
    version = "2019.1.4"
    setPlugins("Kotlin")
}
tasks {
    compileKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
    compileTestKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
}
tasks.getByName<org.jetbrains.intellij.tasks.PatchPluginXmlTask>("patchPluginXml") {
    changeNotes("...")
}

I already am aware of How to include Kotlin PSI classes (e.g. KtClass) in Intellij IDEA Gradle plugin project written in Kotlin? and How to add Kotlin PSI source files to IDEA Plugin project configuration which is essentially what I want to be answered but haven't gotten anything to fix my issue. Maybe there is some documentation on this issue but it evades my searches.

Sebastian Helzer
  • 839
  • 7
  • 12

1 Answers1

0

Your dependencies should include implementation(kotlin("reflect")) and plugin.xml should include <depends>org.jetbrains.kotlin</depends>

Martin
  • 770
  • 6
  • 22