Search in sources :

Example 1 with StubTree

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

the class JavaStubIndexer method indexFile.

@Nullable
@Override
public Unit indexFile(@NotNull FileContent content) {
    Stub stubTree = StubTreeBuilder.buildStubTree(content);
    if (!(stubTree instanceof PsiJavaFileStub))
        return null;
    PsiJavaFileStub javaFileStub = (PsiJavaFileStub) stubTree;
    new StubTree(javaFileStub, false);
    List<ClassDecl> classList = new ArrayList<>();
    Set<String> usedNames = new HashSet<>();
    for (StubElement<?> el : javaFileStub.getChildrenStubs()) {
        if (el instanceof PsiClassStubImpl) {
            classList.add(processClassDecl((PsiClassStubImpl<?>) el, usedNames));
        }
    }
    List<IndexTree.Import> importList = new ArrayList<>();
    for (StubElement<?> el : javaFileStub.getChildrenStubs()) {
        if (el instanceof PsiImportListStub) {
            processImport((PsiImportListStub) el, importList, usedNames);
        }
    }
    ClassDecl[] classes = classList.isEmpty() ? ClassDecl.EMPTY_ARRAY : classList.toArray(new ClassDecl[classList.size()]);
    IndexTree.Import[] imports = importList.isEmpty() ? IndexTree.Import.EMPTY_ARRAY : importList.toArray(new IndexTree.Import[importList.size()]);
    byte type = javaFileStub.isCompiled() ? IndexTree.BYTECODE : IndexTree.JAVA;
    return new Unit(javaFileStub.getPackageName(), type, imports, classes);
}
Also used : PsiClassStubImpl(com.intellij.psi.impl.java.stubs.impl.PsiClassStubImpl) StubTree(com.intellij.psi.stubs.StubTree) ArrayList(java.util.ArrayList) Stub(com.intellij.psi.stubs.Stub) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with StubTree

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

use of com.intellij.psi.stubs.StubTree in project smali by JesusFreke.

the class LightCodeInsightParsingTestCase method doTest.

protected void doTest(boolean checkResult) {
    String name = getTestName(false);
    try {
        String text = loadFile(name + "." + myFileExt);
        PsiFile f = createPsiFile(name, text);
        if (f instanceof PsiFileImpl) {
            // Also want to test stub serialization/deserialization
            StubTree stubTree = ((PsiFileImpl) f).calcStubTree();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            SerializationManagerImpl.getInstanceEx().serialize(stubTree.getRoot(), baos);
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            SerializationManagerImpl.getInstanceEx().deserialize(bais);
        }
        ensureParsed(f);
        assertEquals("light virtual file text mismatch", text, ((LightVirtualFile) f.getVirtualFile()).getContent().toString());
        assertEquals("virtual file text mismatch", text, LoadTextUtil.loadText(f.getVirtualFile()));
        assertEquals("doc text mismatch", text, f.getViewProvider().getDocument().getText());
        assertEquals("psi text mismatch", text, f.getText());
        if (checkResult) {
            checkResult(name, f);
        } else {
            toParseTreeText(f, skipSpaces(), includeRanges());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (SerializerNotFoundException e) {
        throw new RuntimeException(e);
    }
}
Also used : StubTree(com.intellij.psi.stubs.StubTree) ByteArrayInputStream(java.io.ByteArrayInputStream) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) SerializerNotFoundException(com.intellij.psi.stubs.SerializerNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with StubTree

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

the class JavaCompilerElementRetriever method retrieveFunExpressionsByIndices.

@NotNull
static PsiFunctionalExpression[] retrieveFunExpressionsByIndices(@NotNull TIntHashSet indices, @NotNull PsiFileWithStubSupport psiFile) {
    StubTree tree = psiFile.getStubTree();
    boolean foreign = tree == null;
    if (foreign) {
        tree = ((PsiFileImpl) psiFile).calcStubTree();
    }
    PsiFunctionalExpression[] result = new PsiFunctionalExpression[indices.size()];
    int resIdx = 0;
    int funExprIdx = 0;
    for (StubElement<?> element : tree.getPlainList()) {
        if (FUN_EXPR.contains(element.getStubType()) && indices.contains(funExprIdx++)) {
            result[resIdx++] = (PsiFunctionalExpression) element.getPsi();
        }
    }
    if (result.length != resIdx) {
        final CompilerReferenceServiceImpl compilerReferenceService = (CompilerReferenceServiceImpl) CompilerReferenceService.getInstance(psiFile.getProject());
        final Set<Module> state = compilerReferenceService.getDirtyScopeHolder().getAllDirtyModules();
        final VirtualFile file = psiFile.getVirtualFile();
        final Module moduleForFile = ProjectFileIndex.getInstance(psiFile.getProject()).getModuleForFile(file);
        LOG.error("Compiler functional expression index doesn't match to stub index.\n" + "Functional expression indices: " + indices + "\n" + "Does the file belong to dirty scope?: " + state.contains(moduleForFile), new Attachment(psiFile.getName(), psiFile.getText()));
        return ContainerUtil.filter(result, Objects::nonNull).toArray(PsiFunctionalExpression.EMPTY_ARRAY);
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StubTree(com.intellij.psi.stubs.StubTree) PsiFunctionalExpression(com.intellij.psi.PsiFunctionalExpression) Attachment(com.intellij.openapi.diagnostic.Attachment) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with StubTree

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

the class JavaCompilerElementRetriever method retrieveMatchedClasses.

private static List<PsiClass> retrieveMatchedClasses(PsiFileWithStubSupport psiFile, Collection<InternalClassMatcher> matchers) {
    final List<PsiClass> result = new ArrayList<>(matchers.size());
    StubTree tree = psiFile.getStubTree();
    boolean foreign = tree == null;
    if (foreign) {
        tree = ((PsiFileImpl) psiFile).calcStubTree();
    }
    for (StubElement<?> element : tree.getPlainList()) {
        if (element instanceof PsiClassStub && match((PsiClassStub) element, matchers)) {
            result.add(((StubBase<PsiClass>) element).getPsi());
        }
    }
    return result;
}
Also used : StubTree(com.intellij.psi.stubs.StubTree) PsiClass(com.intellij.psi.PsiClass) PsiClassStub(com.intellij.psi.impl.java.stubs.PsiClassStub)

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