【问题标题】:How to set up Kotlin PSI for IntelliJ IDEA Gradle Plugin?如何为 IntelliJ IDEA Gradle 插件设置 Kotlin PSI?
【发布时间】:2020-02-27 05:09:49
【问题描述】:

我正在为 IntelliJ IDE 构建一个插件来操作项目中的 Kotlin 文件。我已经能够编写一堆测试来获取 Kotlin 文件并根据其内容生成一个新文件。当我在 IDE 中运行此插件时,我无法将文件检测为 Kotlin 文件。查看调试器时,我的文件说它是来自org.jetbrains.kotlin.psi 库的 KtFile。但是,如果我尝试将其转换为 KtFile,则会出现错误:

java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtFile cannot be cast to org.jetbrains.kotlin.psi.KtFile

显然库版本在运行时和编译时之间是关闭的。我需要做些什么来配置我的插件以在插件运行时使用正确的 Kotlin PSI?

我的plugin.xml 看起来像这样:

<idea-plugin>
    <id>...</id>
    <name>...</name>
    <vendor email="..." url="...">...</vendor>

    <description><...</description>

    <depends>com.intellij.modules.all</depends>
    <depends>org.jetbrains.kotlin</depends>

    <actions>...</actions>
</idea-plugin>

我的build.gradle.kts 看起来像:

plugins {
    id("org.jetbrains.intellij") version "0.4.16"
    kotlin("jvm") version "1.3.61"
}

group = "..."
version = "..."

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    implementation(kotlin("compiler-embeddable", "1.3.61"))
    implementation(kotlin("gradle-plugin", "1.3.61"))

    testImplementation(group = "junit", name = "junit", version = "4.12")
}

buildscript {
    repositories { mavenCentral() }
    dependencies {
        classpath(kotlin("compiler-embeddable", "1.3.61"))
        classpath(kotlin("gradle-plugin", "1.3.61"))
    }
}

intellij {
    version = "2019.1.4"
    setPlugins("Kotlin")
}
tasks {
    compileKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
    compileTestKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
}
tasks.getByName<org.jetbrains.intellij.tasks.PatchPluginXmlTask>("patchPluginXml") {
    changeNotes("...")
}

我已经知道How to include Kotlin PSI classes (e.g. KtClass) in Intellij IDEA Gradle plugin project written in Kotlin?How to add Kotlin PSI source files to IDEA Plugin project configuration 这基本上是我想要回答的,但还没有任何东西可以解决我的问题。也许有一些关于这个问题的文档,但它逃避了我的搜索。

【问题讨论】:

    标签: kotlin intellij-idea intellij-plugin gradle-kotlin-dsl


    【解决方案1】:

    您的dependencies 应包括implementation(kotlin("reflect"))plugin.xml 应包括&lt;depends&gt;org.jetbrains.kotlin&lt;/depends&gt;

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多