[psi][AA] don't decompile code when stubs have enough information

^ KTIJ-25979
+ do not assert when there are no stubs (text is already computed)
This commit is contained in:
Anna Kozlova
2023-06-28 13:11:24 +02:00
committed by teamcity
parent 4ccea40651
commit 11c4fcda5f
2 changed files with 21 additions and 5 deletions
@@ -50,6 +50,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi import org.jetbrains.kotlin.psi
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
@@ -142,15 +143,30 @@ internal object FirReferenceResolveHelper {
return when (qualified) { return when (qualified) {
null -> FqName(expression.getReferencedName()) null -> FqName(expression.getReferencedName())
else -> { else -> {
qualified val refs =
.collectDescendantsOfType<KtSimpleNameExpression>() if (qualified is KtUserType && qualified.stub != null && (qualified.containingFile as? KtFile)?.isCompiled == true) {
.dropWhile { it.getReferencedName() == ROOT_PREFIX_FOR_IDE_RESOLUTION_MODE } collectTypeReferences(qualified)
.joinToString(separator = ".") { it.getReferencedName() } } else {
qualified.collectDescendantsOfType<KtSimpleNameExpression>()
}
refs.map { it.getReferencedName() }
.dropWhile { it == ROOT_PREFIX_FOR_IDE_RESOLUTION_MODE }
.joinToString(separator = ".")
.let(::FqName) .let(::FqName)
} }
} }
} }
private fun collectTypeReferences(qualified: KtUserType): MutableList<KtNameReferenceExpression> {
val refs = mutableListOf<KtNameReferenceExpression>()
fun collectFragments(type: KtUserType) {
type.getStubOrPsiChild(KtStubElementTypes.USER_TYPE)?.let { collectFragments(it) }
refs.add(type.referenceExpression as? KtNameReferenceExpression ?: return)
}
collectFragments(qualified)
return refs
}
private fun KtSimpleNameExpression.isPartOfQualifiedExpression(): Boolean { private fun KtSimpleNameExpression.isPartOfQualifiedExpression(): Boolean {
var parent = parent var parent = parent
while (parent is KtDotQualifiedExpression) { while (parent is KtDotQualifiedExpression) {
@@ -278,7 +278,7 @@ inline fun <reified T : PsiElement> PsiElement.findDescendantOfType(
fun PsiElement.checkDecompiledText() { fun PsiElement.checkDecompiledText() {
val file = containingFile val file = containingFile
if (file is KtFile && file.isCompiled) { if (file is KtFile && file.isCompiled && file.stub != null) {
error("Attempt to load decompiled text, please use stubs instead. Decompile process might be slow and should be avoided") error("Attempt to load decompiled text, please use stubs instead. Decompile process might be slow and should be avoided")
} }
} }