Home > Enterprise >  How can I modify PSI of Kotlin?
How can I modify PSI of Kotlin?

Time:12-02

I'm trying to create a plugin for IntelliJ IDEA that functions similarly to lombok.

This is what I'm trying to do, with an existing interface:

interface TestInterface {
    var testProperty: Int
}

I want to modify the PSI so that the IntelliJ IDEA can recognize it as:

interface TestInterface {
    var testProperty: Int

    companion object: TestInterface {
        override var testProperty: Int
            get() {
                TODO("Do something")
            }
            set(value) {
                TODO("Do something")
            }
    }
}

With kapt, I can achieve the functions I want, and here is my code: Github, but it dosen't work.

Could you help me to solve this problem? Thank you!

CodePudding user response:

After communicating with the IDEA team, I got the following information:

Hi! Unfortunately, generating the Kotlin PSI on the fly for use by the Kotlin resolve is impossible. Java resolution in the IDE uses PSI to resolve things, and that's why it works for Java. Kotlin resolution in the IDE uses the Kotlin compiler, so creating PSI on the fly and using PsiAugmentProvider will not work.

To sum up, if you want to achieve a function similar to Lombok plugin, you cannot use the method of editing PSI. Then I found a feasible method based on the above information, that is KSP: https://kotlinlang.org/docs/ksp-overview.html

  • Related