Search in sources :

Example 1 with StubBuilder

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

the class StubsTest method doTest.

public void doTest() throws Exception {
    final List<String> data = TestUtils.readInput(getTestDataPath() + "/" + getTestName(true) + ".test");
    String fileText = data.get(0);
    PsiFile psiFile = TestUtils.createPseudoPhysicalGroovyFile(getProject(), fileText);
    ASTNode node = psiFile.getNode();
    Assert.assertNotNull(node);
    IElementType type = node.getElementType();
    Assert.assertTrue(type instanceof IStubFileElementType);
    IStubFileElementType stubFileType = (IStubFileElementType) type;
    StubBuilder builder = stubFileType.getBuilder();
    StubElement element = builder.buildStubTree(psiFile);
    StringBuffer buffer = new StringBuffer();
    getStubsTreeImpl(element, buffer, "");
    String stubTree = buffer.toString().trim();
    assertEquals(data.get(1), stubTree);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) StubElement(com.intellij.psi.stubs.StubElement) StubBuilder(com.intellij.psi.StubBuilder)

Example 2 with StubBuilder

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

the class StubTreeBuilder method buildStubTree.

@Nullable
public static Stub buildStubTree(final FileContent inputData) {
    Stub data = inputData.getUserData(stubElementKey);
    if (data != null)
        return data;
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (inputData) {
        data = inputData.getUserData(stubElementKey);
        if (data != null)
            return data;
        final FileType fileType = inputData.getFileType();
        final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
        if (builder != null) {
            data = builder.buildStubTree(inputData);
            if (data instanceof PsiFileStubImpl && !((PsiFileStubImpl) data).rootsAreSet()) {
                ((PsiFileStubImpl) data).setStubRoots(new PsiFileStub[] { (PsiFileStubImpl) data });
            }
        } else {
            CharSequence contentAsText = inputData.getContentAsText();
            FileContentImpl fileContent = (FileContentImpl) inputData;
            PsiFile psi = fileContent.getPsiFileForPsiDependentIndex();
            final FileViewProvider viewProvider = psi.getViewProvider();
            psi = viewProvider.getStubBindingRoot();
            psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, contentAsText);
            // if we load AST, it should be easily gc-able. See PsiFileImpl.createTreeElementPointer()
            psi.getManager().startBatchFilesProcessingMode();
            try {
                IStubFileElementType stubFileElementType = ((PsiFileImpl) psi).getElementTypeForStubBuilder();
                if (stubFileElementType != null) {
                    final StubBuilder stubBuilder = stubFileElementType.getBuilder();
                    if (stubBuilder instanceof LightStubBuilder) {
                        LightStubBuilder.FORCED_AST.set(fileContent.getLighterASTForPsiDependentIndex());
                    }
                    data = stubBuilder.buildStubTree(psi);
                    final List<Pair<IStubFileElementType, PsiFile>> stubbedRoots = getStubbedRoots(viewProvider);
                    final List<PsiFileStub> stubs = new ArrayList<>(stubbedRoots.size());
                    stubs.add((PsiFileStub) data);
                    for (Pair<IStubFileElementType, PsiFile> stubbedRoot : stubbedRoots) {
                        final PsiFile secondaryPsi = stubbedRoot.second;
                        if (psi == secondaryPsi)
                            continue;
                        final StubBuilder stubbedRootBuilder = stubbedRoot.first.getBuilder();
                        if (stubbedRootBuilder instanceof LightStubBuilder) {
                            LightStubBuilder.FORCED_AST.set(new TreeBackedLighterAST(secondaryPsi.getNode()));
                        }
                        final StubElement element = stubbedRootBuilder.buildStubTree(secondaryPsi);
                        if (element instanceof PsiFileStub) {
                            stubs.add((PsiFileStub) element);
                        }
                    }
                    final PsiFileStub[] stubsArray = stubs.toArray(new PsiFileStub[stubs.size()]);
                    for (PsiFileStub stub : stubsArray) {
                        if (stub instanceof PsiFileStubImpl) {
                            ((PsiFileStubImpl) stub).setStubRoots(stubsArray);
                        }
                    }
                }
            } finally {
                psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, null);
                psi.getManager().finishBatchFilesProcessingMode();
            }
        }
        inputData.putUserData(stubElementKey, data);
        return data;
    }
}
Also used : IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) ArrayList(java.util.ArrayList) StubBuilder(com.intellij.psi.StubBuilder) FileViewProvider(com.intellij.psi.FileViewProvider) FileType(com.intellij.openapi.fileTypes.FileType) TreeBackedLighterAST(com.intellij.lang.TreeBackedLighterAST) PsiFile(com.intellij.psi.PsiFile) FileContentImpl(com.intellij.util.indexing.FileContentImpl) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with StubBuilder

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

the class TreeUtil method bindStubsToTree.

public static void bindStubsToTree(@NotNull PsiFileImpl file, @NotNull StubTree stubTree, @NotNull FileElement tree) throws StubBindingException {
    final ListIterator<StubElement<?>> stubs = stubTree.getPlainList().listIterator();
    // skip file root stub
    stubs.next();
    final IStubFileElementType type = file.getElementTypeForStubBuilder();
    assert type != null;
    final StubBuilder builder = type.getBuilder();
    tree.acceptTree(new RecursiveTreeElementWalkingVisitor() {

        @Override
        protected void visitNode(TreeElement node) {
            CompositeElement parent = node.getTreeParent();
            if (parent != null && builder.skipChildProcessingWhenBuildingStubs(parent, node)) {
                return;
            }
            IElementType type = node.getElementType();
            if (type instanceof IStubElementType && ((IStubElementType) type).shouldCreateStub(node)) {
                final StubElement stub = stubs.hasNext() ? stubs.next() : null;
                if (stub == null || stub.getStubType() != type) {
                    throw new StubBindingException("stub:" + stub + ", AST:" + type);
                }
                StubBasedPsiElementBase psi = (StubBasedPsiElementBase) node.getPsi();
                //noinspection unchecked
                ((StubBase) stub).setPsi(psi);
                psi.setStubIndex(stubs.previousIndex());
            }
            super.visitNode(node);
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) StubElement(com.intellij.psi.stubs.StubElement) IStubElementType(com.intellij.psi.stubs.IStubElementType) StubBuilder(com.intellij.psi.StubBuilder) StubBasedPsiElementBase(com.intellij.extapi.psi.StubBasedPsiElementBase)

Aggregations

StubBuilder (com.intellij.psi.StubBuilder)3 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)3 PsiFile (com.intellij.psi.PsiFile)2 StubElement (com.intellij.psi.stubs.StubElement)2 IElementType (com.intellij.psi.tree.IElementType)2 StubBasedPsiElementBase (com.intellij.extapi.psi.StubBasedPsiElementBase)1 ASTNode (com.intellij.lang.ASTNode)1 TreeBackedLighterAST (com.intellij.lang.TreeBackedLighterAST)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Pair (com.intellij.openapi.util.Pair)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)1 IStubElementType (com.intellij.psi.stubs.IStubElementType)1 FileContentImpl (com.intellij.util.indexing.FileContentImpl)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1