Make Intellij Idea plugin work with Kotlin files

   

I've created this very simple Intellij Idea plugin which folds some reference expressions. It works great for Java files but it doesn't work for Kotlin.

Here is the source: https://github.com/nodes-android/nstack-translation-folding
I will include here the important parts: 
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

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.

 

You can also see the question here https://stackoverflow.com/questions/47439843/make-intellij-idea-plugin-work-with-kotlin-files

0
1 comment
Official comment

First, you need to add a dependency on the Kotlin plugin to your plugin.xml:

<depends>org.jetbrains.kotlin</depends>You also need to add the Kotlin plugin classes (plugins/kotlin/lib/kotlin-plugin.jar) to the classpath of your IntelliJ Platform Plugin SDK.

Second, you need to register your folding builder (most likely this will be a separate one) for Kotlin. Use language="Kotlin" in the extension registration.

Finally, you need to use the Kotlin PSI classes to implement your logic (KtFile instead of PsiJavaFile, KtSimpleNameExpression instead of PsiReferenceExpression, etc.)

Please sign in to leave a comment.