Search in sources :

Example 1 with FileContentImpl

use of com.intellij.util.indexing.FileContentImpl in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoCodeInsightFixtureTestCase method buildStubTreeText.

protected static String buildStubTreeText(@NotNull Project project, @NotNull VirtualFile file, @NotNull String fileContent, boolean checkErrors) throws IOException {
    String path = file.getPath();
    PsiFile psi = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), file.getFileType(), fileContent);
    if (checkErrors) {
        assertFalse(path + " contains error elements", DebugUtil.psiToString(psi, true).contains("PsiErrorElement"));
    }
    String full = DebugUtil.stubTreeToString(GoFileElementType.INSTANCE.getBuilder().buildStubTree(psi));
    psi.putUserData(IndexingDataKeys.VIRTUAL_FILE, file);
    FileContentImpl content = new FileContentImpl(file, fileContent, file.getCharset());
    PsiFile psiFile = content.getPsiFile();
    String fast = DebugUtil.stubTreeToString(GoFileElementType.INSTANCE.getBuilder().buildStubTree(psiFile));
    if (!Comparing.strEqual(full, fast)) {
        System.err.println(path);
        UsefulTestCase.assertSameLines(full, fast);
    }
    return fast;
}
Also used : PsiFile(com.intellij.psi.PsiFile) FileContentImpl(com.intellij.util.indexing.FileContentImpl)

Example 2 with FileContentImpl

use of com.intellij.util.indexing.FileContentImpl in project intellij-community by JetBrains.

the class FrameworkDetectionProcessor method collectSuitableFiles.

private void collectSuitableFiles(@NotNull VirtualFile file) {
    try {
        VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                // Since this code is invoked from New Project Wizard it's very possible that VFS isn't loaded to memory yet, so we need to do it
                // manually, otherwise refresh will do nothing
                myProgressIndicator.checkCanceled();
                return true;
            }
        });
        file.refresh(false, true);
        VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                myProgressIndicator.checkCanceled();
                if (!myProcessedFiles.add(file)) {
                    return false;
                }
                if (!file.isDirectory()) {
                    final FileType fileType = file.getFileType();
                    if (myDetectorsByFileType.containsKey(fileType)) {
                        myProgressIndicator.setText2(file.getPresentableUrl());
                        try {
                            final FileContent fileContent = new FileContentImpl(file, file.contentsToByteArray(false));
                            for (FrameworkDetectorData detector : myDetectorsByFileType.get(fileType)) {
                                if (detector.myFilePattern.accepts(fileContent)) {
                                    detector.mySuitableFiles.add(file);
                                }
                            }
                        } catch (IOException e) {
                            LOG.info(e);
                        }
                    }
                }
                return true;
            }
        });
    } catch (ProcessCanceledException ignored) {
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileContent(com.intellij.util.indexing.FileContent) FileType(com.intellij.openapi.fileTypes.FileType) FileContentImpl(com.intellij.util.indexing.FileContentImpl) IOException(java.io.IOException) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 3 with FileContentImpl

use of com.intellij.util.indexing.FileContentImpl 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 4 with FileContentImpl

use of com.intellij.util.indexing.FileContentImpl in project intellij-community by JetBrains.

the class CoreStubTreeLoader method readOrBuild.

@Override
public ObjectStubTree readOrBuild(Project project, VirtualFile vFile, @Nullable PsiFile psiFile) {
    if (!canHaveStub(vFile)) {
        return null;
    }
    try {
        final FileContent fc = new FileContentImpl(vFile, vFile.contentsToByteArray());
        fc.putUserData(IndexingDataKeys.PROJECT, project);
        final Stub element = StubTreeBuilder.buildStubTree(fc);
        if (element instanceof PsiFileStub) {
            return new StubTree((PsiFileStub) element);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : FileContent(com.intellij.util.indexing.FileContent) FileContentImpl(com.intellij.util.indexing.FileContentImpl) IOException(java.io.IOException)

Example 5 with FileContentImpl

use of com.intellij.util.indexing.FileContentImpl in project intellij-community by JetBrains.

the class GrStubIndexer method indexFile.

@Nullable
@Override
public Unit indexFile(@NotNull FileContent content) {
    if (!isNormalGroovyFile(((FileContentImpl) content).getPsiFileForPsiDependentIndex()))
        return null;
    Stub stubTree = StubTreeBuilder.buildStubTree(content);
    if (!(stubTree instanceof GrFileStub))
        return null;
    GrFileStub grFileStub = (GrFileStub) stubTree;
    new StubTree(grFileStub, false);
    String pid = "";
    ArrayList<ClassDecl> classList = new ArrayList<>();
    Set<String> usedNames = new HashSet<>();
    for (StubElement<?> el : grFileStub.getChildrenStubs()) {
        if (el instanceof GrPackageDefinitionStub) {
            GrPackageDefinitionStub packageStub = (GrPackageDefinitionStub) el;
            String pkgName = packageStub.getPackageName();
            if (pkgName != null) {
                pid = id(pkgName, false, null);
            }
        }
        if (el instanceof GrTypeDefinitionStub) {
            ClassDecl classDecl = processClassDecl((GrTypeDefinitionStub) el, usedNames, false);
            if (classDecl != null) {
                classList.add(classDecl);
            }
        }
    }
    ArrayList<Import> importList = new ArrayList<>();
    for (StubElement<?> el : grFileStub.getChildrenStubs()) {
        if (el instanceof GrImportStatementStub) {
            processImport((GrImportStatementStub) el, importList, usedNames);
        }
    }
    ClassDecl[] classes = classList.isEmpty() ? ClassDecl.EMPTY_ARRAY : classList.toArray(new ClassDecl[classList.size()]);
    Import[] imports = importList.isEmpty() ? Import.EMPTY_ARRAY : importList.toArray(new Import[importList.size()]);
    return new Unit(pid, IndexTree.GROOVY, imports, classes);
}
Also used : StubTree(com.intellij.psi.stubs.StubTree) ArrayList(java.util.ArrayList) Stub(com.intellij.psi.stubs.Stub) FileContentImpl(com.intellij.util.indexing.FileContentImpl) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileContentImpl (com.intellij.util.indexing.FileContentImpl)5 FileType (com.intellij.openapi.fileTypes.FileType)2 PsiFile (com.intellij.psi.PsiFile)2 FileContent (com.intellij.util.indexing.FileContent)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 TreeBackedLighterAST (com.intellij.lang.TreeBackedLighterAST)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 VirtualFileVisitor (com.intellij.openapi.vfs.VirtualFileVisitor)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 StubBuilder (com.intellij.psi.StubBuilder)1 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)1 Stub (com.intellij.psi.stubs.Stub)1 StubTree (com.intellij.psi.stubs.StubTree)1 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)1 HashSet (java.util.HashSet)1