[英]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: 更新:
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.