use of com.intellij.psi.stubs.StubTree in project intellij-community by JetBrains.
the class PsiAnchor method calcStubIndex.
public static int calcStubIndex(@NotNull StubBasedPsiElement psi) {
if (psi instanceof PsiFile) {
return 0;
}
final StubElement liveStub = psi.getStub();
if (liveStub != null) {
return ((StubBase) liveStub).id;
}
PsiFileImpl file = (PsiFileImpl) psi.getContainingFile();
final StubTree stubTree = file.calcStubTree();
for (StubElement<?> stb : stubTree.getPlainList()) {
if (stb.getPsi() == psi) {
return ((StubBase) stb).id;
}
}
// it is possible via custom stub builder intentionally not producing stubs for stubbed elements
return -1;
}
use of com.intellij.psi.stubs.StubTree in project intellij-community by JetBrains.
the class PsiAnchor method restoreFromStubIndex.
@Nullable
public static PsiElement restoreFromStubIndex(PsiFileWithStubSupport fileImpl, int index, @NotNull IStubElementType elementType, boolean throwIfNull) {
if (fileImpl == null) {
if (throwIfNull)
throw new AssertionError("Null file");
return null;
}
StubTree tree = fileImpl.getStubTree();
if (tree == null) {
if (fileImpl instanceof PsiFileImpl) {
// Note: as far as this is a realization of StubIndexReference fileImpl#getContentElementType() must be instance of IStubFileElementType
tree = ((PsiFileImpl) fileImpl).calcStubTree();
} else {
if (throwIfNull)
throw new AssertionError("Not PsiFileImpl: " + fileImpl.getClass());
return null;
}
}
List<StubElement<?>> list = tree.getPlainList();
if (index >= list.size()) {
if (throwIfNull)
throw new AssertionError("Too large index: " + index + ">=" + list.size());
return null;
}
StubElement stub = list.get(index);
if (stub.getStubType() != elementType) {
if (throwIfNull)
throw new AssertionError("Element type mismatch: " + stub.getStubType() + "!=" + elementType);
return null;
}
return stub.getPsi();
}
Aggregations