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.