[light classes] rewrite findEntry to avoid resolve and move to light-classes-base module
^KT-53097
This commit is contained in:
+20
@@ -13,9 +13,29 @@ import com.intellij.util.IncorrectOperationException
|
||||
import org.jetbrains.kotlin.analyzer.KotlinModificationTrackerService
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
fun KtSuperTypeList.findEntry(fqNameToFind: String): KtSuperTypeListEntry? {
|
||||
val name = fqNameToFind.substringAfterLast(delimiter = '.', missingDelimiterValue = "")
|
||||
if (name.isEmpty()) {
|
||||
return entries.find { it.typeAsUserType?.textMatches(fqNameToFind) == true }
|
||||
}
|
||||
|
||||
val qualifier = fqNameToFind.substringBeforeLast('.')
|
||||
val entries = entries.mapNotNull { entry -> entry.typeAsUserType?.let { entry to it } }
|
||||
.filter { (_, type) -> type.referencedName == name && (type.qualifier?.textMatches(qualifier) != false) }
|
||||
.ifEmpty { return null }
|
||||
|
||||
return if (entries.size == 1) {
|
||||
entries.first().first
|
||||
} else {
|
||||
val entry = entries.firstOrNull { it.second.qualifier != null } ?: entries.firstOrNull()
|
||||
entry?.first
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: avoid using blocking lazy in light classes, it leads to deadlocks
|
||||
fun <T> lazyPub(initializer: () -> T) = lazy(LazyThreadSafetyMode.PUBLICATION, initializer)
|
||||
|
||||
@Suppress("UnusedReceiverParameter")
|
||||
fun LightElement.cannotModify(): Nothing {
|
||||
throw IncorrectOperationException("Modification not implemented.")
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.asJava.classes
|
||||
|
||||
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtSuperTypeList
|
||||
import org.jetbrains.kotlin.psi.KtSuperTypeListEntry
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
|
||||
fun KtSuperTypeList.findEntry(fqNameToFind: String): KtSuperTypeListEntry? {
|
||||
val context = LightClassGenerationSupport.getInstance(project).analyzeWithContent(parent as KtClassOrObject)
|
||||
return entries.firstOrNull {
|
||||
val referencedType = context[BindingContext.TYPE, it.typeReference]
|
||||
referencedType?.constructor?.declarationDescriptor?.fqNameUnsafe?.asString() == fqNameToFind
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user