Kotlin - replace var with val

Answered

I just started my journey with plugins development. I want to create simple plugin to toggle VAR and VAL keyword. I have created simple Action, but I don't know how to get Project object from `org.jetbrains.kotlin.com.intellij.openapi.project` package. This is what I've done:

 


import com.intellij.lang.ASTNode
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.LangDataKeys
import org.jetbrains.kotlin.psi.KtPsiFactory

class ToggleKeyword : AnAction() {

override fun actionPerformed(e: AnActionEvent) {
val project = e.project
val editor = e.getRequiredData(CommonDataKeys.EDITOR)

val caretModel = editor.caretModel

val file = e.getData(LangDataKeys.PSI_FILE)
val element = file?.findElementAt(caretModel.offset)
if (element is ASTNode) {
if (element.elementType.index == VAR_INDEX) {
val factory = KtPsiFactory(project, true) // Wrong type
element.replace(factory.createValKeyword()) // Wrong type
}
}
}

companion object {
const val VAR_INDEX: Short = 187
}
}

 

I know that I have two kinds of imports: `com.intellij.openapi` and `org.jetbrains.kotlin.psi`, but I can't get `org.jetbrains.kotlin.com.intellij.openapi.project.Project` from `AnActionEvent`

Any suggestions?

12 comments
Comment actions Permalink

There is no such class "org.jetbrains.kotlin.com.intellij.openapi.project.Project". KtPsiFactory does expect com.intellij.openapi.project.Project which matches your code. Eventually something is wrong with your project setup/SDK setup? please try re-creating IJ SDK and/or reimport Gradle project.

0
Comment actions Permalink

I have created another project from the scratch and it happens again. This is my build.gradle:

plugins {
id 'org.jetbrains.intellij' version '0.4.9'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
}

group 'lau'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
intellij {
version '2019.1.3'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}

 

This is what auto-complete suggests

 

And this is what `KtPsiFactory` requires:

 

My SDK settings window:

0
Comment actions Permalink

The problem is your build.gradle definition. You want to use the Kotlin plugin bundled with IntelliJ to use KtPsiFactory.

" implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31" is wrong then.

 

This build.gradle works for me:

plugins {
id 'org.jetbrains.intellij' version '0.4.9'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
}

group 'test'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.1.3'
plugins 'Kotlin'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
0
Comment actions Permalink

But then `KtPsiFactory` is not found

0
Comment actions Permalink

Did you perform full reimport of Gradle project? Please make sure your project synchronizes properly or try to recreate it from scratch.

0
Comment actions Permalink

I have noticed another difference:

In `intellij` node I didn't set "plugin 'kotlin'", but now there is a sync error:

Cannot find builtin plugin kotlin for IDE: /home/lau/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2019.1.3/6a2d76b455a8e07743bd5bdc891a410a7ddb2fb3/ideaIC-2019.1.3

So there must be something with kotlin config

0
Comment actions Permalink

Did you take my build.gradle as-is? Please do so.

0
Comment actions Permalink

Sorry, it must be "Kotlin" for plugins, not "kotlin" (it worked on my case-insensitive filesystem).

0
Comment actions Permalink

Yes. That was the case. I have tried everything for the past few hours, but I didn't thought about that. Now it compiles but throws `ClassNotFoundException` when I try to initialize `KtPsiFactory`. To be precise: `org.jetbrains.kotlin.psi.KtPsiFactoryKt PluginClassLoader[lau.example, 1.0-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@1a576670`

0
Comment actions Permalink

You must declare dependency on Kotlin plugin in your plugin.xml by adding

<depends>org.jetbrains.kotlin</depends>

 

See http://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_dependencies.html

0
Comment actions Permalink

That's right. Now it works like a charm... well it's not working yet, but that's not the subject of this thread. Initial problem is solved thanks to you

0

Please sign in to leave a comment.