Search in sources :

Example 41 with Computable

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

the class ConvertParameterToMapEntryIntention method collectOwnerOccurrences.

private static boolean collectOwnerOccurrences(final Project project, final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
    final PsiElement namedElem = getReferencedElement(owner);
    if (namedElem == null)
        return true;
    final Ref<Boolean> result = new Ref<>(true);
    final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            final Collection<PsiReference> references = Collections.synchronizedSet(new HashSet<PsiReference>());
            final Processor<PsiReference> consumer = psiReference -> {
                references.add(psiReference);
                return true;
            };
            ReferencesSearch.search(namedElem).forEach(consumer);
            boolean isProperty = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    return namedElem instanceof GrField && ((GrField) namedElem).isProperty();
                }
            });
            if (isProperty) {
                final GrAccessorMethod[] getters = ApplicationManager.getApplication().runReadAction(new Computable<GrAccessorMethod[]>() {

                    @Override
                    public GrAccessorMethod[] compute() {
                        return ((GrField) namedElem).getGetters();
                    }
                });
                for (GrAccessorMethod getter : getters) {
                    MethodReferencesSearch.search(getter).forEach(consumer);
                }
            }
            for (final PsiReference reference : references) {
                ApplicationManager.getApplication().runReadAction(() -> {
                    final PsiElement element = reference.getElement();
                    if (element != null) {
                        occurrences.add(element);
                    }
                });
            }
        }

        @Override
        public void onCancel() {
            result.set(false);
        }

        @Override
        public void onThrowable(@NotNull Throwable error) {
            super.onThrowable(error);
            result.set(false);
        }

        @Override
        public void onSuccess() {
            result.set(true);
        }
    };
    ProgressManager.getInstance().run(task);
    return result.get().booleanValue();
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) HashSet(com.intellij.util.containers.HashSet) GrMethodImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.typedef.members.GrMethodImpl) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Task(com.intellij.openapi.progress.Task) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) Ref(com.intellij.openapi.util.Ref) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) GroovyIntentionsBundle(org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) StringUtil(com.intellij.openapi.util.text.StringUtil) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPropertyUtils(org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyValidationUtil(org.jetbrains.plugins.groovy.refactoring.GroovyValidationUtil) Collections(java.util.Collections) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) Task(com.intellij.openapi.progress.Task) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NotNull(org.jetbrains.annotations.NotNull) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 42 with Computable

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

the class GrAliasImportIntention method runTemplate.

private static void runTemplate(Project project, final GrImportStatement context, PsiMember resolved, final GroovyFileBase file, final List<UsageInfo> usages, GrImportStatement templateImport) {
    PostprocessReformattingAspect.getInstance(project).doPostponedFormatting();
    TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(templateImport);
    LinkedHashSet<String> names = getSuggestedNames(resolved, context);
    final PsiElement aliasNameElement = templateImport.getAliasNameElement();
    assert aliasNameElement != null;
    templateBuilder.replaceElement(aliasNameElement, new MyLookupExpression(resolved.getName(), names, (PsiNamedElement) resolved, resolved, true, null));
    Template built = templateBuilder.buildTemplate();
    final Editor newEditor = IntentionUtils.positionCursor(project, file, templateImport);
    final Document document = newEditor.getDocument();
    final RangeMarker contextImportPointer = document.createRangeMarker(context.getTextRange());
    final TextRange range = templateImport.getTextRange();
    document.deleteString(range.getStartOffset(), range.getEndOffset());
    final String name = resolved.getName();
    TemplateManager manager = TemplateManager.getInstance(project);
    manager.startTemplate(newEditor, built, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            final GrImportStatement importStatement = ApplicationManager.getApplication().runReadAction(new Computable<GrImportStatement>() {

                @Nullable
                @Override
                public GrImportStatement compute() {
                    return PsiTreeUtil.findElementOfClassAtOffset(file, range.getStartOffset(), GrImportStatement.class, true);
                }
            });
            if (brokenOff) {
                if (importStatement != null) {
                    ApplicationManager.getApplication().runWriteAction(() -> importStatement.delete());
                }
                return;
            }
            updateRefs(usages, name, importStatement);
            ApplicationManager.getApplication().runWriteAction(() -> {
                final GrImportStatement context1 = PsiTreeUtil.findElementOfClassAtRange(file, contextImportPointer.getStartOffset(), contextImportPointer.getEndOffset(), GrImportStatement.class);
                if (context1 != null) {
                    context1.delete();
                }
            });
        }
    });
}
Also used : MyLookupExpression(com.intellij.refactoring.rename.inplace.MyLookupExpression) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) Template(com.intellij.codeInsight.template.Template) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Editor(com.intellij.openapi.editor.Editor) Computable(com.intellij.openapi.util.Computable)

Example 43 with Computable

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

the class RerunFailedTestsAction method getRunProfile.

@Override
protected MyRunProfile getRunProfile(@NotNull ExecutionEnvironment environment) {
    final TestNGConfiguration configuration = (TestNGConfiguration) myConsoleProperties.getConfiguration();
    final List<AbstractTestProxy> failedTests = getFailedTests(configuration.getProject());
    return new MyRunProfile(configuration) {

        @Override
        @NotNull
        public Module[] getModules() {
            return configuration.getModules();
        }

        @Override
        public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment env) {
            return new TestNGRunnableState(env, configuration) {

                @Override
                public SearchingForTestsTask createSearchingForTestsTask() {
                    return new SearchingForTestsTask(myServerSocket, getConfiguration(), myTempFile) {

                        @Override
                        protected void fillTestObjects(final Map<PsiClass, Map<PsiMethod, List<String>>> classes) throws CantRunException {
                            final HashMap<PsiClass, Map<PsiMethod, List<String>>> fullClassList = ContainerUtil.newHashMap();
                            super.fillTestObjects(fullClassList);
                            for (final PsiClass aClass : fullClassList.keySet()) {
                                if (!ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                                    @Override
                                    public Boolean compute() {
                                        return TestNGUtil.hasTest(aClass);
                                    }
                                })) {
                                    classes.put(aClass, fullClassList.get(aClass));
                                }
                            }
                            final GlobalSearchScope scope = getConfiguration().getConfigurationModule().getSearchScope();
                            final Project project = getConfiguration().getProject();
                            for (final AbstractTestProxy proxy : failedTests) {
                                ApplicationManager.getApplication().runReadAction(() -> includeFailedTestWithDependencies(classes, scope, project, proxy));
                            }
                        }
                    };
                }
            };
        }
    };
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) PsiMethod(com.intellij.psi.PsiMethod) TestNGConfiguration(com.theoryinpractice.testng.configuration.TestNGConfiguration) PsiClass(com.intellij.psi.PsiClass) AbstractTestProxy(com.intellij.execution.testframework.AbstractTestProxy) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Executor(com.intellij.execution.Executor) TestNGRunnableState(com.theoryinpractice.testng.configuration.TestNGRunnableState) SearchingForTestsTask(com.theoryinpractice.testng.configuration.SearchingForTestsTask) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) Computable(com.intellij.openapi.util.Computable)

Example 44 with Computable

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

the class JavaLineMarkerProvider method collectSlowLineMarkers.

@Override
public void collectSlowLineMarkers(@NotNull final List<PsiElement> elements, @NotNull final Collection<LineMarkerInfo> result) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    List<Computable<List<LineMarkerInfo>>> tasks = new ArrayList<>();
    MultiMap<PsiClass, PsiMethod> byClass = MultiMap.create();
    //noinspection ForLoopReplaceableByForEach
    for (int i = 0; i < elements.size(); i++) {
        PsiElement element = elements.get(i);
        ProgressManager.checkCanceled();
        if (!(element instanceof PsiIdentifier))
            continue;
        PsiElement parent = element.getParent();
        if (parent instanceof PsiMethod) {
            final PsiMethod method = (PsiMethod) parent;
            PsiClass psiClass = method.getContainingClass();
            if (PsiUtil.canBeOverriden(method) && psiClass != null) {
                byClass.putValue(psiClass, method);
            }
        } else if (parent instanceof PsiClass && !(parent instanceof PsiTypeParameter)) {
            tasks.add(() -> collectInheritingClasses((PsiClass) parent));
        }
    }
    for (PsiClass psiClass : byClass.keySet()) {
        Collection<PsiMethod> methods = byClass.get(psiClass);
        tasks.add(() -> collectSiblingInheritedMethods(methods));
        tasks.add(() -> collectOverridingMethods(methods, psiClass));
    }
    Object lock = new Object();
    ProgressIndicator indicator = ProgressIndicatorProvider.getGlobalProgressIndicator();
    JobLauncher.getInstance().invokeConcurrentlyUnderProgress(tasks, indicator, true, computable -> {
        List<LineMarkerInfo> infos = computable.compute();
        synchronized (lock) {
            result.addAll(infos);
        }
        return true;
    });
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Computable(com.intellij.openapi.util.Computable)

Example 45 with Computable

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

the class StaticIconFieldsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = LangDataKeys.PROJECT.getData(e.getDataContext());
    final UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setTabName("Statics");
    presentation.setTabText("Statitcs");
    final UsageView view = UsageViewManager.getInstance(project).showUsages(UsageTarget.EMPTY_ARRAY, Usage.EMPTY_ARRAY, presentation);
    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Searching icons usages") {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
            final GlobalSearchScope all = GlobalSearchScope.allScope(project);
            PsiClass allIcons = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {

                @Override
                public PsiClass compute() {
                    return facade.findClass("com.intellij.icons.AllIcons", all);
                }
            });
            searchFields(allIcons, view, indicator);
            PsiClass[] classes = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass[]>() {

                @Override
                public PsiClass[] compute() {
                    return facade.findPackage("icons").getClasses(all);
                }
            });
            for (PsiClass iconsClass : classes) {
                searchFields(iconsClass, view, indicator);
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) Task(com.intellij.openapi.progress.Task) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Computable(com.intellij.openapi.util.Computable)

Aggregations

Computable (com.intellij.openapi.util.Computable)49 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)11 Project (com.intellij.openapi.project.Project)10 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)8 PsiFile (com.intellij.psi.PsiFile)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 Application (com.intellij.openapi.application.Application)5 Task (com.intellij.openapi.progress.Task)5 IOException (java.io.IOException)5 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 Editor (com.intellij.openapi.editor.Editor)4 Ref (com.intellij.openapi.util.Ref)4 Module (com.intellij.openapi.module.Module)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 PsiElement (com.intellij.psi.PsiElement)3 UsageInfo (com.intellij.usageView.UsageInfo)3 Logger (com.intellij.openapi.diagnostic.Logger)2