Search in sources :

Example 11 with StubTree

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;
}
Also used : StubBase(com.intellij.psi.stubs.StubBase) StubTree(com.intellij.psi.stubs.StubTree) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) StubElement(com.intellij.psi.stubs.StubElement)

Example 12 with StubTree

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

Aggregations

StubTree (com.intellij.psi.stubs.StubTree)12 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)6 StubElement (com.intellij.psi.stubs.StubElement)4 Nullable (org.jetbrains.annotations.Nullable)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Stub (com.intellij.psi.stubs.Stub)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 NotNull (org.jetbrains.annotations.NotNull)2 JSFile (com.intellij.lang.javascript.psi.JSFile)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 JSClassStub (com.intellij.lang.javascript.psi.stubs.JSClassStub)1 Attachment (com.intellij.openapi.diagnostic.Attachment)1 Module (com.intellij.openapi.module.Module)1 PsiClass (com.intellij.psi.PsiClass)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiFunctionalExpression (com.intellij.psi.PsiFunctionalExpression)1 PsiClassStub (com.intellij.psi.impl.java.stubs.PsiClassStub)1 PsiClassStubImpl (com.intellij.psi.impl.java.stubs.impl.PsiClassStubImpl)1