Search in sources :

Example 11 with Function

use of com.intellij.util.Function 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 12 with Function

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

the class AddMethodQualifierTest method doTest.

private void doTest(final String... candidatesNames) {
    myFixture.configureByFile(getTestName(false) + ".java");
    final AddMethodQualifierFix addMethodQualifierFix = getQuickFix();
    if (candidatesNames.length == 0) {
        assertNull(addMethodQualifierFix);
        return;
    }
    assertNotNull(addMethodQualifierFix);
    final Set<String> actualCandidatesNames = new TreeSet<>(ContainerUtil.map(addMethodQualifierFix.getCandidates(), new Function<PsiNamedElement, String>() {

        @Override
        public String fun(final PsiNamedElement psiNamedElement) {
            final String name = psiNamedElement.getName();
            assertNotNull(name);
            return name;
        }
    }));
    final Set<String> expectedCandidatesNames = new TreeSet<>(ContainerUtil.list(candidatesNames));
    assertEquals(expectedCandidatesNames, actualCandidatesNames);
}
Also used : AddMethodQualifierFix(com.intellij.codeInsight.daemon.impl.quickfix.AddMethodQualifierFix) Function(com.intellij.util.Function) PsiNamedElement(com.intellij.psi.PsiNamedElement)

Example 13 with Function

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

the class IncProjectBuilder method runBuildersForChunk.

private boolean runBuildersForChunk(final CompileContext context, final BuildTargetChunk chunk) throws ProjectBuildException, IOException {
    Set<? extends BuildTarget<?>> targets = chunk.getTargets();
    if (targets.size() > 1) {
        Set<ModuleBuildTarget> moduleTargets = new LinkedHashSet<>();
        for (BuildTarget<?> target : targets) {
            if (target instanceof ModuleBuildTarget) {
                moduleTargets.add((ModuleBuildTarget) target);
            } else {
                String targetsString = StringUtil.join(targets, (Function<BuildTarget<?>, String>) target1 -> StringUtil.decapitalize(target1.getPresentableName()), ", ");
                context.processMessage(new CompilerMessage("", BuildMessage.Kind.ERROR, "Cannot build " + StringUtil.decapitalize(target.getPresentableName()) + " because it is included into a circular dependency (" + targetsString + ")"));
                return false;
            }
        }
        return runModuleLevelBuilders(context, new ModuleChunk(moduleTargets));
    }
    final BuildTarget<?> target = targets.iterator().next();
    if (target instanceof ModuleBuildTarget) {
        return runModuleLevelBuilders(context, new ModuleChunk(Collections.singleton((ModuleBuildTarget) target)));
    }
    // In general the set of files corresponding to changed source file may be different
    // Need this for example, to keep up with case changes in file names  for case-insensitive OSes: 
    // deleting the output before copying is the only way to ensure the case of the output file's name is exactly the same as source file's case
    cleanOldOutputs(context, target);
    final List<TargetBuilder<?, ?>> builders = BuilderRegistry.getInstance().getTargetBuilders();
    final float builderProgressDelta = 1.0f / builders.size();
    for (TargetBuilder<?, ?> builder : builders) {
        buildTarget(target, context, builder);
        updateDoneFraction(context, builderProgressDelta);
    }
    return true;
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) BuildTargetConfiguration(org.jetbrains.jps.incremental.storage.BuildTargetConfiguration) ExternalJavacManager(org.jetbrains.jps.javac.ExternalJavacManager) THashSet(gnu.trove.THashSet) CanceledStatus(org.jetbrains.jps.api.CanceledStatus) CompilationRound(org.jetbrains.jps.incremental.fs.CompilationRound) THashMap(gnu.trove.THashMap) FilesDelta(org.jetbrains.jps.incremental.fs.FilesDelta) org.jetbrains.jps.incremental.messages(org.jetbrains.jps.incremental.messages) Predicate(com.intellij.util.containers.Predicate) TimingLog(org.jetbrains.jps.TimingLog) JavaSourceRootDescriptor(org.jetbrains.jps.builders.java.JavaSourceRootDescriptor) ProjectDescriptor(org.jetbrains.jps.cmdline.ProjectDescriptor) SmartList(com.intellij.util.SmartList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JpsJavaCompilerConfiguration(org.jetbrains.jps.model.java.compiler.JpsJavaCompilerConfiguration) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) BuildRunner(org.jetbrains.jps.cmdline.BuildRunner) MultiMap(com.intellij.util.containers.MultiMap) Method(java.lang.reflect.Method) BuildFSState(org.jetbrains.jps.incremental.fs.BuildFSState) ModuleChunk(org.jetbrains.jps.ModuleChunk) java.util.concurrent(java.util.concurrent) PersistentEnumerator(com.intellij.util.io.PersistentEnumerator) JpsModule(org.jetbrains.jps.model.module.JpsModule) DirtyFilesHolderBase(org.jetbrains.jps.builders.impl.DirtyFilesHolderBase) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.jetbrains.annotations.Nullable) Function(com.intellij.util.Function) NotNull(org.jetbrains.annotations.NotNull) ModuleExcludeIndex(org.jetbrains.jps.indices.ModuleExcludeIndex) OutputToTargetRegistry(org.jetbrains.jps.incremental.storage.OutputToTargetRegistry) java.util(java.util) Proxy(java.lang.reflect.Proxy) SharedThreadPool(org.jetbrains.jps.service.SharedThreadPool) SourceToOutputMapping(org.jetbrains.jps.builders.storage.SourceToOutputMapping) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProjectBuilderLogger(org.jetbrains.jps.builders.logging.ProjectBuilderLogger) AtomicReference(java.util.concurrent.atomic.AtomicReference) GlobalOptions(org.jetbrains.jps.api.GlobalOptions) BuildDataCorruptedException(org.jetbrains.jps.builders.storage.BuildDataCorruptedException) JpsJavaExtensionService(org.jetbrains.jps.model.java.JpsJavaExtensionService) BuildTargetChunk(org.jetbrains.jps.builders.impl.BuildTargetChunk) BoundedTaskExecutor(com.intellij.util.concurrency.BoundedTaskExecutor) OneToManyPathsMapping(org.jetbrains.jps.incremental.storage.OneToManyPathsMapping) StringUtil(com.intellij.openapi.util.text.StringUtil) IOException(java.io.IOException) File(java.io.File) JavaBuilderUtil(org.jetbrains.jps.builders.java.JavaBuilderUtil) org.jetbrains.jps.builders(org.jetbrains.jps.builders) AtomicLong(java.util.concurrent.atomic.AtomicLong) JavacMain(org.jetbrains.jps.javac.JavacMain) Callbacks(org.jetbrains.jps.builders.java.dependencyView.Callbacks) InvocationHandler(java.lang.reflect.InvocationHandler) MappingFailedException(com.intellij.util.io.MappingFailedException) BuildOutputConsumerImpl(org.jetbrains.jps.builders.impl.BuildOutputConsumerImpl) JpsPathUtil(org.jetbrains.jps.util.JpsPathUtil) ModuleChunk(org.jetbrains.jps.ModuleChunk)

Example 14 with Function

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

the class TreeTraverserTest method testTracingBfsLaziness.

public void testTracingBfsLaziness() {
    List<Integer> result = ContainerUtil.newArrayList();
    TreeTraversal.TracingIt<List<Integer>> it = TreeTraversal.TRACING_BFS.traversal((Function<List<Integer>, Iterable<List<Integer>>>) integers -> JBIterable.from(integers).skip(1).transform(WRAP_TO_LIST)).fun(ContainerUtil.newArrayList(1)).typedIterator();
    while (it.advance()) {
        Integer cur = it.current().get(0);
        result.add(cur);
        assertEquals(JBIterable.generate(cur, DIV_2).takeWhile(IS_POSITIVE).toList(), it.backtrace().transform(integers -> integers.get(0)).toList());
        if (cur > 4)
            continue;
        it.current().add(cur * 2);
        it.current().add(cur * 2);
    }
    assertEquals(JBIterable.of(1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8).toList(), result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) java.util(java.util) TextRange(com.intellij.openapi.util.TextRange) Conditions.not(com.intellij.openapi.util.Conditions.not) Comparing(com.intellij.openapi.util.Comparing) PairFunction(com.intellij.util.PairFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(com.intellij.util.Function) Processor(com.intellij.util.Processor) Conditions(com.intellij.openapi.util.Conditions) TestCase(junit.framework.TestCase) NotNull(org.jetbrains.annotations.NotNull) Functions(com.intellij.util.Functions) Split(com.intellij.util.containers.JBIterable.Split) Condition(com.intellij.openapi.util.Condition)

Example 15 with Function

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

the class IntelliSortChooserPopupAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    VcsLogUi logUI = e.getRequiredData(VcsLogDataKeys.VCS_LOG_UI);
    VcsLogUiProperties properties = e.getRequiredData(VcsLogInternalDataKeys.LOG_UI_PROPERTIES);
    ActionGroup settingsGroup = new DefaultActionGroup(ContainerUtil.map(PermanentGraph.SortType.values(), (Function<PermanentGraph.SortType, AnAction>) sortType -> new SelectIntelliSortTypeAction(logUI, properties, sortType)));
    ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(null, settingsGroup, e.getDataContext(), JBPopupFactory.ActionSelectionAid.MNEMONICS, true, ToolWindowContentUi.POPUP_PLACE);
    Component component = e.getInputEvent().getComponent();
    if (component instanceof ActionButtonComponent) {
        popup.showUnderneathOf(component);
    } else {
        popup.showInCenterOf(component);
    }
}
Also used : Function(com.intellij.util.Function) MainVcsLogUiProperties(com.intellij.vcs.log.impl.MainVcsLogUiProperties) VcsLogUiProperties(com.intellij.vcs.log.impl.VcsLogUiProperties) ListPopup(com.intellij.openapi.ui.popup.ListPopup) VcsLogUi(com.intellij.vcs.log.VcsLogUi) PermanentGraph(com.intellij.vcs.log.graph.PermanentGraph)

Aggregations

Function (com.intellij.util.Function)53 NotNull (org.jetbrains.annotations.NotNull)32 Nullable (org.jetbrains.annotations.Nullable)24 Project (com.intellij.openapi.project.Project)23 Logger (com.intellij.openapi.diagnostic.Logger)19 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ContainerUtil (com.intellij.util.containers.ContainerUtil)15 List (java.util.List)15 StringUtil (com.intellij.openapi.util.text.StringUtil)12 Module (com.intellij.openapi.module.Module)11 com.intellij.psi (com.intellij.psi)11 java.util (java.util)10 ArrayList (java.util.ArrayList)10 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)9 ApplicationManager (com.intellij.openapi.application.ApplicationManager)8 PsiElement (com.intellij.psi.PsiElement)7 File (java.io.File)7 Messages (com.intellij.openapi.ui.Messages)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)6 IOException (java.io.IOException)6