[psi] don't load text for compiled code

use stubs when possible, when stub doesn't contain anything,
decompiled text won't contain it either,
no sense to load
This commit is contained in:
Anna Kozlova
2023-03-21 20:27:36 +01:00
committed by teamcity
parent e9c8be3b64
commit 956294821b
9 changed files with 344 additions and 86 deletions
@@ -125,13 +125,17 @@ public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunction
@Nullable
public KtExpression getBodyExpression() {
KotlinFunctionStub stub = getStub();
if (stub != null && !stub.hasBody()) {
return null;
if (stub != null) {
if (!stub.hasBody()) {
return null;
}
if (getContainingKtFile().isCompiled()) {
//don't load ast
return null;
}
}
return AstLoadingFilter.forceAllowTreeLoading(this.getContainingFile(), () ->
findChildByClass(KtExpression.class)
);
return findChildByClass(KtExpression.class);
}
@Nullable
@@ -279,9 +279,7 @@ public class KtProperty extends KtTypeParameterListOwnerStub<KotlinPropertyStub>
}
}
return AstLoadingFilter.forceAllowTreeLoading(this.getContainingFile(), () ->
PsiTreeUtil.getNextSiblingOfType(findChildByType(EQ), KtExpression.class)
);
return PsiTreeUtil.getNextSiblingOfType(findChildByType(EQ), KtExpression.class);
}
public boolean hasDelegateExpressionOrInitializer() {
@@ -89,13 +89,17 @@ public class KtPropertyAccessor extends KtDeclarationStub<KotlinPropertyAccessor
@Override
public KtExpression getBodyExpression() {
KotlinPropertyAccessorStub stub = getStub();
if (stub != null && !stub.hasBody()) {
return null;
if (stub != null) {
if (!stub.hasBody()) {
return null;
}
if (getContainingKtFile().isCompiled()) {
return null;
}
}
return AstLoadingFilter.forceAllowTreeLoading(this.getContainingFile(), () ->
findChildByClass(KtExpression.class)
);
return findChildByClass(KtExpression.class);
}
@Nullable
@@ -225,6 +225,7 @@ inline fun <reified T : PsiElement> PsiElement.forEachDescendantOfType(
crossinline canGoInside: (PsiElement) -> Boolean,
noinline action: (T) -> Unit
) {
checkDecompiledText()
this.accept(object : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) {
if (canGoInside(element)) {
@@ -257,6 +258,7 @@ inline fun <reified T : PsiElement> PsiElement.findDescendantOfType(
crossinline canGoInside: (PsiElement) -> Boolean,
noinline predicate: (T) -> Boolean = { true }
): T? {
checkDecompiledText()
var result: T? = null
this.accept(object : PsiRecursiveElementWalkingVisitor() {
override fun visitElement(element: PsiElement) {
@@ -274,6 +276,13 @@ inline fun <reified T : PsiElement> PsiElement.findDescendantOfType(
return result
}
fun PsiElement.checkDecompiledText() {
val file = containingFile
if (file is KtFile && file.isCompiled) {
error("Attempt to load decompiled text, please use stubs instead. Decompile process might be slow and should be avoided")
}
}
inline fun <reified T : PsiElement> PsiElement.collectDescendantsOfType(noinline predicate: (T) -> Boolean = { true }): List<T> {
return collectDescendantsOfType({ true }, predicate)
}