Skip to content

Commit 81bd846

Browse files
committed
Fix ProhibitedAnalysisException in KotestStructureViewExtension
1 parent 78eb2a4 commit 81bd846

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

src/IC-242/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.kotest.plugin.intellij.psi
22

3+
import com.intellij.openapi.application.runReadAction
34
import org.jetbrains.kotlin.analysis.api.analyze
45
import org.jetbrains.kotlin.analysis.api.permissions.KaAllowAnalysisOnEdt
5-
import org.jetbrains.kotlin.analysis.api.permissions.allowAnalysisOnEdt
66
import org.jetbrains.kotlin.analysis.api.types.symbol
77
import org.jetbrains.kotlin.name.FqName
88
import org.jetbrains.kotlin.name.StandardClassIds
@@ -15,10 +15,7 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
1515
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
1616
return superTypeListEntries.mapNotNull { it.typeReference }
1717
.flatMap { ref ->
18-
// SurroundSelectionWithFunctionIntention.isAvailable is called in EDT before the intention is applied
19-
// unfortunately API to avoid this was introduced in 23.2 only
20-
// this we need to move intentions to the facade or accept EDT here until 23.2- are still supported
21-
allowAnalysisOnEdt {
18+
runReadAction {
2219
analyze(this) {
2320
val kaType = ref.type
2421
val superTypes = (kaType.allSupertypes(false) + kaType).toList()

src/IC-243/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
1515
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
1616
return superTypeListEntries.mapNotNull { it.typeReference }
1717
.flatMap { ref ->
18-
// SurroundSelectionWithFunctionIntention.isAvailable is called in EDT before the intention is applied
19-
// unfortunately API to avoid this was introduced in 23.2 only
20-
// this we need to move intentions to the facade or accept EDT here until 23.2- are still supported
21-
allowAnalysisOnEdt {
22-
analyze(this) {
23-
val kaType = ref.type
24-
val superTypes = (kaType.allSupertypes(false) + kaType).toList()
25-
superTypes.mapNotNull {
26-
val classId = it.symbol?.classId?.takeIf { id -> id != StandardClassIds.Any }
27-
classId?.asSingleFqName()
28-
}
18+
analyze(this) {
19+
val kaType = ref.type
20+
val superTypes = (kaType.allSupertypes(false) + kaType).toList()
21+
superTypes.mapNotNull {
22+
val classId = it.symbol?.classId?.takeIf { id -> id != StandardClassIds.Any }
23+
classId?.asSingleFqName()
2924
}
3025
}
3126
}

src/IC-251/kotlin/io/kotest/plugin/intellij/psi/superClasses.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
1515
fun KtClassOrObject.getAllSuperClasses(): List<FqName> {
1616
return superTypeListEntries.mapNotNull { it.typeReference }
1717
.flatMap { ref ->
18-
// SurroundSelectionWithFunctionIntention.isAvailable is called in EDT before the intention is applied
19-
// unfortunately API to avoid this was introduced in 23.2 only
20-
// this we need to move intentions to the facade or accept EDT here until 23.2- are still supported
21-
allowAnalysisOnEdt {
22-
analyze(this) {
23-
val kaType = ref.type
24-
val superTypes = (kaType.allSupertypes(false) + kaType).toList()
25-
superTypes.mapNotNull {
26-
val classId = it.symbol?.classId?.takeIf { id -> id != StandardClassIds.Any }
27-
classId?.asSingleFqName()
28-
}
18+
analyze(this) {
19+
val kaType = ref.type
20+
val superTypes = (kaType.allSupertypes(false) + kaType).toList()
21+
superTypes.mapNotNull {
22+
val classId = it.symbol?.classId?.takeIf { id -> id != StandardClassIds.Any }
23+
classId?.asSingleFqName()
2924
}
3025
}
3126
}

src/main/kotlin/io/kotest/plugin/intellij/intentions/SurroundSelectionWithFunctionIntention.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.kotest.plugin.intellij.intentions
22

33
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
4+
import com.intellij.openapi.application.ApplicationManager
45
import com.intellij.openapi.editor.Editor
56
import com.intellij.openapi.project.Project
67
import com.intellij.openapi.util.TextRange
@@ -15,6 +16,9 @@ import org.jetbrains.kotlin.resolve.ImportPath
1516
abstract class SurroundSelectionWithFunctionIntention : PsiElementBaseIntentionAction() {
1617

1718
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
19+
if (ApplicationManager.getApplication().isDispatchThread) {
20+
return false
21+
}
1822
return try {
1923
editor?.selectionModel?.hasSelection() == true && element.isContainedInSpec()
2024
} catch (e: Exception) {

src/main/kotlin/io/kotest/plugin/intellij/structure/KotestStructureViewExtension.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import com.intellij.ide.structureView.StructureViewExtension
55
import com.intellij.ide.structureView.StructureViewTreeElement
66
import com.intellij.ide.util.treeView.smartTree.TreeElement
77
import com.intellij.navigation.ItemPresentation
8+
import com.intellij.openapi.application.ApplicationManager
89
import com.intellij.openapi.editor.Editor
9-
import com.intellij.openapi.project.DumbService
1010
import com.intellij.psi.NavigatablePsiElement
1111
import com.intellij.psi.PsiElement
1212
import io.kotest.plugin.intellij.Test
@@ -25,11 +25,14 @@ class KotestStructureViewExtension : StructureViewExtension {
2525
}
2626

2727
override fun getChildren(parent: PsiElement): Array<StructureViewTreeElement> {
28-
if (DumbService.isDumb(parent.project)) return emptyArray()
28+
if (ApplicationManager.getApplication().isDispatchThread) {
29+
return emptyArray()
30+
}
2931
val ktClassOrObject = parent as? KtClassOrObject ?: return emptyArray()
3032
val spec = ktClassOrObject.specStyle() ?: return emptyArray()
3133
val tests = spec.tests(parent, false)
32-
return tests.map { KotestTestStructureViewTreeElement(it) }.toTypedArray()
34+
val children = tests.map { KotestTestStructureViewTreeElement(it) }
35+
return children.toTypedArray()
3336
}
3437

3538
class KotestTestStructureViewTreeElement(private val testElement: TestElement) : StructureViewTreeElement {

0 commit comments

Comments
 (0)