gpt4 book ai didi

gradle - 如何在用 Kotlin 编写的 Intellij IDEA Gradle 插件项目中包含 Kotlin PSI 类(例如 KtClass)?

转载 作者:行者123 更新时间:2023-12-02 12:52:41 29 4
gpt4 key购买 nike

我正在尝试编写一个插件来向 Kotlin 项目添加模拟数据。第一部分涉及在当前项目中查找从特定基类继承的所有 Kotlin 类。我希望能够解析这些类以读取注释的值并获取构造函数的结构。然后,此信息将用于向项目添加代码,将所选类的实例添加到模拟数据库实例。

我一直在使用 PsiViewer 插件来检查 Kotlin 类文件中的 PSI 树。为了访问 KtFile、Kt​​Class 等,我在 build-gradle 文件中添加了对“org.jetbrains.kotlin:kotlin-compiler-embeddable”的依赖。在我尝试运行插件之前,这似乎没问题。使用时我从来没有得到匹配,例如psiFile is KtFile .我仍然设法得到正确的 PsiFile通过简单解析 text 的实例字段,但是当我尝试转换 PsiFile 时实例我得到以下异常:

java.lang.ClassCastException: class org.jetbrains.kotlin.psi.KtFile cannot be cast to class org.jetbrains.kotlin.psi.KtFile (org.jetbrains.kotlin.psi.KtFile is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @2ed18f3d; org.jetbrains.kotlin.psi.KtFile is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @4308c14c)

我目前正在使用 IntelliJ IDEA 2019.2.3。尝试使用不同版本的 kotlin 插件(1.3.31 和 1.3.41)。在 Windows 10 上运行。

build.gradle
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.11'
id 'org.jetbrains.kotlin.jvm' version '1.3.31'
}

group 'se.winassist'
version '1.0'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.31"
testCompile group: 'junit', name: 'junit', version: '4.12'
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.2.3'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}

CreateMockdataFromPostgreSQLClass.kt:
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.ui.Messages
import org.jetbrains.kotlin.psi.KtFile

class CreateMockdataFromPostgreSQLClass: AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val project = e.getRequiredData(CommonDataKeys.PROJECT)
val kotlinFileHandler = KotlinFileHandler(project)
val dbClasses = kotlinFileHandler.getAllKotlinDbClasses()
val classNames = dbClasses.map {
val ktFile = it as KtFile
ktFile.name
}.joinToString(separator = "\n")
Messages.showInfoMessage(classNames, "Test")
}
}

KotlinFileHandler.kt :
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager

data class KotlinFileHandler(val aProject: Project) {

fun getAllKotlinDbClasses(): List<PsiFile> {
val roots = ProjectRootManager.getInstance(aProject).contentSourceRoots
val dirs = roots.map { PsiManager.getInstance(aProject).findDirectory(it) }
val result = mutableListOf<PsiFile>()
getKotlinDbClassesInPsiDir(result, dirs)
return result.toList()
}

private tailrec fun getKotlinDbClassesInPsiDir(aResult: MutableList<PsiFile>, aPsiDirectoryList: List<PsiDirectory?>) {
if (aPsiDirectoryList.isEmpty()) return

val subDirectories = mutableListOf<PsiDirectory>()
aPsiDirectoryList.filter { it != null }.forEach {
val children = it!!.children
val files = children.filter {
it is PsiFile && it.language.toString().toLowerCase() == "language: kotlin"
}.map { it as PsiFile }
subDirectories += children.filter { it is PsiDirectory }.map { it as PsiDirectory }
aResult += files.filter {
hasBasePersistenceSuperclass(it) && hasTableNameAnnotation(it)
}.toList()
}
getKotlinDbClassesInPsiDir(aResult, subDirectories)
}
}

我假设我错误地设置了 gradle。我已经广泛寻找合适的设置,但未能找到可行的解决方案。任何人都可以指导我进行正确的 gradle 设置吗?

最佳答案

您是否通过向 IntelliJ 任务添加插件来检查

intellij {
version '2020.1'
plugins = ['java','Kotlin']
intellij.type = 'IC'
}
请检查并更新

关于gradle - 如何在用 Kotlin 编写的 Intellij IDEA Gradle 插件项目中包含 Kotlin PSI 类(例如 KtClass)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58803103/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com