Search in sources :

Example 21 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 22 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 23 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 24 with StubElement

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

the class Test method testSOEProof.

public void testSOEProof() {
    final StringBuilder sb = new StringBuilder();
    final SecureRandom random = new SecureRandom();
    sb.append("class SOE_test {\n BigInteger BIG = new BigInteger(\n");
    int i;
    for (i = 0; i < 100000; i++) {
        sb.append("  \"").append(Math.abs(random.nextInt())).append("\" +\n");
    }
    sb.append("  \"\");\n}");
    final PsiJavaFile file = (PsiJavaFile) createLightFile("SOE_test.java", sb.toString());
    long t = System.currentTimeMillis();
    final StubElement tree = NEW_BUILDER.buildStubTree(file);
    t = System.currentTimeMillis() - t;
    assertEquals("PsiJavaFileStub []\n" + "  IMPORT_LIST:PsiImportListStub\n" + "  CLASS:PsiClassStub[name=SOE_test fqn=SOE_test]\n" + "    MODIFIER_LIST:PsiModifierListStub[mask=4096]\n" + "    TYPE_PARAMETER_LIST:PsiTypeParameterListStub\n" + "    EXTENDS_LIST:PsiRefListStub[EXTENDS_LIST:]\n" + "    IMPLEMENTS_LIST:PsiRefListStub[IMPLEMENTS_LIST:]\n" + "    FIELD:PsiFieldStub[BIG:BigInteger=;INITIALIZER_NOT_STORED;]\n" + "      MODIFIER_LIST:PsiModifierListStub[mask=4096]\n", DebugUtil.stubTreeToString(tree));
    System.out.println("SOE depth=" + i + ", time=" + t + "ms");
}
Also used : SecureRandom(java.security.SecureRandom) StubElement(com.intellij.psi.stubs.StubElement)

Example 25 with StubElement

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

the class Test method doTest.

private static void doTest(@NonNls final String source, @NonNls @Nullable final String tree) {
    final PsiJavaFile file = (PsiJavaFile) createLightFile("test.java", source);
    final FileASTNode fileNode = file.getNode();
    assertNotNull(fileNode);
    assertFalse(fileNode.isParsed());
    long t1 = System.nanoTime();
    final StubElement lighterTree = NEW_BUILDER.buildStubTree(file);
    t1 = Math.max((System.nanoTime() - t1) / 1000, 1);
    assertFalse(fileNode.isParsed());
    // force switch to AST
    file.getNode().getChildren(null);
    long t2 = System.nanoTime();
    // build over AST
    final StubElement lighterTree2 = NEW_BUILDER.buildStubTree(file);
    t2 = Math.max((System.nanoTime() - t2) / 1000, 1);
    file.accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            assert !(element instanceof PsiErrorElement) : element;
            super.visitElement(element);
        }
    });
    final String lightStr = DebugUtil.stubTreeToString(lighterTree);
    final String lightStr2 = DebugUtil.stubTreeToString(lighterTree2);
    if (tree != null) {
        System.out.println("light=" + t1 + "mks, heavy=" + t2 + "mks");
        if (!tree.isEmpty()) {
            assertEquals("light tree differs", tree, lightStr);
            assertEquals("light tree (2nd) differs", tree, lightStr2);
        }
    }
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) StubElement(com.intellij.psi.stubs.StubElement)

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