use of com.intellij.psi.tree.IStubFileElementType 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.tree.IStubFileElementType in project intellij-community by JetBrains.
the class PsiAnchor method canHaveStub.
private static boolean canHaveStub(PsiFile file) {
if (!(file instanceof PsiFileImpl))
return false;
VirtualFile vFile = file.getVirtualFile();
IStubFileElementType elementType = ((PsiFileImpl) file).getElementTypeForStubBuilder();
return elementType != null && vFile != null && elementType.shouldBuildStubFor(vFile);
}
use of com.intellij.psi.tree.IStubFileElementType 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;
}
use of com.intellij.psi.tree.IStubFileElementType in project intellij-community by JetBrains.
the class StubTreeBuilder method getStubbedRoots.
/** Order is deterministic. First element matches {@link FileViewProvider#getStubBindingRoot()} */
@NotNull
public static List<Pair<IStubFileElementType, PsiFile>> getStubbedRoots(@NotNull FileViewProvider viewProvider) {
final List<Trinity<Language, IStubFileElementType, PsiFile>> roots = new SmartList<>();
final PsiFile stubBindingRoot = viewProvider.getStubBindingRoot();
for (Language language : viewProvider.getLanguages()) {
final PsiFile file = viewProvider.getPsi(language);
if (file instanceof PsiFileImpl) {
final IElementType type = ((PsiFileImpl) file).getElementTypeForStubBuilder();
if (type != null) {
roots.add(Trinity.create(language, (IStubFileElementType) type, file));
}
}
}
ContainerUtil.sort(roots, (o1, o2) -> {
if (o1.third == stubBindingRoot)
return o2.third == stubBindingRoot ? 0 : -1;
else if (o2.third == stubBindingRoot)
return 1;
else
return StringUtil.compare(o1.first.getID(), o2.first.getID(), false);
});
return ContainerUtil.map(roots, trinity -> Pair.create(trinity.second, trinity.third));
}
use of com.intellij.psi.tree.IStubFileElementType in project intellij-community by JetBrains.
the class CoreStubTreeLoader method canHaveStub.
@Override
public boolean canHaveStub(VirtualFile file) {
final FileType fileType = file.getFileType();
if (fileType instanceof LanguageFileType) {
Language l = ((LanguageFileType) fileType).getLanguage();
ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l);
if (parserDefinition == null)
return false;
final IFileElementType elementType = parserDefinition.getFileNodeType();
return elementType instanceof IStubFileElementType && ((IStubFileElementType) elementType).shouldBuildStubFor(file);
} else if (fileType.isBinary()) {
final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
return builder != null && builder.acceptsFile(file);
}
return false;
}
Aggregations