Search in sources :

Example 1 with Predicate

use of com.intellij.util.containers.Predicate in project intellij-community by JetBrains.

the class LibraryEditingUtil method getNotAddedSuitableLibrariesCondition.

public static Predicate<Library> getNotAddedSuitableLibrariesCondition(final ModuleRootModel rootModel, final FacetsProvider facetsProvider) {
    final OrderEntry[] orderEntries = rootModel.getOrderEntries();
    final Set<Library> result = new HashSet<>(orderEntries.length);
    for (OrderEntry orderEntry : orderEntries) {
        if (orderEntry instanceof LibraryOrderEntry && orderEntry.isValid()) {
            final LibraryImpl library = (LibraryImpl) ((LibraryOrderEntry) orderEntry).getLibrary();
            if (library != null) {
                final Library source = library.getSource();
                result.add(source != null ? source : library);
            }
        }
    }
    return new Predicate<Library>() {

        @Override
        public boolean apply(Library library) {
            if (result.contains(library))
                return false;
            if (library instanceof LibraryImpl) {
                final Library source = ((LibraryImpl) library).getSource();
                if (source != null && result.contains(source))
                    return false;
            }
            PersistentLibraryKind<?> kind = ((LibraryEx) library).getKind();
            if (kind != null) {
                LibraryType type = LibraryType.findByKind(kind);
                if (type != null && !type.isSuitableModule(rootModel.getModule(), facetsProvider)) {
                    return false;
                }
            }
            return true;
        }
    };
}
Also used : OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) LibraryImpl(com.intellij.openapi.roots.impl.libraries.LibraryImpl) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Predicate(com.intellij.util.containers.Predicate)

Example 2 with Predicate

use of com.intellij.util.containers.Predicate in project intellij-community by JetBrains.

the class JavaClassInheritorsSearcher method getOrComputeSubClasses.

@NotNull
private static Iterable<PsiClass> getOrComputeSubClasses(@NotNull Project project, @NotNull PsiClass baseClass, @NotNull SearchScope searchScopeForNonPhysical) {
    ConcurrentMap<PsiClass, Iterable<PsiClass>> map = HighlightingCaches.getInstance(project).ALL_SUB_CLASSES;
    Iterable<PsiClass> cached = map.get(baseClass);
    if (cached == null) {
        // returns lazy collection of subclasses. Each call to next() leads to calculation of next batch of subclasses.
        Function<PsiAnchor, PsiClass> converter = anchor -> ReadAction.compute(() -> (PsiClass) anchor.retrieve());
        Predicate<PsiClass> applicableFilter = candidate -> !(candidate instanceof PsiAnonymousClass) && candidate != null && !candidate.hasModifierProperty(PsiModifier.FINAL);
        // for non-physical elements ignore the cache completely because non-physical elements created so often/unpredictably so I can't figure out when to clear caches in this case
        boolean isPhysical = ReadAction.compute(baseClass::isPhysical);
        SearchScope scopeToUse = isPhysical ? GlobalSearchScope.allScope(project) : searchScopeForNonPhysical;
        LazyConcurrentCollection.MoreElementsGenerator<PsiAnchor, PsiClass> generator = (candidate, processor) -> DirectClassInheritorsSearch.search(candidate, scopeToUse).forEach(subClass -> {
            ProgressManager.checkCanceled();
            PsiAnchor pointer = ReadAction.compute(() -> PsiAnchor.create(subClass));
            processor.consume(pointer);
            return true;
        });
        PsiAnchor seed = ReadAction.compute(() -> PsiAnchor.create(baseClass));
        // lazy collection: store underlying queue as PsiAnchors, generate new elements by running direct inheritors
        Iterable<PsiClass> computed = new LazyConcurrentCollection<>(seed, converter, applicableFilter, generator);
        // make sure concurrent calls of this method always return the same collection to avoid expensive duplicate work
        cached = isPhysical ? ConcurrencyUtil.cacheOrGet(map, baseClass, computed) : computed;
    }
    return cached;
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ConcurrencyUtil(com.intellij.util.ConcurrencyUtil) SearchScope(com.intellij.psi.search.SearchScope) ReadAction(com.intellij.openapi.application.ReadAction) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Predicate(com.intellij.util.containers.Predicate) QueryExecutorBase(com.intellij.openapi.application.QueryExecutorBase) ProgressIndicatorProvider(com.intellij.openapi.progress.ProgressIndicatorProvider) ConcurrentMap(java.util.concurrent.ConcurrentMap) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) Function(com.intellij.util.Function) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) AllClassesSearch(com.intellij.psi.search.searches.AllClassesSearch) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) NotNull(org.jetbrains.annotations.NotNull) DirectClassInheritorsSearch(com.intellij.psi.search.searches.DirectClassInheritorsSearch) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Predicate

use of com.intellij.util.containers.Predicate in project intellij-community by JetBrains.

the class IncProjectBuilder method clearOutputs.

private void clearOutputs(CompileContext context) throws ProjectBuildException {
    final long cleanStart = System.currentTimeMillis();
    final MultiMap<File, BuildTarget<?>> rootsToDelete = MultiMap.createSet();
    final Set<File> allSourceRoots = ContainerUtil.newTroveSet(FileUtil.FILE_HASHING_STRATEGY);
    final ProjectDescriptor projectDescriptor = context.getProjectDescriptor();
    final List<? extends BuildTarget<?>> allTargets = projectDescriptor.getBuildTargetIndex().getAllTargets();
    for (BuildTarget<?> target : allTargets) {
        if (target instanceof ModuleBasedTarget) {
            for (File file : target.getOutputRoots(context)) {
                rootsToDelete.putValue(file, target);
            }
        } else {
            if (context.getScope().isBuildForced(target)) {
                clearOutputFilesUninterruptibly(context, target);
            }
        }
    }
    final ModuleExcludeIndex moduleIndex = projectDescriptor.getModuleExcludeIndex();
    for (BuildTarget<?> target : allTargets) {
        for (BuildRootDescriptor descriptor : projectDescriptor.getBuildRootIndex().getTargetRoots(target, context)) {
            // excluding from checks roots with generated sources; because it is safe to delete generated stuff
            if (!descriptor.isGenerated()) {
                File rootFile = descriptor.getRootFile();
                //However if a root isn't excluded it means that its content will be shown in 'Project View' and an user can create new files under it so it would be dangerous to clean such roots
                if (moduleIndex.isInContent(rootFile)) {
                    allSourceRoots.add(rootFile);
                }
            }
        }
    }
    // check that output and source roots are not overlapping
    final CompileScope compileScope = context.getScope();
    final List<File> filesToDelete = new ArrayList<>();
    final Predicate<BuildTarget<?>> forcedBuild = new Predicate<BuildTarget<?>>() {

        public boolean apply(BuildTarget<?> input) {
            return compileScope.isBuildForced(input);
        }
    };
    for (Map.Entry<File, Collection<BuildTarget<?>>> entry : rootsToDelete.entrySet()) {
        context.checkCanceled();
        final File outputRoot = entry.getKey();
        final Collection<BuildTarget<?>> rootTargets = entry.getValue();
        final Applicability applicability = Applicability.calculate(forcedBuild, rootTargets);
        if (applicability == Applicability.NONE) {
            continue;
        }
        boolean okToDelete = applicability == Applicability.ALL;
        if (okToDelete && !moduleIndex.isExcluded(outputRoot)) {
            // so in this case it is safe to delete such root
            if (JpsPathUtil.isUnder(allSourceRoots, outputRoot)) {
                okToDelete = false;
            } else {
                final Set<File> _outRoot = ContainerUtil.newTroveSet(FileUtil.FILE_HASHING_STRATEGY, outputRoot);
                for (File srcRoot : allSourceRoots) {
                    if (JpsPathUtil.isUnder(_outRoot, srcRoot)) {
                        okToDelete = false;
                        break;
                    }
                }
            }
        }
        if (okToDelete) {
            // do not delete output root itself to avoid lots of unnecessary "roots_changed" events in IDEA
            final File[] children = outputRoot.listFiles();
            if (children != null) {
                for (File child : children) {
                    if (!child.delete()) {
                        filesToDelete.add(child);
                    }
                }
            } else {
                // the output root must be file
                if (!outputRoot.delete()) {
                    filesToDelete.add(outputRoot);
                }
            }
            registerTargetsWithClearedOutput(context, rootTargets);
        } else {
            if (applicability == Applicability.ALL) {
                // only warn if unable to delete because of roots intersection
                context.processMessage(new CompilerMessage("", BuildMessage.Kind.WARNING, "Output path " + outputRoot.getPath() + " intersects with a source root. Only files that were created by build will be cleaned."));
            }
            context.processMessage(new ProgressMessage("Cleaning output directories..."));
            // clean only those files we are aware of
            for (BuildTarget<?> target : rootTargets) {
                if (compileScope.isBuildForced(target)) {
                    clearOutputFilesUninterruptibly(context, target);
                }
            }
        }
    }
    if (!filesToDelete.isEmpty()) {
        context.processMessage(new ProgressMessage("Cleaning output directories..."));
        if (SYNC_DELETE) {
            for (File file : filesToDelete) {
                context.checkCanceled();
                FileUtil.delete(file);
            }
        } else {
            myAsyncTasks.add(FileUtil.asyncDelete(filesToDelete));
        }
    }
    LOG.info("Cleaned output directories in " + (System.currentTimeMillis() - cleanStart) + " ms");
}
Also used : Predicate(com.intellij.util.containers.Predicate) ModuleExcludeIndex(org.jetbrains.jps.indices.ModuleExcludeIndex) ProjectDescriptor(org.jetbrains.jps.cmdline.ProjectDescriptor) File(java.io.File) THashMap(gnu.trove.THashMap) MultiMap(com.intellij.util.containers.MultiMap)

Aggregations

Predicate (com.intellij.util.containers.Predicate)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 QueryExecutorBase (com.intellij.openapi.application.QueryExecutorBase)1 ReadAction (com.intellij.openapi.application.ReadAction)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressIndicatorProvider (com.intellij.openapi.progress.ProgressIndicatorProvider)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Project (com.intellij.openapi.project.Project)1 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)1 OrderEntry (com.intellij.openapi.roots.OrderEntry)1 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)1 LibraryImpl (com.intellij.openapi.roots.impl.libraries.LibraryImpl)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 com.intellij.psi (com.intellij.psi)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 PsiSearchScopeUtil (com.intellij.psi.search.PsiSearchScopeUtil)1 SearchScope (com.intellij.psi.search.SearchScope)1 AllClassesSearch (com.intellij.psi.search.searches.AllClassesSearch)1 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)1