use of com.intellij.psi.impl.source.PsiFileWithStubSupport in project intellij-community by JetBrains.
the class CompilerHierarchyInfoImpl method getHierarchyChildren.
@Override
@NotNull
public Stream<PsiElement> getHierarchyChildren() {
PsiManager psiManager = PsiManager.getInstance(myProject);
final LanguageLightRefAdapter adapter = ObjectUtils.notNull(CompilerReferenceServiceImpl.findAdapterForFileType(mySearchFileType));
return myCandidatePerFile.entrySet().stream().filter(e -> mySearchScope.contains(e.getKey())).flatMap(e -> {
final VirtualFile file = e.getKey();
final Object[] definitions = e.getValue();
final PsiElement[] hierarchyChildren = ReadAction.compute(() -> {
final PsiFileWithStubSupport psiFile = (PsiFileWithStubSupport) psiManager.findFile(file);
return mySearchType.performSearchInFile(definitions, myBaseClass, psiFile, adapter);
});
if (hierarchyChildren.length == definitions.length) {
return Stream.of(hierarchyChildren);
} else {
LOG.assertTrue(mySearchType == CompilerHierarchySearchType.DIRECT_INHERITOR, "Should not happens for functional expression search");
return Stream.of(hierarchyChildren).filter(c -> ReadAction.compute(() -> adapter.isDirectInheritor(c, myBaseClass)));
}
});
}
use of com.intellij.psi.impl.source.PsiFileWithStubSupport in project intellij-community by JetBrains.
the class SmartPsiElementPointerImpl method createAnchorInfo.
@Nullable
private static SmartPointerElementInfo createAnchorInfo(@NotNull PsiElement element, @NotNull PsiFile containingFile) {
if (element instanceof StubBasedPsiElement && containingFile instanceof PsiFileWithStubSupport) {
PsiFileWithStubSupport stubFile = (PsiFileWithStubSupport) containingFile;
StubTree stubTree = stubFile.getStubTree();
if (stubTree != null) {
// use stubs when tree is not loaded
StubBasedPsiElement stubPsi = (StubBasedPsiElement) element;
int stubId = PsiAnchor.calcStubIndex(stubPsi);
IStubElementType myStubElementType = stubPsi.getElementType();
IStubFileElementType elementTypeForStubBuilder = ((PsiFileImpl) containingFile).getElementTypeForStubBuilder();
if (stubId != -1 && elementTypeForStubBuilder != null) {
// TemplateDataElementType is not IStubFileElementType
return new AnchorElementInfo(element, stubFile, stubId, myStubElementType);
}
}
}
Pair<Identikit.ByAnchor, PsiElement> pair = Identikit.withAnchor(element, LanguageUtil.getRootLanguage(containingFile));
if (pair != null) {
return new AnchorElementInfo(pair.second, containingFile, pair.first);
}
return null;
}
use of com.intellij.psi.impl.source.PsiFileWithStubSupport in project intellij-community by JetBrains.
the class AnchorElementInfo method restoreElement.
@Override
@Nullable
public PsiElement restoreElement() {
long typeAndId = myStubElementTypeAndId;
int stubId = (int) typeAndId;
if (stubId != -1) {
PsiFile file = restoreFile();
if (!(file instanceof PsiFileWithStubSupport))
return null;
short index = (short) (typeAndId >> 32);
IStubElementType stubElementType = (IStubElementType) IElementType.find(index);
return PsiAnchor.restoreFromStubIndex((PsiFileWithStubSupport) file, stubId, stubElementType, false);
}
return super.restoreElement();
}
use of com.intellij.psi.impl.source.PsiFileWithStubSupport in project intellij-community by JetBrains.
the class StubProcessingHelperBase method processStubsInFile.
public <Psi extends PsiElement> boolean processStubsInFile(@NotNull final Project project, @NotNull final VirtualFile file, @NotNull StubIdList value, @NotNull final Processor<? super Psi> processor, @NotNull Class<Psi> requiredClass, final boolean skipOnErrors) {
StubTree stubTree = null;
PsiFile candidatePsiFile = PsiManager.getInstance(project).findFile(file);
PsiFileWithStubSupport psiFile = null;
boolean customStubs = false;
if (candidatePsiFile != null && !(candidatePsiFile instanceof PsiPlainTextFile)) {
final FileViewProvider viewProvider = candidatePsiFile.getViewProvider();
final PsiFile stubBindingRoot = viewProvider.getStubBindingRoot();
if (stubBindingRoot instanceof PsiFileWithStubSupport) {
psiFile = (PsiFileWithStubSupport) stubBindingRoot;
stubTree = psiFile.getStubTree();
if (stubTree == null && psiFile instanceof PsiFileImpl) {
IStubFileElementType elementType = ((PsiFileImpl) psiFile).getElementTypeForStubBuilder();
if (elementType != null) {
stubTree = ((PsiFileImpl) psiFile).calcStubTree();
} else {
customStubs = true;
if (BinaryFileStubBuilders.INSTANCE.forFileType(psiFile.getFileType()) == null) {
LOG.error("unable to get stub builder for " + psiFile.getFileType() + ", " + StubTreeLoader.getFileViewProviderMismatchDiagnostics(viewProvider));
}
}
}
}
}
if (stubTree == null && psiFile == null) {
return true;
}
if (stubTree == null) {
ObjectStubTree objectStubTree = StubTreeLoader.getInstance().readFromVFile(project, file);
if (objectStubTree == null) {
return true;
}
if (customStubs && !(objectStubTree instanceof StubTree)) {
if (!skipOnErrors && !requiredClass.isInstance(psiFile)) {
inconsistencyDetected(objectStubTree, psiFile);
return true;
}
// e.g. dom indices
return processor.process((Psi) psiFile);
}
stubTree = (StubTree) objectStubTree;
final List<StubElement<?>> plained = stubTree.getPlainListFromAllRoots();
for (int i = 0, size = value.size(); i < size; i++) {
final int stubTreeIndex = value.get(i);
if (stubTreeIndex >= plained.size()) {
if (!skipOnErrors)
onInternalError(file);
break;
}
final StubElement<?> stub = plained.get(stubTreeIndex);
PsiUtilCore.ensureValid(psiFile);
final ASTNode tree = psiFile.findTreeForStub(stubTree, stub);
if (tree != null) {
if (tree.getElementType() == stubType(stub)) {
Psi psi = (Psi) tree.getPsi();
PsiUtilCore.ensureValid(psi);
if (!skipOnErrors && !requiredClass.isInstance(psi)) {
inconsistencyDetected(stubTree, psiFile);
break;
}
if (!processor.process(psi))
return false;
} else if (!skipOnErrors) {
String persistedStubTree = ((PsiFileStubImpl) stubTree.getRoot()).printTree();
String stubTreeJustBuilt = ((PsiFileStubImpl) ((PsiFileImpl) psiFile).getElementTypeForStubBuilder().getBuilder().buildStubTree(psiFile)).printTree();
StringBuilder builder = new StringBuilder();
builder.append("Oops\n");
builder.append("Recorded stub:-----------------------------------\n");
builder.append(persistedStubTree);
builder.append("\nAST built stub: ------------------------------------\n");
builder.append(stubTreeJustBuilt);
builder.append("\n");
LOG.info(builder.toString());
onInternalError(file);
}
}
}
} else {
final List<StubElement<?>> plained = stubTree.getPlainListFromAllRoots();
for (int i = 0, size = value.size(); i < size; i++) {
final int stubTreeIndex = value.get(i);
if (stubTreeIndex >= plained.size()) {
if (!skipOnErrors) {
inconsistencyDetected(stubTree, psiFile);
}
break;
}
Psi psi = (Psi) plained.get(stubTreeIndex).getPsi();
PsiUtilCore.ensureValid(psi);
if (!skipOnErrors && !requiredClass.isInstance(psi)) {
inconsistencyDetected(stubTree, psiFile);
break;
}
if (!processor.process(psi))
return false;
}
}
return true;
}
Aggregations