Search in sources :

Example 6 with Processor

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

the class MorphAction method actionPerformed.

protected void actionPerformed(final GuiEditor editor, final List<RadComponent> selection, final AnActionEvent e) {
    Processor<ComponentItem> processor = selectedValue -> {
        Runnable runnable = () -> {
            for (RadComponent c : selection) {
                if (!morphComponent(editor, c, selectedValue))
                    break;
            }
            editor.refreshAndSave(true);
        };
        CommandProcessor.getInstance().executeCommand(editor.getProject(), runnable, UIDesignerBundle.message("morph.component.command"), null);
        IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
            IdeFocusManager.getGlobalInstance().requestFocus(editor.getGlassLayer(), true);
        });
        return true;
    };
    PaletteListPopupStep step = new PaletteListPopupStep(editor, myLastMorphComponent, processor, UIDesignerBundle.message("morph.component.title"));
    step.hideNonAtomic();
    if (selection.size() == 1) {
        step.hideComponentClass(selection.get(0).getComponentClassName());
    }
    final ListPopup listPopup = JBPopupFactory.getInstance().createListPopup(step);
    FormEditingUtil.showPopupUnderComponent(listPopup, selection.get(0));
}
Also used : PsiType(com.intellij.psi.PsiType) UIDesignerBundle(com.intellij.uiDesigner.UIDesignerBundle) RadAtomicComponent(com.intellij.uiDesigner.radComponents.RadAtomicComponent) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) ChangeFieldTypeFix(com.intellij.uiDesigner.quickFixes.ChangeFieldTypeFix) ArrayList(java.util.ArrayList) GuiEditor(com.intellij.uiDesigner.designSurface.GuiEditor) Palette(com.intellij.uiDesigner.palette.Palette) PsiElementFactory(com.intellij.psi.PsiElementFactory) IProperty(com.intellij.uiDesigner.lw.IProperty) Logger(com.intellij.openapi.diagnostic.Logger) IntroComponentProperty(com.intellij.uiDesigner.propertyInspector.properties.IntroComponentProperty) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ComponentItem(com.intellij.uiDesigner.palette.ComponentItem) BindingProperty(com.intellij.uiDesigner.propertyInspector.properties.BindingProperty) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) FormEditingUtil(com.intellij.uiDesigner.FormEditingUtil) CommandProcessor(com.intellij.openapi.command.CommandProcessor) ListPopup(com.intellij.openapi.ui.popup.ListPopup) List(java.util.List) IComponent(com.intellij.uiDesigner.lw.IComponent) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) Processor(com.intellij.util.Processor) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) PsiField(com.intellij.psi.PsiField) RadContainer(com.intellij.uiDesigner.radComponents.RadContainer) NotNull(org.jetbrains.annotations.NotNull) InsertComponentProcessor(com.intellij.uiDesigner.designSurface.InsertComponentProcessor) FormInspectionUtil(com.intellij.uiDesigner.inspections.FormInspectionUtil) IntrospectedProperty(com.intellij.uiDesigner.propertyInspector.IntrospectedProperty) ComponentItem(com.intellij.uiDesigner.palette.ComponentItem) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) ListPopup(com.intellij.openapi.ui.popup.ListPopup)

Example 7 with Processor

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

the class PyiTypeProvider method getOverloads.

@NotNull
private static List<PyFunction> getOverloads(@NotNull PyFunction function, @NotNull final TypeEvalContext context) {
    final ScopeOwner owner = ScopeUtil.getScopeOwner(function);
    final String name = function.getName();
    final List<PyFunction> overloads = new ArrayList<>();
    final Processor<PyFunction> overloadsProcessor = f -> {
        if (name != null && name.equals(f.getName()) && isOverload(f, context)) {
            overloads.add(f);
        }
        return true;
    };
    if (owner instanceof PyClass) {
        final PyClass cls = (PyClass) owner;
        if (name != null) {
            cls.visitMethods(overloadsProcessor, false, context);
        }
    } else if (owner instanceof PyFile) {
        final PyFile file = (PyFile) owner;
        for (PyFunction f : file.getTopLevelFunctions()) {
            if (!overloadsProcessor.process(f)) {
                break;
            }
        }
    }
    return overloads;
}
Also used : Arrays(java.util.Arrays) ImmutableSet(com.google.common.collect.ImmutableSet) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) PyUtil.as(com.jetbrains.python.psi.PyUtil.as) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) com.jetbrains.python.psi(com.jetbrains.python.psi) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PyCallExpressionHelper(com.jetbrains.python.psi.impl.PyCallExpressionHelper) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Processor

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

the class JavaFxPsiUtil method computeInjectedControllerClass.

@Nullable
private static CachedValueProvider.Result<PsiClass> computeInjectedControllerClass(PsiFile containingFile) {
    return ourGuard.doPreventingRecursion(containingFile, true, () -> {
        final Project project = containingFile.getProject();
        final Ref<PsiClass> injectedController = new Ref<>();
        final PsiClass fxmlLoader = JavaPsiFacade.getInstance(project).findClass(JavaFxCommonNames.JAVAFX_FXML_FXMLLOADER, GlobalSearchScope.allScope(project));
        if (fxmlLoader != null) {
            final PsiMethod[] injectControllerMethods = fxmlLoader.findMethodsByName("setController", false);
            if (injectControllerMethods.length == 1) {
                final GlobalSearchScope globalSearchScope = GlobalSearchScope.getScopeRestrictedByFileTypes(containingFile.getResolveScope(), StdFileTypes.JAVA);
                final VirtualFile[] virtualFiles = CacheManager.SERVICE.getInstance(project).getVirtualFilesWithWord(ClassUtil.extractClassName(JavaFxCommonNames.JAVAFX_FXML_FXMLLOADER), UsageSearchContext.IN_CODE, globalSearchScope, true);
                if (virtualFiles.length == 0) {
                    return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
                }
                final GlobalSearchScope filesScope = GlobalSearchScope.filesScope(project, Arrays.asList(virtualFiles));
                final Processor<PsiReference> processor = loaderReference -> findControllerClassInjection(loaderReference, injectedController, injectControllerMethods[0]);
                ReferencesSearch.search(containingFile, filesScope).forEach(reference -> {
                    final PsiElement element = reference.getElement();
                    if (element instanceof PsiLiteralExpression) {
                        final PsiNewExpression expression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class);
                        if (expression != null) {
                            final PsiType type = expression.getType();
                            if (type != null && type.equalsToText(JavaFxCommonNames.JAVAFX_FXML_FXMLLOADER)) {
                                final PsiElement parent = expression.getParent();
                                if (parent instanceof PsiLocalVariable) {
                                    ReferencesSearch.search(parent).forEach(processor);
                                    final PsiClass controller = injectedController.get();
                                    if (controller != null) {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                    return true;
                });
            }
        }
        return new CachedValueProvider.Result<>(injectedController.get(), PsiModificationTracker.MODIFICATION_COUNT);
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) THashSet(gnu.trove.THashSet) XMLLanguage(com.intellij.lang.xml.XMLLanguage) JavaGenericsUtil(com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil) THashMap(gnu.trove.THashMap) com.intellij.psi.xml(com.intellij.psi.xml) JavaFxControllerClassIndex(org.jetbrains.plugins.javaFX.indexing.JavaFxControllerClassIndex) Validator(com.intellij.codeInsight.daemon.Validator) CacheManager(com.intellij.psi.impl.cache.CacheManager) JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) BiConsumer(java.util.function.BiConsumer) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.psi.util(com.intellij.psi.util) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JavaFxClassTagDescriptorBase(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxClassTagDescriptorBase) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) Collectors(java.util.stream.Collectors) UsageSearchContext(com.intellij.psi.search.UsageSearchContext) Consumer(java.util.function.Consumer) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) HighlightUtil(com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil) Processor(com.intellij.util.Processor) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) Registry(com.intellij.openapi.util.registry.Registry) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) NotNull(org.jetbrains.annotations.NotNull) PostprocessReformattingAspect(com.intellij.psi.impl.source.PostprocessReformattingAspect) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with Processor

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

the class PsiSearchHelperImpl method isCheapEnoughToSearch.

@NotNull
@Override
public SearchCostResult isCheapEnoughToSearch(@NotNull String name, @NotNull final GlobalSearchScope scope, @Nullable final PsiFile fileToIgnoreOccurrencesIn, @Nullable final ProgressIndicator progress) {
    final AtomicInteger count = new AtomicInteger();
    final ProgressIndicator indicator = progress == null ? new EmptyProgressIndicator() : progress;
    final Processor<VirtualFile> processor = new Processor<VirtualFile>() {

        private final VirtualFile virtualFileToIgnoreOccurrencesIn = fileToIgnoreOccurrencesIn == null ? null : fileToIgnoreOccurrencesIn.getVirtualFile();

        @Override
        public boolean process(VirtualFile file) {
            indicator.checkCanceled();
            if (Comparing.equal(file, virtualFileToIgnoreOccurrencesIn))
                return true;
            final int value = count.incrementAndGet();
            return value < 10;
        }
    };
    List<IdIndexEntry> keys = getWordEntries(name, true);
    boolean cheap = keys.isEmpty() || processFilesContainingAllKeys(myManager.getProject(), scope, null, keys, processor);
    if (!cheap) {
        return SearchCostResult.TOO_MANY_OCCURRENCES;
    }
    return count.get() == 0 ? SearchCostResult.ZERO_OCCURRENCES : SearchCostResult.FEW_OCCURRENCES;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) Processor(com.intellij.util.Processor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Processor

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

the class PsiSearchHelperImpl method processFilesConcurrentlyDespiteWriteActions.

// Tries to run {@code localProcessor} for each file in {@code files} concurrently on ForkJoinPool.
// When encounters write action request, stops all threads, waits for write action to finish and re-starts all threads again.
// {@localProcessor} must be as idempotent as possible.
public static boolean processFilesConcurrentlyDespiteWriteActions(@NotNull Project project, @NotNull List<VirtualFile> files, @NotNull final ProgressIndicator progress, @NotNull final Processor<VirtualFile> localProcessor) {
    ApplicationEx app = (ApplicationEx) ApplicationManager.getApplication();
    final AtomicBoolean canceled = new AtomicBoolean(false);
    while (true) {
        List<VirtualFile> failedList = new SmartList<>();
        final List<VirtualFile> failedFiles = Collections.synchronizedList(failedList);
        final Processor<VirtualFile> processor = vfile -> {
            try {
                boolean result = localProcessor.process(vfile);
                if (!result) {
                    canceled.set(true);
                }
                return result;
            } catch (ApplicationUtil.CannotRunReadActionException action) {
                failedFiles.add(vfile);
            }
            return !canceled.get();
        };
        boolean completed;
        if (app.isWriteAccessAllowed() || app.isReadAccessAllowed() && app.isWriteActionPending()) {
            // no point in processing in separate threads - they are doomed to fail to obtain read action anyway
            completed = ContainerUtil.process(files, processor);
        } else if (app.isWriteActionPending()) {
            completed = true;
            // we don't have read action now so wait for write action to complete
            failedFiles.addAll(files);
        } else {
            // try to run parallel read actions but fail as soon as possible
            completed = JobLauncher.getInstance().invokeConcurrentlyUnderProgress(files, progress, false, true, processor);
        }
        if (!completed) {
            return false;
        }
        if (failedFiles.isEmpty()) {
            break;
        }
        // we failed to run read action in job launcher thread
        // run read action in our thread instead to wait for a write action to complete and resume parallel processing
        DumbService.getInstance(project).runReadActionInSmartMode(EmptyRunnable.getInstance());
        files = failedList;
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) CommentUtilCore(com.intellij.util.codeInsight.CommentUtilCore) JobLauncher(com.intellij.concurrency.JobLauncher) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ApplicationUtil(com.intellij.openapi.application.ex.ApplicationUtil) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UsageInfo(com.intellij.usageView.UsageInfo) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) ReadAction(com.intellij.openapi.application.ReadAction) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) StringSearcher(com.intellij.util.text.StringSearcher) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) TooManyUsagesStatus(com.intellij.openapi.progress.util.TooManyUsagesStatus) SmartList(com.intellij.util.SmartList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(com.intellij.psi.impl.cache.CacheManager) Project(com.intellij.openapi.project.Project) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) DumbService(com.intellij.openapi.project.DumbService) StringUtil(com.intellij.openapi.util.text.StringUtil) Processors(com.intellij.util.Processors) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) ProgressIndicatorProvider(com.intellij.openapi.progress.ProgressIndicatorProvider) ExtensionPointName(com.intellij.openapi.extensions.ExtensionPointName) AsyncFuture(com.intellij.concurrency.AsyncFuture) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) com.intellij.psi.search(com.intellij.psi.search) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IdIndex(com.intellij.psi.impl.cache.impl.id.IdIndex) com.intellij.psi(com.intellij.psi) UsageInfoFactory(com.intellij.usageView.UsageInfoFactory) AsyncUtil(com.intellij.concurrency.AsyncUtil) NotNull(org.jetbrains.annotations.NotNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) SmartList(com.intellij.util.SmartList)

Aggregations

Processor (com.intellij.util.Processor)83 NotNull (org.jetbrains.annotations.NotNull)65 Project (com.intellij.openapi.project.Project)49 Nullable (org.jetbrains.annotations.Nullable)49 ContainerUtil (com.intellij.util.containers.ContainerUtil)42 com.intellij.psi (com.intellij.psi)31 List (java.util.List)28 ApplicationManager (com.intellij.openapi.application.ApplicationManager)25 StringUtil (com.intellij.openapi.util.text.StringUtil)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)25 java.util (java.util)25 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)24 ProgressManager (com.intellij.openapi.progress.ProgressManager)21 Logger (com.intellij.openapi.diagnostic.Logger)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)20 NonNls (org.jetbrains.annotations.NonNls)18 Ref (com.intellij.openapi.util.Ref)16 Collection (java.util.Collection)16 SmartList (com.intellij.util.SmartList)14 Document (com.intellij.openapi.editor.Document)13