Search in sources :

Example 51 with Processor

use of com.intellij.util.Processor in project android by JetBrains.

the class ThemeAttributeResolverTest method createNewStyle.

public boolean createNewStyle(@NotNull final VirtualFile resourceDir, @NotNull final String newStyleName, @NotNull final String parentStyleName, @Nullable final String colorPrimaryValue, @NotNull final List<String> folders) {
    return new WriteCommandAction<Boolean>(getProject(), "Create new style " + newStyleName) {

        @Override
        protected void run(@NotNull Result<Boolean> result) {
            result.setResult(AndroidResourceUtil.createValueResource(getProject(), resourceDir, newStyleName, null, ResourceType.STYLE, "styles.xml", folders, new Processor<ResourceElement>() {

                @Override
                public boolean process(ResourceElement element) {
                    assert element instanceof Style;
                    final Style style = (Style) element;
                    style.getParentStyle().setStringValue(parentStyleName);
                    if (colorPrimaryValue != null) {
                        StyleItem styleItem = style.addItem();
                        styleItem.getName().setStringValue("colorPrimary");
                        styleItem.setStringValue(colorPrimaryValue);
                    }
                    return true;
                }
            }));
        }
    }.execute().getResultObject();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) Processor(com.intellij.util.Processor) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) StyleItem(org.jetbrains.android.dom.resources.StyleItem) Style(org.jetbrains.android.dom.resources.Style) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 52 with Processor

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

the class FlexResolveHelper method findClassByQName.

@Nullable
public PsiElement findClassByQName(final String link, final Project project, final String className, final GlobalSearchScope scope) {
    final Ref<JSClass> result = new Ref<>();
    final String expectedPackage = link.equals(className) ? "" : link.substring(0, link.length() - className.length() - 1);
    final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final PsiManager manager = PsiManager.getInstance(project);
    final Processor<VirtualFile> processor = file -> {
        VirtualFile rootForFile = projectFileIndex.getSourceRootForFile(file);
        if (rootForFile == null)
            return true;
        if (expectedPackage.equals(VfsUtilCore.getRelativePath(file.getParent(), rootForFile, '.'))) {
            PsiFile psiFile = manager.findFile(file);
            final JSClass clazz = psiFile instanceof XmlFile ? XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) psiFile) : null;
            if (clazz != null) {
                result.set(clazz);
                return false;
            }
        }
        return true;
    };
    Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, className + JavaScriptSupportLoader.MXML_FILE_EXTENSION_DOT, scope);
    ContainerUtil.process(files, processor);
    if (result.isNull()) {
        files = FilenameIndex.getVirtualFilesByName(project, className + JavaScriptSupportLoader.FXG_FILE_EXTENSION_DOT, scope);
        ContainerUtil.process(files, processor);
    }
    return result.get();
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) JavaScriptSupportLoader(com.intellij.lang.javascript.JavaScriptSupportLoader) XmlFile(com.intellij.psi.xml.XmlFile) JSChangeUtil(com.intellij.lang.javascript.psi.impl.JSChangeUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) FilenameIndex(com.intellij.psi.search.FilenameIndex) ContainerUtil(com.intellij.util.containers.ContainerUtil) ResolveProcessor(com.intellij.lang.javascript.psi.resolve.ResolveProcessor) DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex) Query(com.intellij.util.Query) Project(com.intellij.openapi.project.Project) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) JSResolveHelper(com.intellij.lang.javascript.flex.JSResolveHelper) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) CssString(com.intellij.psi.css.CssString) JSFile(com.intellij.lang.javascript.psi.JSFile) XmlTag(com.intellij.psi.xml.XmlTag) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) com.intellij.lang.javascript.psi.ecmal4(com.intellij.lang.javascript.psi.ecmal4) JSImportHandlingUtil(com.intellij.lang.javascript.psi.resolve.JSImportHandlingUtil) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) JSResolveUtil(com.intellij.lang.javascript.psi.resolve.JSResolveUtil) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JSPsiImplUtils(com.intellij.lang.javascript.psi.impl.JSPsiImplUtils) ImportUtils(com.intellij.lang.javascript.flex.ImportUtils) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) Processor(com.intellij.util.Processor) MxmlJSClassProvider(com.intellij.javascript.flex.mxml.MxmlJSClassProvider) com.intellij.psi(com.intellij.psi) Ref(com.intellij.openapi.util.Ref) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Ref(com.intellij.openapi.util.Ref) XmlFile(com.intellij.psi.xml.XmlFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) CssString(com.intellij.psi.css.CssString) Nullable(org.jetbrains.annotations.Nullable)

Example 53 with Processor

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

the class GotoActionItemProvider method processActions.

private boolean processActions(String pattern, boolean everywhere, Processor<MatchedValue> consumer, DataContext dataContext) {
    JBIterable<AnAction> actions;
    if (everywhere) {
        Set<String> ids = ((ActionManagerImpl) myActionManager).getActionIds();
        actions = JBIterable.from(ids).transform(myActionManager::getAction).filter(Condition.NOT_NULL);
    } else {
        actions = JBIterable.from(myModel.myActionGroups.keySet());
    }
    MinusculeMatcher matcher = NameUtil.buildMatcher("*" + pattern, NameUtil.MatchingCaseSensitivity.NONE);
    JBIterable<ActionWrapper> actionWrappers = actions.transform(action -> {
        MatchMode mode = myModel.actionMatches(pattern, matcher, action);
        if (mode == MatchMode.NONE)
            return null;
        return new ActionWrapper(action, myModel.myActionGroups.get(action), mode, dataContext);
    }).filter(Condition.NOT_NULL);
    return processItems(pattern, actionWrappers, consumer);
}
Also used : CollectConsumer(com.intellij.util.CollectConsumer) java.util(java.util) JBIterable(com.intellij.util.containers.JBIterable) ApplyIntentionAction(com.intellij.ide.actions.ApplyIntentionAction) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) ContainerUtil(com.intellij.util.containers.ContainerUtil) ActionFromOptionDescriptorProvider(com.intellij.ide.ui.search.ActionFromOptionDescriptorProvider) NameUtil(com.intellij.psi.codeStyle.NameUtil) SearchableOptionsRegistrarImpl(com.intellij.ide.ui.search.SearchableOptionsRegistrarImpl) Project(com.intellij.openapi.project.Project) SearchableOptionsRegistrar(com.intellij.ide.ui.search.SearchableOptionsRegistrar) Matcher(com.intellij.util.text.Matcher) DataManager(com.intellij.ide.DataManager) ProgressManager(com.intellij.openapi.progress.ProgressManager) StringUtil(com.intellij.openapi.util.text.StringUtil) NotNullLazyValue(com.intellij.openapi.util.NotNullLazyValue) ActionManagerImpl(com.intellij.openapi.actionSystem.impl.ActionManagerImpl) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GotoActionModel(com.intellij.ide.util.gotoByName.GotoActionModel) OptionDescription(com.intellij.ide.ui.search.OptionDescription) Processor(com.intellij.util.Processor) OptionsTopHitProvider(com.intellij.ide.ui.OptionsTopHitProvider) NotNull(org.jetbrains.annotations.NotNull) SearchTopHitProvider(com.intellij.ide.SearchTopHitProvider) Condition(com.intellij.openapi.util.Condition) ActionManagerImpl(com.intellij.openapi.actionSystem.impl.ActionManagerImpl) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher)

Example 54 with Processor

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

the class JobUtilTest method testTasksRunEvenWhenReadActionIsHardToGet_Performance.

public void testTasksRunEvenWhenReadActionIsHardToGet_Performance() throws ExecutionException, InterruptedException {
    AtomicInteger processorCalled = new AtomicInteger();
    final Processor<String> processor = s -> {
        busySleep(1);
        processorCalled.incrementAndGet();
        return true;
    };
    for (int i = 0; i < 10; /*0*/
    i++) {
        System.out.println("i = " + i);
        processorCalled.set(0);
        final ProgressIndicator indicator = new EmptyProgressIndicator();
        int N = 10000;
        Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(() -> {
            JobLauncher.getInstance().invokeConcurrentlyUnderProgress(Collections.nCopies(N, ""), indicator, true, false, processor);
            assertFalse(indicator.isCanceled());
        });
        for (int k = 0; k < 10000; k++) {
            ApplicationManager.getApplication().runWriteAction(() -> {
                busySleep(1);
            });
        }
        future.get();
        assertEquals(N, processorCalled.get());
    }
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) UIUtil(com.intellij.util.ui.UIUtil) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) java.util(java.util) PlatformTestUtil(com.intellij.testFramework.PlatformTestUtil) PlatformTestCase(com.intellij.testFramework.PlatformTestCase) java.util.concurrent(java.util.concurrent) AbstractProgressIndicatorBase(com.intellij.openapi.progress.util.AbstractProgressIndicatorBase) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ThreadDumper(com.intellij.diagnostic.ThreadDumper) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) BigDecimal(java.math.BigDecimal) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimeoutUtil(com.intellij.util.TimeoutUtil) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) javax.swing(javax.swing) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)

Example 55 with Processor

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

the class SafeDeleteJavaCallerChooser method isTheOnlyOneParameterUsage.

/**
   * @return parameter if it is used inside method only as argument in nodeMethod call at parameterIndex
   */
static PsiParameter isTheOnlyOneParameterUsage(PsiElement call, final int parameterIndex, final PsiMethod nodeMethod) {
    if (call instanceof PsiCallExpression) {
        final PsiExpressionList argumentList = ((PsiCallExpression) call).getArgumentList();
        if (argumentList != null) {
            final PsiExpression[] expressions = argumentList.getExpressions();
            if (expressions.length > parameterIndex) {
                final PsiExpression expression = PsiUtil.deparenthesizeExpression(expressions[parameterIndex]);
                if (expression != null) {
                    final Set<PsiParameter> paramRefs = new HashSet<>();
                    expression.accept(new JavaRecursiveElementWalkingVisitor() {

                        @Override
                        public void visitReferenceExpression(PsiReferenceExpression expression) {
                            super.visitReferenceExpression(expression);
                            final PsiElement resolve = expression.resolve();
                            if (resolve instanceof PsiParameter) {
                                paramRefs.add((PsiParameter) resolve);
                            }
                        }
                    });
                    final PsiParameter parameter = ContainerUtil.getFirstItem(paramRefs);
                    if (parameter != null && !parameter.isVarArgs()) {
                        final PsiElement scope = parameter.getDeclarationScope();
                        if (scope instanceof PsiMethod && ((PsiMethod) scope).findDeepestSuperMethods().length == 0 && OverridingMethodsSearch.search((PsiMethod) scope).findFirst() == null) {
                            final int scopeParamIdx = ((PsiMethod) scope).getParameterList().getParameterIndex(parameter);
                            final Ref<Boolean> ref = new Ref<>(false);
                            if (ReferencesSearch.search(parameter, new LocalSearchScope(scope)).forEach(new Processor<PsiReference>() {

                                @Override
                                public boolean process(PsiReference reference) {
                                    final PsiElement element = reference.getElement();
                                    if (element instanceof PsiReferenceExpression) {
                                        PsiCallExpression parent = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class);
                                        while (parent != null) {
                                            final PsiMethod resolved = parent.resolveMethod();
                                            if (scope.equals(resolved)) {
                                                if (usedInQualifier(element, parent, scopeParamIdx))
                                                    return false;
                                                return true;
                                            }
                                            if (nodeMethod.equals(resolved)) {
                                                if (usedInQualifier(element, parent, parameterIndex))
                                                    return false;
                                                ref.set(true);
                                                return true;
                                            }
                                            parent = PsiTreeUtil.getParentOfType(parent, PsiCallExpression.class, true);
                                        }
                                        return false;
                                    }
                                    return true;
                                }

                                private boolean usedInQualifier(PsiElement element, PsiCallExpression parent, int parameterIndex) {
                                    PsiExpression qualifier = null;
                                    if (parent instanceof PsiMethodCallExpression) {
                                        qualifier = ((PsiMethodCallExpression) parent).getMethodExpression();
                                    } else if (parent instanceof PsiNewExpression) {
                                        qualifier = ((PsiNewExpression) parent).getQualifier();
                                    }
                                    if (PsiTreeUtil.isAncestor(qualifier, element, true)) {
                                        return true;
                                    }
                                    final PsiExpressionList list = parent.getArgumentList();
                                    return list != null && !PsiTreeUtil.isAncestor(list.getExpressions()[parameterIndex], element, false);
                                }
                            }) && ref.get()) {
                                return parameter;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Processor(com.intellij.util.Processor) Ref(com.intellij.openapi.util.Ref) HashSet(java.util.HashSet)

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