Search in sources :

Example 6 with IStubFileElementType

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;
}
Also used : PsiFileWithStubSupport(com.intellij.psi.impl.source.PsiFileWithStubSupport) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) StubTree(com.intellij.psi.stubs.StubTree) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) IStubElementType(com.intellij.psi.stubs.IStubElementType) ForeignLeafPsiElement(com.intellij.psi.impl.source.tree.ForeignLeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with IStubFileElementType

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl)

Example 8 with IStubFileElementType

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;
}
Also used : IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFileWithStubSupport(com.intellij.psi.impl.source.PsiFileWithStubSupport) ASTNode(com.intellij.lang.ASTNode)

Example 9 with IStubFileElementType

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));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) Trinity(com.intellij.openapi.util.Trinity) Language(com.intellij.lang.Language) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with IStubFileElementType

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;
}
Also used : IFileElementType(com.intellij.psi.tree.IFileElementType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) ParserDefinition(com.intellij.lang.ParserDefinition) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType)

Aggregations

IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)12 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)5 Language (com.intellij.lang.Language)4 PsiFile (com.intellij.psi.PsiFile)4 IElementType (com.intellij.psi.tree.IElementType)4 ASTNode (com.intellij.lang.ASTNode)3 ParserDefinition (com.intellij.lang.ParserDefinition)3 FileType (com.intellij.openapi.fileTypes.FileType)3 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)3 StubBuilder (com.intellij.psi.StubBuilder)3 IStubElementType (com.intellij.psi.stubs.IStubElementType)3 IFileElementType (com.intellij.psi.tree.IFileElementType)3 Pair (com.intellij.openapi.util.Pair)2 FileViewProvider (com.intellij.psi.FileViewProvider)2 PsiFileWithStubSupport (com.intellij.psi.impl.source.PsiFileWithStubSupport)2 StubElement (com.intellij.psi.stubs.StubElement)2 SmartList (com.intellij.util.SmartList)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 StubBasedPsiElementBase (com.intellij.extapi.psi.StubBasedPsiElementBase)1