简体   繁体   English

使Intellij Idea插件与Kotlin文件一起使用

[英]Make Intellij Idea plugin work with Kotlin files

I've created this very simple Intellij Idea plugin which folds some reference expressions. 我创建了这个非常简单的Intellij Idea插件 ,它可以折叠一些参考表达式。 It works great for Java files but it doesn't work for Kotlin. 它适用于Java文件,但不适用于Kotlin。

Here is the source: https://github.com/nodes-android/nstack-translation-folding . 这是源代码: https : //github.com/nodes-android/nstack-translation-folding
I will include here the important parts: 我将在这里包括重要部分:
plugin.xml plugin.xml中

</idea-plugin>
    <depends>com.intellij.modules.all</depends>

    <application-components>
        <component>
            <implementation-class>com.nodes.folding.TranslationFoldingBuilder</implementation-class>
        </component>
    </application-components>

    <extensions defaultExtensionNs="com.intellij">
        <lang.foldingBuilder language="JAVA" implementationClass="com.nodes.folding.TranslationFoldingBuilder"/>
    </extensions>

</idea-plugin>

TranslationFoldingBuilder.kt TranslationFoldingBuilder.kt

class TranslationFoldingBuilder : FoldingBuilderEx() {

    override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
        if (root !is PsiJavaFile) {
            return FoldingDescriptor.EMPTY
        }

        val descriptors = ArrayList<FoldingDescriptor>()
        // Get all the reference expressions in this Java file
        val referenceExpressions = PsiTreeUtil.findChildrenOfType(root, PsiReferenceExpression::class.java)

        // Some logic

        return descriptors.toTypedArray()
    }
}

My problem is that for Kotlin files the buildFoldRegions() is not called at all. 我的问题是,对于Kotlin文件, 根本不调用buildFoldRegions()

Of course it will not work for kotlin ,as your 当然,这对于Kotlin不起作用,因为您

if (root !is PsiJavaFile) { return FoldingDescriptor.EMPTY } 如果(根!是PsiJavaFile){返回FoldingDescriptor.EMPTY}

For kotlin files, the file is org.jetbrains.kotlin.psi.KtFile instance, rather than PsiJavaFile 对于kotlin文件,该文件是org.jetbrains.kotlin.psi.KtFile实例,而不是PsiJavaFile


Update: 更新:

  1. You need to add kotlin plugin as the dependency of your plugin in plugin.xml 您需要在plugin.xml中添加kotlin插件作为插件的依赖项
  2. The psi api of Kotlin is not same as Java (They are different languages). Kotlin的psi api与Java不同(它们是不同的语言)。 You need to write a different class (but some can be same, I just copy your origin code and edit so there is some duplicate in my kt implementation. 您需要编写一个不同的类(但有些可以相同,我只是复制您的原始代码并进行编辑,因此我的kt实现中有一些重复项。

You can see my commit here https://github.com/aristotll/nstack-translation-folding/commit/45286e6ec10d3b50defe25d55f8fbd8f122a148b . 您可以在这里https://github.com/aristotll/nstack-translation-folding/commit/45286e6ec10d3b50defe25d55f8fbd8f122a148b看到我的提交。

就我而言,向项目添加kotlin-compiler库解决了缺少KtFile和其他与Kotlin Psi相关的类的问题。

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

相关问题 尝试更新到 Kotlin 1.4.0 时出现奇怪的错误。 如何使其与 Gradle 和 IntelliJ IDEA 2020.2.1 一起使用? - Getting weird bugs when trying to update to Kotlin 1.4.0. How to make it work with Gradle and IntelliJ IDEA 2020.2.1? 如何使 Intellij Idea 的 Intellisense 在 Flutter 项目上的 C++ 和 Kotlin 上工作? - How to make Intellij Idea's Intellisense work on C++ and Kotlin on a Flutter project? 带有“ Go”插件的IntelliJ IDEA中的调试器不起作用 - Debugger is not work in IntelliJ IDEA with “Go” plugin 用于折叠 .conf 文件的 IntelliJ IDEA 插件? - IntelliJ IDEA plugin to fold .conf files? elm 从 IntelliJ IDEA 插件制作 - elm make from IntelliJ IDEA plugin Intellij IDEA插件开发。 行动“创建科特琳课” - Intellij IDEA plugin development. Action “create kotlin class” Intellij Idea Kotlin 插件看不到 scala 案例 class 访问器 - Intellij Idea Kotlin plugin cannot see scala case class accessors Intellij IDEA (Android Studio) Kotlin 插件的目的是什么? - What is the purpose of Intellij IDEA (Android Studio) Kotlin plugin? 如何为 IntelliJ IDEA Gradle 插件设置 Kotlin PSI? - How to set up Kotlin PSI for IntelliJ IDEA Gradle Plugin? 开发IntelliJ Idea插件时如何创建kotlin类? - how to create kotlin class when developing intellij idea plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM