Search in sources :

Example 1 with FileIndex

use of com.intellij.openapi.roots.FileIndex in project intellij-community by JetBrains.

the class FileIndexCompileScope method getFiles.

@NotNull
public VirtualFile[] getFiles(final FileType fileType, final boolean inSourceOnly) {
    final List<VirtualFile> files = new ArrayList<>();
    final FileIndex[] fileIndices = getFileIndices();
    for (final FileIndex fileIndex : fileIndices) {
        fileIndex.iterateContent(new CompilerContentIterator(fileType, fileIndex, inSourceOnly, files));
    }
    return VfsUtil.toVirtualFileArray(files);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileIndex(com.intellij.openapi.roots.FileIndex) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with FileIndex

use of com.intellij.openapi.roots.FileIndex in project intellij-community by JetBrains.

the class PackageChooserDialog method createTreeModel.

private void createTreeModel() {
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    final FileIndex fileIndex = myModule != null ? ModuleRootManager.getInstance(myModule).getFileIndex() : ProjectRootManager.getInstance(myProject).getFileIndex();
    fileIndex.iterateContent(new ContentIterator() {

        public boolean processFile(VirtualFile fileOrDir) {
            if (fileOrDir.isDirectory() && fileIndex.isUnderSourceRootOfType(fileOrDir, JavaModuleSourceRootTypes.SOURCES)) {
                final PsiDirectory psiDirectory = psiManager.findDirectory(fileOrDir);
                LOG.assertTrue(psiDirectory != null);
                PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
                if (aPackage != null) {
                    addPackage(aPackage);
                }
            }
            return true;
        }
    });
    TreeUtil.sort(myModel, (o1, o2) -> {
        DefaultMutableTreeNode n1 = (DefaultMutableTreeNode) o1;
        DefaultMutableTreeNode n2 = (DefaultMutableTreeNode) o2;
        PsiNamedElement element1 = (PsiNamedElement) n1.getUserObject();
        PsiNamedElement element2 = (PsiNamedElement) n2.getUserObject();
        return element1.getName().compareToIgnoreCase(element2.getName());
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileIndex(com.intellij.openapi.roots.FileIndex) ContentIterator(com.intellij.openapi.roots.ContentIterator) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 3 with FileIndex

use of com.intellij.openapi.roots.FileIndex in project intellij-community by JetBrains.

the class GlobalInspectionContextImpl method startIterateScopeInBackground.

@NotNull
private Future<?> startIterateScopeInBackground(@NotNull final AnalysisScope scope, @Nullable final Collection<VirtualFile> localScopeFiles, final boolean headlessEnvironment, @NotNull final BlockingQueue<PsiFile> outFilesToInspect, @NotNull final ProgressIndicator progressIndicator) {
    Task.Backgroundable task = new Task.Backgroundable(getProject(), "Scanning Files to Inspect") {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                final FileIndex fileIndex = ProjectRootManager.getInstance(getProject()).getFileIndex();
                scope.accept(file -> {
                    indicator.checkCanceled();
                    if (ProjectUtil.isProjectOrWorkspaceFile(file) || !fileIndex.isInContent(file))
                        return true;
                    PsiFile psiFile = ReadAction.compute(() -> {
                        if (getProject().isDisposed())
                            throw new ProcessCanceledException();
                        PsiFile psi = PsiManager.getInstance(getProject()).findFile(file);
                        Document document = psi == null ? null : shouldProcess(psi, headlessEnvironment, localScopeFiles);
                        if (document != null) {
                            return psi;
                        }
                        return null;
                    });
                    if (psiFile != null) {
                        try {
                            LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed());
                            outFilesToInspect.put(psiFile);
                        } catch (InterruptedException e) {
                            LOG.error(e);
                        }
                    }
                    indicator.checkCanceled();
                    return true;
                });
            } catch (ProcessCanceledException e) {
            // ignore, but put tombstone
            } finally {
                try {
                    outFilesToInspect.put(TOMBSTONE);
                } catch (InterruptedException e) {
                    LOG.error(e);
                }
            }
        }
    };
    return ((CoreProgressManager) ProgressManager.getInstance()).runProcessWithProgressAsynchronously(task, progressIndicator, null);
}
Also used : CoreProgressManager(com.intellij.openapi.progress.impl.CoreProgressManager) FileIndex(com.intellij.openapi.roots.FileIndex) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with FileIndex

use of com.intellij.openapi.roots.FileIndex in project intellij-community by JetBrains.

the class SaveProjectAsTemplateAction method saveProject.

public static void saveProject(final Project project, @NotNull Path zipFile, Module moduleToSave, final String description, boolean replaceParameters, final ProgressIndicator indicator, boolean shouldEscape) {
    final Map<String, String> parameters = computeParameters(project, replaceParameters);
    indicator.setText("Saving project...");
    ApplicationManager.getApplication().invokeAndWait(() -> WriteAction.run(project::save));
    indicator.setText("Processing project files...");
    ZipOutputStream stream = null;
    try {
        stream = new ZipOutputStream(PathKt.outputStream(zipFile));
        final VirtualFile dir = getDirectoryToSave(project, moduleToSave);
        List<LocalArchivedTemplate.RootDescription> roots = collectStructure(project, moduleToSave);
        LocalArchivedTemplate.RootDescription basePathRoot = findOrAddBaseRoot(roots, dir);
        writeFile(LocalArchivedTemplate.DESCRIPTION_PATH, description, project, basePathRoot.myRelativePath, stream, true);
        if (replaceParameters) {
            String text = getInputFieldsText(parameters);
            writeFile(LocalArchivedTemplate.TEMPLATE_DESCRIPTOR, text, project, basePathRoot.myRelativePath, stream, false);
        }
        String metaDescription = getTemplateMetaText(shouldEscape, roots);
        writeFile(LocalArchivedTemplate.META_TEMPLATE_DESCRIPTOR_PATH, metaDescription, project, basePathRoot.myRelativePath, stream, true);
        FileIndex index = moduleToSave == null ? ProjectRootManager.getInstance(project).getFileIndex() : ModuleRootManager.getInstance(moduleToSave).getFileIndex();
        final ZipOutputStream finalStream = stream;
        MyContentIterator iterator = new MyContentIterator(indicator, finalStream, project, parameters, shouldEscape);
        for (LocalArchivedTemplate.RootDescription root : roots) {
            String prefix = LocalArchivedTemplate.ROOT_FILE_NAME + root.myIndex;
            VirtualFile rootFile = root.myFile;
            iterator.setRootAndPrefix(rootFile, prefix);
            index.iterateContentUnderDirectory(rootFile, iterator);
        }
    } catch (ProcessCanceledException ex) {
    //ignore
    } catch (Exception ex) {
        LOG.error(ex);
        UIUtil.invokeLaterIfNeeded(() -> Messages.showErrorDialog(project, "Can't save project as template", "Internal Error"));
    } finally {
        StreamUtil.closeStream(stream);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileIndex(com.intellij.openapi.roots.FileIndex) ZipOutputStream(java.util.zip.ZipOutputStream) IOException(java.io.IOException)

Aggregations

FileIndex (com.intellij.openapi.roots.FileIndex)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 NotNull (org.jetbrains.annotations.NotNull)2 Document (com.intellij.openapi.editor.Document)1 CoreProgressManager (com.intellij.openapi.progress.impl.CoreProgressManager)1 ContentIterator (com.intellij.openapi.roots.ContentIterator)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1