Search in sources :

Example 1 with OrderEnumerator

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

the class SearchScope method iterateContent.

public void iterateContent(@NotNull final Project project, final Processor<VirtualFile> processor) {
    switch(getScopeType()) {
        case PROJECT:
            //noinspection unchecked
            ProjectRootManager.getInstance(project).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
            break;
        case MODULE:
            final Module module = ModuleManager.getInstance(project).findModuleByName(getModuleName());
            assert module != null;
            ModuleRootManager.getInstance(module).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
            break;
        case DIRECTORY:
            final String dirName = getPath();
            assert dirName != null;
            final VirtualFile virtualFile = findFile(dirName);
            if (virtualFile != null) {
                iterateRecursively(virtualFile, processor, isRecursive());
            }
            break;
        case CUSTOM:
            assert myCustomScope != null;
            final ContentIterator iterator;
            if (myCustomScope instanceof GlobalSearchScope) {
                final GlobalSearchScope searchScope = (GlobalSearchScope) myCustomScope;
                iterator = new MyFileIterator(processor, virtualFile13 -> searchScope.contains(virtualFile13));
                if (searchScope.isSearchInLibraries()) {
                    final OrderEnumerator enumerator = OrderEnumerator.orderEntries(project).withoutModuleSourceEntries().withoutDepModules();
                    final Collection<VirtualFile> libraryFiles = new THashSet<>();
                    Collections.addAll(libraryFiles, enumerator.getClassesRoots());
                    Collections.addAll(libraryFiles, enumerator.getSourceRoots());
                    final Processor<VirtualFile> adapter = virtualFile1 -> iterator.processFile(virtualFile1);
                    for (final VirtualFile file : libraryFiles) {
                        iterateRecursively(file, adapter, true);
                    }
                }
            } else {
                final PsiManager manager = PsiManager.getInstance(project);
                iterator = new MyFileIterator(processor, virtualFile12 -> {
                    final PsiFile element = manager.findFile(virtualFile12);
                    return element != null && PsiSearchScopeUtil.isInScope(myCustomScope, element);
                });
            }
            ProjectRootManager.getInstance(project).getFileIndex().iterateContent(iterator);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Tag(com.intellij.util.xmlb.annotations.Tag) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashSet(gnu.trove.THashSet) PsiManager(com.intellij.psi.PsiManager) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Conditions(com.intellij.openapi.util.Conditions) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) ContentIterator(com.intellij.openapi.roots.ContentIterator) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Nullable(org.jetbrains.annotations.Nullable) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) Processor(com.intellij.util.Processor) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) Attribute(com.intellij.util.xmlb.annotations.Attribute) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Condition(com.intellij.openapi.util.Condition) ContentIterator(com.intellij.openapi.roots.ContentIterator) PsiManager(com.intellij.psi.PsiManager) THashSet(gnu.trove.THashSet) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 2 with OrderEnumerator

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

the class JarFromModulesTemplate method doCreateArtifact.

@Nullable
public NewArtifactConfiguration doCreateArtifact(final Module[] modules, final String mainClassName, final String directoryForManifest, final boolean extractLibrariesToJar, final boolean includeTests) {
    VirtualFile manifestFile = null;
    final Project project = myContext.getProject();
    if (mainClassName != null && !mainClassName.isEmpty() || !extractLibrariesToJar) {
        final VirtualFile directory;
        try {
            directory = ApplicationManager.getApplication().runWriteAction(new ThrowableComputable<VirtualFile, IOException>() {

                @Override
                public VirtualFile compute() throws IOException {
                    return VfsUtil.createDirectoryIfMissing(directoryForManifest);
                }
            });
        } catch (IOException e) {
            LOG.info(e);
            Messages.showErrorDialog(project, "Cannot create directory '" + directoryForManifest + "': " + e.getMessage(), CommonBundle.getErrorTitle());
            return null;
        }
        if (directory == null)
            return null;
        manifestFile = ManifestFileUtil.createManifestFile(directory, project);
        if (manifestFile == null) {
            return null;
        }
        ManifestFileUtil.updateManifest(manifestFile, mainClassName, null, true);
    }
    String name = modules.length == 1 ? modules[0].getName() : project.getName();
    final PackagingElementFactory factory = PackagingElementFactory.getInstance();
    final CompositePackagingElement<?> archive = factory.createArchive(ArtifactUtil.suggestArtifactFileName(name) + ".jar");
    OrderEnumerator orderEnumerator = ProjectRootManager.getInstance(project).orderEntries(Arrays.asList(modules));
    final Set<Library> libraries = new THashSet<>();
    if (!includeTests) {
        orderEnumerator = orderEnumerator.productionOnly();
    }
    final ModulesProvider modulesProvider = myContext.getModulesProvider();
    final OrderEnumerator enumerator = orderEnumerator.using(modulesProvider).withoutSdk().runtimeOnly().recursively();
    enumerator.forEachLibrary(new CommonProcessors.CollectProcessor<>(libraries));
    enumerator.forEachModule(module -> {
        if (ProductionModuleOutputElementType.ELEMENT_TYPE.isSuitableModule(modulesProvider, module)) {
            archive.addOrFindChild(factory.createModuleOutput(module));
        }
        if (includeTests && TestModuleOutputElementType.ELEMENT_TYPE.isSuitableModule(modulesProvider, module)) {
            archive.addOrFindChild(factory.createTestModuleOutput(module));
        }
        return true;
    });
    final JarArtifactType jarArtifactType = JarArtifactType.getInstance();
    if (manifestFile != null && !manifestFile.equals(ManifestFileUtil.findManifestFile(archive, myContext, jarArtifactType))) {
        archive.addFirstChild(factory.createFileCopyWithParentDirectories(manifestFile.getPath(), ManifestFileUtil.MANIFEST_DIR_NAME));
    }
    final String artifactName = name + ":jar";
    if (extractLibrariesToJar) {
        addExtractedLibrariesToJar(archive, factory, libraries);
        return new NewArtifactConfiguration(archive, artifactName, jarArtifactType);
    } else {
        final ArtifactRootElement<?> root = factory.createArtifactRootElement();
        List<String> classpath = new ArrayList<>();
        root.addOrFindChild(archive);
        addLibraries(libraries, root, archive, classpath);
        ManifestFileUtil.updateManifest(manifestFile, mainClassName, classpath, true);
        return new NewArtifactConfiguration(root, artifactName, PlainArtifactType.getInstance());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) ThrowableComputable(com.intellij.openapi.util.ThrowableComputable) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Library(com.intellij.openapi.roots.libraries.Library) CommonProcessors(com.intellij.util.CommonProcessors) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with OrderEnumerator

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

the class VfsRootAccess method getAllRoots.

@NotNull
private static VirtualFile[] getAllRoots(@NotNull Project project) {
    insideGettingRoots = true;
    final Set<VirtualFile> roots = new THashSet<>();
    final OrderEnumerator enumerator = ProjectRootManager.getInstance(project).orderEntries();
    ContainerUtil.addAll(roots, enumerator.getClassesRoots());
    ContainerUtil.addAll(roots, enumerator.getSourceRoots());
    insideGettingRoots = false;
    return VfsUtilCore.toVirtualFileArray(roots);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with OrderEnumerator

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

the class JspSpiUtil method processClassPathItems.

public static void processClassPathItems(final VirtualFile virtualFile, final Module module, final Consumer<VirtualFile> consumer, boolean includeModuleOutput) {
    if (isJarFile(virtualFile)) {
        consumer.consume(virtualFile);
    }
    if (module != null) {
        OrderEnumerator enumerator = ModuleRootManager.getInstance(module).orderEntries().recursively();
        if (!includeModuleOutput) {
            enumerator = enumerator.withoutModuleSourceEntries();
        }
        for (VirtualFile root : enumerator.getClassesRoots()) {
            final VirtualFile file;
            if (root.getFileSystem().getProtocol().equals(JarFileSystem.PROTOCOL)) {
                file = JarFileSystem.getInstance().getVirtualFileForJar(root);
            } else {
                file = root;
            }
            consumer.consume(file);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator)

Example 5 with OrderEnumerator

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

the class ManifestFileUtil method suggestManifestFileDirectory.

@Nullable
public static VirtualFile suggestManifestFileDirectory(@NotNull Project project, @Nullable Module module) {
    OrderEnumerator enumerator = module != null ? OrderEnumerator.orderEntries(module) : OrderEnumerator.orderEntries(project);
    final VirtualFile[] files = enumerator.withoutDepModules().withoutLibraries().withoutSdk().productionOnly().sources().getRoots();
    if (files.length > 0) {
        return files[0];
    }
    return suggestBaseDir(project, null);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

OrderEnumerator (com.intellij.openapi.roots.OrderEnumerator)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 Nullable (org.jetbrains.annotations.Nullable)4 Module (com.intellij.openapi.module.Module)3 Project (com.intellij.openapi.project.Project)3 THashSet (gnu.trove.THashSet)3 IOException (java.io.IOException)3 Sdk (com.intellij.openapi.projectRoots.Sdk)2 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)2 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 ExecutionException (com.intellij.execution.ExecutionException)1 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)1 ModuleManager (com.intellij.openapi.module.ModuleManager)1 JavaSdk (com.intellij.openapi.projectRoots.JavaSdk)1 JavaSdkType (com.intellij.openapi.projectRoots.JavaSdkType)1 JavaSdkVersion (com.intellij.openapi.projectRoots.JavaSdkVersion)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 ContentIterator (com.intellij.openapi.roots.ContentIterator)1 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)1