Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K2 plugin migration #321

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ val descriptors = listOf(
PluginDescriptor(
since = "242.*", // this version is 2024.2.x
until = "243.*",
sdkVersion = "2024.2",
sdkVersion = "2024.2.2",
sourceFolder = "IC-242",
useInstaller = true,
),
Expand Down Expand Up @@ -111,6 +111,12 @@ val runWithCustomSandbox by intellijPlatformTesting.runIde.registering {
}
}

val runWithK2Mode by intellijPlatformTesting.runIde.registering {
task {
jvmArgs = listOf("-Didea.kotlin.plugin.use.k2=true")
}
}

intellijPlatform {
buildSearchableOptions = false
projectName = project.name
Expand Down
36 changes: 36 additions & 0 deletions src/IC-223/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.kotest.plugin.intellij.psi

import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.typeUtil.supertypes

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries
.mapNotNull { it.typeReference }
.mapNotNull {
runCatching {
val bindingContext = it.analyze()
bindingContext.get(BindingContext.TYPE, it)
}.getOrNull()
}.flatMap {
runCatching {
it.supertypes() + it
}.getOrElse { emptyList() }
}.mapNotNull {
runCatching {
it.constructor.declarationDescriptor.classId
}.getOrNull()
}.mapNotNull {
runCatching {
val packageName = it.packageFqName
val simpleName = it.relativeClassName
FqName("$packageName.$simpleName")
}.getOrNull()
}.filterNot { it.toString() == "kotlin.Any" }
}
36 changes: 36 additions & 0 deletions src/IC-231/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.kotest.plugin.intellij.psi

import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.typeUtil.supertypes

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries
.mapNotNull { it.typeReference }
.mapNotNull {
runCatching {
val bindingContext = it.analyze()
bindingContext.get(BindingContext.TYPE, it)
}.getOrNull()
}.flatMap {
runCatching {
it.supertypes() + it
}.getOrElse { emptyList() }
}.mapNotNull {
runCatching {
it.constructor.declarationDescriptor.classId
}.getOrNull()
}.mapNotNull {
runCatching {
val packageName = it.packageFqName
val simpleName = it.relativeClassName
FqName("$packageName.$simpleName")
}.getOrNull()
}.filterNot { it.toString() == "kotlin.Any" }
}
36 changes: 36 additions & 0 deletions src/IC-232/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.kotest.plugin.intellij.psi

import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.typeUtil.supertypes

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries
.mapNotNull { it.typeReference }
.mapNotNull {
runCatching {
val bindingContext = it.analyze()
bindingContext.get(BindingContext.TYPE, it)
}.getOrNull()
}.flatMap {
runCatching {
it.supertypes() + it
}.getOrElse { emptyList() }
}.mapNotNull {
runCatching {
it.constructor.declarationDescriptor.classId
}.getOrNull()
}.mapNotNull {
runCatching {
val packageName = it.packageFqName
val simpleName = it.relativeClassName
FqName("$packageName.$simpleName")
}.getOrNull()
}.filterNot { it.toString() == "kotlin.Any" }
}
36 changes: 36 additions & 0 deletions src/IC-233/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.kotest.plugin.intellij.psi

import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.typeUtil.supertypes

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries
.mapNotNull { it.typeReference }
.mapNotNull {
runCatching {
val bindingContext = it.analyze()
bindingContext.get(BindingContext.TYPE, it)
}.getOrNull()
}.flatMap {
runCatching {
it.supertypes() + it
}.getOrElse { emptyList() }
}.mapNotNull {
runCatching {
it.constructor.declarationDescriptor.classId
}.getOrNull()
}.mapNotNull {
runCatching {
val packageName = it.packageFqName
val simpleName = it.relativeClassName
FqName("$packageName.$simpleName")
}.getOrNull()
}.filterNot { it.toString() == "kotlin.Any" }
}
36 changes: 36 additions & 0 deletions src/IC-241/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.kotest.plugin.intellij.psi

import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.typeUtil.supertypes

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries
.mapNotNull { it.typeReference }
.mapNotNull {
runCatching {
val bindingContext = it.analyze()
bindingContext.get(BindingContext.TYPE, it)
}.getOrNull()
}.flatMap {
runCatching {
it.supertypes() + it
}.getOrElse { emptyList() }
}.mapNotNull {
runCatching {
it.constructor.declarationDescriptor.classId
}.getOrNull()
}.mapNotNull {
runCatching {
val packageName = it.packageFqName
val simpleName = it.relativeClassName
FqName("$packageName.$simpleName")
}.getOrNull()
}.filterNot { it.toString() == "kotlin.Any" }
}
32 changes: 32 additions & 0 deletions src/IC-242/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.kotest.plugin.intellij.psi

import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.permissions.KaAllowAnalysisOnEdt
import org.jetbrains.kotlin.analysis.api.permissions.allowAnalysisOnEdt
import org.jetbrains.kotlin.analysis.api.types.symbol
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.psi.KtClassOrObject

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
@OptIn(KaAllowAnalysisOnEdt::class)
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries.mapNotNull { it.typeReference }
.flatMap { ref ->
// SurroundSelectionWithFunctionIntention.isAvailable is called in EDT before the intention is applied
// unfortunately API to avoid this was introduced in 23.2 only
// this we need to move intentions to the facade or accept EDT here until 23.2- are still supported
allowAnalysisOnEdt {
analyze(this) {
val kaType = ref.type
val superTypes = (kaType.allSupertypes(false) + kaType).toList()
superTypes.mapNotNull {
val classId = it.symbol?.classId?.takeIf { id -> id != StandardClassIds.Any }
classId?.asSingleFqName()
}
}
}
}
}
32 changes: 32 additions & 0 deletions src/IC-243/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.kotest.plugin.intellij.psi

import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.permissions.KaAllowAnalysisOnEdt
import org.jetbrains.kotlin.analysis.api.permissions.allowAnalysisOnEdt
import org.jetbrains.kotlin.analysis.api.types.symbol
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.psi.KtClassOrObject

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
@OptIn(KaAllowAnalysisOnEdt::class)
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries.mapNotNull { it.typeReference }
.flatMap { ref ->
// SurroundSelectionWithFunctionIntention.isAvailable is called in EDT before the intention is applied
// unfortunately API to avoid this was introduced in 23.2 only
// this we need to move intentions to the facade or accept EDT here until 23.2- are still supported
allowAnalysisOnEdt {
analyze(this) {
val kaType = ref.type
val superTypes = (kaType.allSupertypes(false) + kaType).toList()
superTypes.mapNotNull {
val classId = it.symbol?.classId?.takeIf { id -> id != StandardClassIds.Any }
classId?.asSingleFqName()
}
}
}
}
}
32 changes: 0 additions & 32 deletions src/main/kotlin/io/kotest/plugin/intellij/psi/classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.asJava.classes.KtLightClass
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isAbstract
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.typeUtil.supertypes

/**
* Returns the [KtClass] from this light class, otherwise null.
Expand Down Expand Up @@ -46,34 +42,6 @@ fun PsiElement.enclosingKtClass(): KtClass? = getStrictParentOfType()
fun PsiElement.enclosingKtClassOrObject(): KtClassOrObject? =
PsiTreeUtil.getParentOfType(this, KtClassOrObject::class.java)

/**
* Recursively returns the list of classes and interfaces extended or implemented by the class.
*/
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
return superTypeListEntries
.mapNotNull { it.typeReference }
.mapNotNull {
runCatching {
val bindingContext = it.analyze()
bindingContext.get(BindingContext.TYPE, it)
}.getOrNull()
}.flatMap {
runCatching {
it.supertypes() + it
}.getOrElse { emptyList() }
}.mapNotNull {
runCatching {
it.constructor.declarationDescriptor.classId
}.getOrNull()
}.mapNotNull {
runCatching {
val packageName = it.packageFqName
val simpleName = it.relativeClassName
FqName("$packageName.$simpleName")
}.getOrNull()
}.filterNot { it.toString() == "kotlin.Any" }
}

/**
* Returns true if this [KtClassOrObject] points to a runnable spec object.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import io.kotest.plugin.intellij.psi.specs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.kotlin.idea.core.util.toPsiFile
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtProperty
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@
</intentionAction>
</extensions>

<extensions defaultExtensionNs="org.jetbrains.kotlin">
<supportsKotlinPluginMode supportsK2="true" />
</extensions>

</idea-plugin>
Loading