Jetbrains - now that K2 mode is in beta as of Inte...
# intellij
c
Jetbrains - now that K2 mode is in beta as of IntelliJ 2024.2 - are there updates on this:
We’re actively working on assisting plugin authors to quickly make their plugins compatible with K2 mode.
Its challenging for users to beta test K2 mode when required plugins are incompatible and there’s not a clear path to compatibility. This limits the effectiveness of K2 beta if it isn’t (always) practical for users to enable it. Example: https://github.com/kotest/kotest-intellij-plugin/issues/289
m
Hey! We’ve published a guide on migration from K1 to K2: https://kotlin.github.io/analysis-api/migrating-from-k1.html The guide has a section on declaring that a plugin supports K2: https://kotlin.github.io/analysis-api/migrating-from-k1.html#declaring-compatibility-with-the-k2-kotlin-mode The setting will only be available starting from IntelliJ 2024.2.1 (Preview), which hasn’t been released yet AFAIK, but will be soon. By the way, the website I linked is a general guide for the Analysis API, which is the new analysis engine for K2 (and also has a K1 mode for backwards compatibility). Beyond the K1 to K2 migration guide, there’s a lot of information in the other sections of the guide which should help with migrating a plugin to the Analysis API.
c
Awesome. Thank you for pulling all that together.
m
You’re welcome!
e
Is the K2 analysis API available starting from 242 as well, or can be it be used by earlier versions? Can K2 APIs be used in K1 mode? Or can we somehow tell what mode the user is running and use that to invoke the correct APIs?
m
@Emil Kantis The Analysis API is bundled in earlier versions, as we’re using it to develop the Kotlin K2 plugin. But we’re not making any stability guarantees for those versions and we recently revamped a big part of the API surface, so there may be significant differences between 242 and earlier versions. As for K1, the Analysis API has a backend for K1, so a lot of the functionality should work in K1 mode. But the support isn’t as extensive/mature as for K2. Our documentation notes the following:
The Analysis API mainly targets the K2 Kotlin compiler, but there is also limited support for the legacy 1.0 compiler. So the same piece of logic, if implemented on top of the Analysis API, can work both in the K1 and K2 Kotlin modes.
If you run into any problems, please let us know!
n
Hello need help in pointing out the potential root cause. I'm trying to make a proof of concept with standalone Analysis API, one thing I hit when analyze KtDeclaration. Failure message:
Copy code
Exception: org.jetbrains.kotlin.utils.exceptions.KotlinIllegalArgumentExceptionWithAttachments: Unexpected FirElement FirRegularClassImpl
I hit a failure where the code looks like:
Copy code
object A {
   private data class Q { ... } 
   private fun someMethod(number: Int): Q? { ... } // analyze(KtDeclaration) { getReturnKtType() } will throw exception
}
My naive analysis code, I capture the declarations from PsiFile and try to filter return type with String.
Copy code
fun KtDeclaration.hasStringType(): Boolean {
  analyze(this) {
      return getReturnKtType() == builtinTypes.STRING
  }
}
Wonder is it necessary I should register kotlin-compiler-fir-for-ide library when setup this standalone session?