Search in sources :

Example 1 with PsiFileWithStubSupport

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)));
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CompilerDirectHierarchyInfo(com.intellij.compiler.CompilerDirectHierarchyInfo) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) FileType(com.intellij.openapi.fileTypes.FileType) PsiFileWithStubSupport(com.intellij.psi.impl.source.PsiFileWithStubSupport) ReadAction(com.intellij.openapi.application.ReadAction) PsiManager(com.intellij.psi.PsiManager) Stream(java.util.stream.Stream) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiNamedElement(com.intellij.psi.PsiNamedElement) ObjectUtils(com.intellij.util.ObjectUtils) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFileWithStubSupport(com.intellij.psi.impl.source.PsiFileWithStubSupport) PsiManager(com.intellij.psi.PsiManager) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiFileWithStubSupport

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;
}
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 3 with PsiFileWithStubSupport

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

Example 4 with PsiFileWithStubSupport

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;
}
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)

Aggregations

PsiFileWithStubSupport (com.intellij.psi.impl.source.PsiFileWithStubSupport)4 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)2 IStubElementType (com.intellij.psi.stubs.IStubElementType)2 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)2 Nullable (org.jetbrains.annotations.Nullable)2 CompilerDirectHierarchyInfo (com.intellij.compiler.CompilerDirectHierarchyInfo)1 ASTNode (com.intellij.lang.ASTNode)1 ReadAction (com.intellij.openapi.application.ReadAction)1 Logger (com.intellij.openapi.diagnostic.Logger)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiManager (com.intellij.psi.PsiManager)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 ForeignLeafPsiElement (com.intellij.psi.impl.source.tree.ForeignLeafPsiElement)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 StubTree (com.intellij.psi.stubs.StubTree)1 ObjectUtils (com.intellij.util.ObjectUtils)1