Search code examples
kotlinandroid-studiointellij-pluginintellij-platform-psi

How can I modify Kotlin code from a self-developed Intellij plugin?


I plan to create a plugin for Intellij (Android Studio to be more specific) that amongst other things shall modify existing code, e.g. I want to add a new case to a when statement in a given function of an already existing Kotlin file:

fun test() {
    val num = 1

    when(num) {
        1 -> NumberOne()
        2 -> NumberTwo()
        // want to add 3 -> NumberThree() here
    }
}

I already did some research and stumbled across the Intellij PSI. However the linked docs only mention Java, it's unclear to me whether I could modify Kotlin code with the PSI. Can someone with more experience in this topic share his knowledge on this? By the way I only need to modify Kotlin code, no need to be able to modify Java and Kotlin code.


Solution

  • it's unclear to me whether I could modify Kotlin code with the PSI

    Yes you can achieve this.

    I cannot give you the code for this, but if you examine the source code for the Kotlin function arguments helper Plugin, it achieves similar things:

    https://github.com/DeveloperUtils/kotlin-function-arguments-helper