CodeVisionProviderBase KtNamedFunction is KtNamedFunction = false

Answered

I am developing a plugin similar to https://plugins.jetbrains.com/plugin/12159-codemetrics but with kotlin support.
I am doing this using the newer CodeVision api and computing cognitive complexity using io.gitlab.arturbosch.detekt:detekt-metrics lib which uses kotlin-compiler-embeddable
I am running into two issues depending on my build.

1. With this build:

plugins {
  id("java")
  id("org.jetbrains.kotlin.jvm") version "1.9.10"
  id("org.jetbrains.intellij") version "1.16.0"
}

group = "com.blabla.idea.plugin"
version = "1.0-SNAPSHOT"

repositories {
  mavenCentral()
}

// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
intellij {
  version.set("2023.2")
  type.set("IC") // Target IDE Platform

  plugins.set(listOf("Kotlin"))
}

dependencies {
  testImplementation("org.junit.jupiter:junit-jupiter-engine:5.10.0")
  testImplementation("io.mockk:mockk:1.13.8")
  testImplementation("io.kotest:kotest-assertions-jvm:4.0.7")
  implementation("io.gitlab.arturbosch.detekt:detekt-metrics:1.23.1") {
    //exclude("org.jetbrains.kotlin", "kotlin-compiler-embeddable")
  }

}

tasks {
  // Set the JVM compatibility versions
  withType<JavaCompile> {
    sourceCompatibility = "17"
    targetCompatibility = "17"
  }
  withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions.jvmTarget = "17"
  }

  patchPluginXml {
    sinceBuild.set("222")
    untilBuild.set("232.*")
  }

  signPlugin {
    certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
    privateKey.set(System.getenv("PRIVATE_KEY"))
    password.set(System.getenv("PRIVATE_KEY_PASSWORD"))
  }

  publishPlugin {
    token.set(System.getenv("PUBLISH_TOKEN"))
  }
}

This code hint:

class CodeVisionHintBis: CodeVisionProviderBase() {
  override val id = "cyclomatik_bis"
  override val name = "cyclomatik_bis"
  override val relativeOrderings: List<CodeVisionRelativeOrdering> = emptyList()

  override fun acceptsElement(element: PsiElement): Boolean = element is KtNamedFunction

  override fun acceptsFile(file: PsiFile): Boolean = file.fileType is KotlinFileType

  override fun getHint(element: PsiElement, file: PsiFile): String? = CognitiveComplexity.calculate(element as KtNamedFunction).toString()

  override fun handleClick(editor: Editor, element: PsiElement, event: MouseEvent?) = Unit
}

And this plugin.xml:
 

<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
    <!-- Unique identifier of the plugin. It should be FQN. It cannot be changed between the plugin versions. -->
    <id>com.blabla.idea.plugin.cyclomatik</id>

    <!-- Public plugin name should be written in Title Case.
         Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-name -->
    <name>Cyclomatik</name>

    <!-- A displayed Vendor name or Organization ID displayed on the Plugins Page. -->
    <vendor email="blabla@blabla.com" url="https://www.blabla.fr">Blabla</vendor>

    <!-- Description of the plugin displayed on the Plugin Page and IDE Plugin Manager.
         Simple HTML elements (text formatting, paragraphs, and lists) can be added inside of <![CDATA[ ]]> tag.
         Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-description -->
    <description><![CDATA[
    This plugin is used to display a short cyclomatic tip on your code.<br>
  ]]></description>

    <!-- Product and plugin compatibility requirements.
         Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
    <depends>com.intellij.modules.platform</depends>
    <depends>org.jetbrains.kotlin</depends>

    <!-- Extension points defined by the plugin.
         Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
    <extensions defaultExtensionNs="com.intellij">
        <!--<codeInsight.daemonBoundCodeVisionProvider implementation="com.blabla.idea.plugin.cyclomatik.CodeVisionHint"/>-->
        <codeInsight.daemonBoundCodeVisionProvider implementation="com.blabla.idea.plugin.cyclomatik.CodeVisionHintBis"/>
    </extensions>
</idea-plugin>

The acceptsFile and acceptsElement both return false when the element look like they are the required type:

 
2. When I exclude("org.jetbrains.kotlin", "kotlin-compiler-embeddable") , I get a class not found exception:
Caused by: java.lang.ClassNotFoundException: org.jetbrains.kotlin.com.intellij.openapi.util.Key PluginClassLoader(plugin=PluginDescriptor(name=Cyclomatik, id=com.blabla.idea.plugin.cyclomatik, descriptorPath=plugin.xml, path=~/IdeaProjects/blabla/innovation/cyclomatik/build/idea-sandbox/plugins/cyclomatik, version=1.0-SNAPSHOT, package=null, isBundled=false), packagePrefix=null, state=active)


What am I missing ?

Thank you 

0
1 comment

The dependency on bundled Kotlin plugin is using the wrong plugin ID “Kotlin”.

See https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html#ids-of-bundled-plugins

 

If you're still stuck, please provide a link to your repository to reproduce.

0

Please sign in to leave a comment.