allow psi.getStub to return something after AST is GC-ed

This commit is contained in:
peter
2017-01-20 09:20:11 +01:00
parent 57ae246f60
commit 8a146b1014
4 changed files with 44 additions and 4 deletions
@@ -19,7 +19,9 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.psi.impl.source.PsiClassImpl
import com.intellij.psi.impl.source.PsiFileImpl
import com.intellij.psi.impl.source.PsiJavaFileImpl
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.searches.DirectClassInheritorsSearch
import com.intellij.psi.search.searches.OverridingMethodsSearch
@@ -235,4 +237,15 @@ class B {
assert !file.node.parsed
}
void "test load stub from non-file PSI after AST is unloaded"() {
PsiJavaFileImpl file = (PsiJavaFileImpl)myFixture.addFileToProject("a.java", "class A<T>{}")
def cls = file.classes[0]
assert cls.nameIdentifier
GCUtil.tryGcSoftlyReachableObjects()
assert !file.treeElement
assert ((PsiClassImpl) cls).stub
}
}
@@ -371,7 +371,7 @@ public class StubBasedPsiElementBase<T extends StubElement> extends ASTDelegateP
public T getStub() {
ProgressIndicatorProvider.checkCanceled(); // Hope, this is called often
//noinspection unchecked
return (T)mySubstrateRef.getStub();
return (T)mySubstrateRef.getStub(myStubIndex);
}
/**
@@ -42,13 +42,13 @@ public abstract class SubstrateRef {
public abstract ASTNode getNode();
@Nullable
public Stub getStub() {
public Stub getStub(int stubIndex) {
return null;
}
@Nullable
public Stub getGreenStub(int index) {
return getStub();
return getStub(index);
}
public abstract boolean isValid();
@@ -119,7 +119,7 @@ public abstract class SubstrateRef {
@NotNull
@Override
public Stub getStub() {
public Stub getStub(int stubIndex) {
return myStub;
}
@@ -57,6 +57,18 @@ public abstract class AstPath extends SubstrateRef {
protected abstract int getDepth();
@Nullable
@Override
public Stub getStub(int stubIndex) {
if (stubIndex < 0) return null;
StubTree stubTree = getFileStubTree();
return stubTree == null ? null : stubTree.getPlainList().get(stubIndex);
}
@Nullable
protected abstract StubTree getFileStubTree();
@Nullable
@Override
public Stub getGreenStub(int stubIndex) {
@@ -165,6 +177,11 @@ public abstract class AstPath extends SubstrateRef {
return 1 + myParent.getDepth();
}
@Override
protected StubTree getFileStubTree() {
return myParent.getFileStubTree();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -204,6 +221,11 @@ public abstract class AstPath extends SubstrateRef {
return node;
}
@Override
protected StubTree getFileStubTree() {
return SoftReference.dereference(myNode) == null ? myFile.getStubTree() : null;
}
@NotNull
@Override
public PsiFileImpl getContainingFile() {
@@ -249,6 +271,11 @@ public abstract class AstPath extends SubstrateRef {
protected int getDepth() {
return 0;
}
@Override
protected StubTree getFileStubTree() {
return myFile.getStubTree();
}
}
}