Kotlin - replace var with val Follow
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?
Please sign in to leave a comment.
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.
I have created another project from the scratch and it happens again. This is my build.gradle:
This is what auto-complete suggests
And this is what `KtPsiFactory` requires:
My SDK settings window:
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:
But then `KtPsiFactory` is not found
Did you perform full reimport of Gradle project? Please make sure your project synchronizes properly or try to recreate it from scratch.
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
Did you take my build.gradle as-is? Please do so.
I did
Sorry, it must be "Kotlin" for plugins, not "kotlin" (it worked on my case-insensitive filesystem).
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`
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
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