简体   繁体   English

如何修改 Kotlin 的 PSI?

[英]How can I modify PSI of Kotlin?

I'm trying to create a plugin for IntelliJ IDEA that functions similarly to lombok.我正在尝试为 IntelliJ IDEA 创建一个功能类似于 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:我想修改 PSI 以便 IntelliJ IDEA 可以将其识别为:

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.使用kapt,我可以实现我想要的功能,这是我的代码: Github ,但它不起作用。

Could you help me to solve this problem?你能帮我解决这个问题吗? Thank you!谢谢!

After communicating with the IDEA team, I got the following information:与IDEA团队沟通后,得到如下信息:

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.即时生成 Kotlin PSI 供 Kotlin 解析使用是不可能的,IDE 中的 Java 解析使用 PSI 来解析事物。 and that's why it works for Java, Kotlin resolution in the IDE uses the Kotlin compiler.这就是为什么它适用于 Java,IDE 中的 Kotlin 分辨率使用 Kotlin 编译器。 so creating PSI on the fly and using PsiAugmentProvider will not work.因此即时创建 PSI 和使用 PsiAugmentProvider 将不起作用。

To sum up, if you want to achieve a function similar to Lombok plugin, you cannot use the method of editing PSI.综上所述,如果想实现一个类似Lombok插件的function,不能使用编辑PSI的方法。 Then I found a feasible method based on the above information, that is KSP: https://kotlinlang.org/docs/ksp-overview.html然后根据以上信息找到了一个可行的方法,就是KSP: https://kotlinlang.org/docs/ksp-overview.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Gradle插件上使用PSI / UAST - How can I use PSI/UAST on my Gradle plugin 如何在Kotlin施工期间修改val成员 - How can I modify val members during construction in Kotlin 如何为 IntelliJ IDEA Gradle 插件设置 Kotlin PSI? - How to set up Kotlin PSI for IntelliJ IDEA Gradle Plugin? 如何使用 kotlin PSI 获取 ktExpression 的类型信息? - How to get the type information of the ktExpression using kotlin PSI? 如何在 Kotlin PSI 中获得 class 的完全限定域名? - How do a get a fully qualified domain name of class in Kotlin PSI? 如何在 Kotlin 中修改地图内的数组 - How to I modify arrays inside of a map in Kotlin 如何在用 Kotlin 编写的 Intellij IDEA Gradle 插件项目中包含 Kotlin PSI 类(例如 KtClass)? - How to include Kotlin PSI classes (e.g. KtClass) in Intellij IDEA Gradle plugin project written in Kotlin? 如何修改 TypeConverter 以便我可以使用 kotlin 解析 Room 中的复杂数据 - How to modify the TypeConverter so I can parse complex data in Room with kotlin 从哪里获得Kotlin Psi依赖库? - Where to get Kotlin Psi dependent library? 如何修改 Kotlin Flow distinctUntilChanged 添加过期时间 - How do I modify Kotlin Flow distinctUntilChanged to add an expiry time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM