How to integrate KotlinPlugin's PSI classes
I have added kotlin plugin jar to my SDK class list, and have added the correct <depends> tag to my plugin.xml. The IDE resolves the class reference and compiles.
But when I call:
if (psiElement is KtPackageDirective)
or
if (psiElement is KtFile)
The statement is never true, even if the psiElement matches the class. AFAIK there is only 1 kotlin plugin.jar on the classpath.
And a cast results in:
java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtFile cannot be cast to org.jetbrains.kotlin.psi.KtFile
So obviously there are 2 versions of the class defined.
Does anyone have any suggestions on how to resolve this issue?
Please sign in to leave a comment.
Different classloaders. So how to correct...
if (item::class.java.name == KtClass::class.java.name) {
try {
item as KtClass
} catch (e: Exception) {
kotlin.io.println(item::class.java.classLoader.toString())
kotlin.io.println(KtClass::class.java.classLoader.toString())
}
}
PluginClassLoader[org.jetbrains.kotlin, 1.2.0-rc-39-IJ2017.3-1] com.intellij.ide.plugins.cl.PluginClassLoader@6c227290
PluginClassLoader[tv.twelvetone.intellij.plugin.visualfile, 1.0] com.intellij.ide.plugins.cl.PluginClassLoader@702bf1e6
It turns out I needed to add kotlin-compiler.jar instead of kotlin-plugin.jar to the SDK classes. But now the IDE can't navigate to the source code for classes such as KDoc.
Did you manage to resolve the issue with setting up a project to use Kotlin Psi? Thanks.
I get the same problem, but I use a gradle project. Any idea of how to fix the problem? Can it be fixed in the gradle setup? This is my build.gradle so far:
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.11'
id 'org.jetbrains.kotlin.jvm' version '1.3.41'
}
group 'se.winassist'
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.41"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.2.3'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Edit: realised we used different type of projects
In gradle, instead of;