Search in sources :

Example 31 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class PsiClassImpl method getContainingClass.

@Override
@Nullable
public PsiClass getContainingClass() {
    final PsiClassStub stub = getGreenStub();
    if (stub != null) {
        StubElement parent = stub.getParentStub();
        return parent instanceof PsiClassStub ? ((PsiClassStub<?>) parent).getPsi() : null;
    }
    PsiElement parent = getParent();
    if (parent instanceof PsiClassLevelDeclarationStatement) {
        return PsiTreeUtil.getParentOfType(this, PsiSyntheticClass.class);
    }
    return parent instanceof PsiClass ? (PsiClass) parent : null;
}
Also used : StubElement(com.intellij.psi.stubs.StubElement) PsiClassStub(com.intellij.psi.impl.java.stubs.PsiClassStub) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class PsiClassImpl method isAnonymousOrLocal.

private static boolean isAnonymousOrLocal(PsiClass aClass) {
    if (aClass instanceof PsiAnonymousClass)
        return true;
    final PsiClassStub stub = ((PsiClassImpl) aClass).getGreenStub();
    if (stub != null) {
        final StubElement parentStub = stub.getParentStub();
        return !(parentStub instanceof PsiClassStub || parentStub instanceof PsiFileStub);
    }
    PsiElement parent = aClass.getParent();
    while (parent != null) {
        if (parent instanceof PsiMethod || parent instanceof PsiField || parent instanceof PsiClassInitializer)
            return true;
        if (parent instanceof PsiClass || parent instanceof PsiFile)
            return false;
        parent = parent.getParent();
    }
    return false;
}
Also used : PsiFileStub(com.intellij.psi.stubs.PsiFileStub) StubElement(com.intellij.psi.stubs.StubElement) PsiClassStub(com.intellij.psi.impl.java.stubs.PsiClassStub)

Example 33 with StubElement

use of com.intellij.psi.stubs.StubElement 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 34 with StubElement

use of com.intellij.psi.stubs.StubElement 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)

Example 35 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class PyFunctionImpl method getContainingClass.

public PyClass getContainingClass() {
    final PyFunctionStub stub = getStub();
    if (stub != null) {
        final StubElement parentStub = stub.getParentStub();
        if (parentStub instanceof PyClassStub) {
            return ((PyClassStub) parentStub).getPsi();
        }
        return null;
    }
    final PsiElement parent = PsiTreeUtil.getParentOfType(this, StubBasedPsiElement.class);
    if (parent instanceof PyClass) {
        return (PyClass) parent;
    }
    return null;
}
Also used : PyClassStub(com.jetbrains.python.psi.stubs.PyClassStub) StubElement(com.intellij.psi.stubs.StubElement) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) PsiElement(com.intellij.psi.PsiElement) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement)

Aggregations

StubElement (com.intellij.psi.stubs.StubElement)38 Nullable (org.jetbrains.annotations.Nullable)9 PsiElement (com.intellij.psi.PsiElement)7 NotNull (org.jetbrains.annotations.NotNull)7 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)4 PsiClassStub (com.intellij.psi.impl.java.stubs.PsiClassStub)4 StubTree (com.intellij.psi.stubs.StubTree)4 StubBasedPsiElementBase (com.intellij.extapi.psi.StubBasedPsiElementBase)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)3 PsiFileStub (com.intellij.psi.stubs.PsiFileStub)3 SmartList (com.intellij.util.SmartList)3 PyClassStub (com.jetbrains.python.psi.stubs.PyClassStub)3 GoFileStub (com.goide.stubs.GoFileStub)2 ASTNode (com.intellij.lang.ASTNode)2 FileASTNode (com.intellij.lang.FileASTNode)2 PsiFile (com.intellij.psi.PsiFile)2 StubBuilder (com.intellij.psi.StubBuilder)2 PsiMethodStub (com.intellij.psi.impl.java.stubs.PsiMethodStub)2 IElementType (com.intellij.psi.tree.IElementType)2