Search in sources :

Example 31 with Computable

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

the class PlatformContentEntriesConfigurable method createEditor.

private void createEditor() {
    myModifiableModel = ApplicationManager.getApplication().runReadAction(new Computable<ModifiableRootModel>() {

        @Override
        public ModifiableRootModel compute() {
            return ModuleRootManager.getInstance(myModule).getModifiableModel();
        }
    });
    final ModuleConfigurationStateImpl moduleConfigurationState = new ModuleConfigurationStateImpl(myModule.getProject(), new DefaultModulesProvider(myModule.getProject())) {

        @Override
        public ModifiableRootModel getRootModel() {
            return myModifiableModel;
        }

        @Override
        public FacetsProvider getFacetsProvider() {
            return DefaultFacetsProvider.INSTANCE;
        }
    };
    myEditor = new CommonContentEntriesEditor(myModule.getName(), moduleConfigurationState, true, myRootTypes) {

        @Override
        protected List<ContentEntry> addContentEntries(VirtualFile[] files) {
            List<ContentEntry> entries = super.addContentEntries(files);
            addContentEntryPanels(entries.toArray(new ContentEntry[entries.size()]));
            return entries;
        }
    };
    JComponent component = ApplicationManager.getApplication().runReadAction(new Computable<JComponent>() {

        @Override
        public JComponent compute() {
            return myEditor.createComponent();
        }
    });
    myTopPanel.add(component, BorderLayout.CENTER);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) List(java.util.List) ModuleConfigurationStateImpl(com.intellij.openapi.module.impl.ModuleConfigurationStateImpl) Computable(com.intellij.openapi.util.Computable)

Example 32 with Computable

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

the class BaseRefactoringProcessor method doRun.

protected void doRun() {
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    final Ref<UsageInfo[]> refUsages = new Ref<>();
    final Ref<Language> refErrorLanguage = new Ref<>();
    final Ref<Boolean> refProcessCanceled = new Ref<>();
    final Ref<Boolean> anyException = new Ref<>();
    final Runnable findUsagesRunnable = new Runnable() {

        @Override
        public void run() {
            try {
                refUsages.set(DumbService.getInstance(myProject).runReadActionInSmartMode(new Computable<UsageInfo[]>() {

                    @Override
                    public UsageInfo[] compute() {
                        return findUsages();
                    }
                }));
            } catch (UnknownReferenceTypeException e) {
                refErrorLanguage.set(e.getElementLanguage());
            } catch (ProcessCanceledException e) {
                refProcessCanceled.set(Boolean.TRUE);
            } catch (Throwable e) {
                anyException.set(Boolean.TRUE);
                LOG.error(e);
            }
        }
    };
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(findUsagesRunnable, RefactoringBundle.message("progress.text"), true, myProject)) {
        return;
    }
    if (!refErrorLanguage.isNull()) {
        Messages.showErrorDialog(myProject, RefactoringBundle.message("unsupported.refs.found", refErrorLanguage.get().getDisplayName()), RefactoringBundle.message("error.title"));
        return;
    }
    if (DumbService.isDumb(myProject)) {
        DumbService.getInstance(myProject).showDumbModeNotification("Refactoring is not available until indices are ready");
        return;
    }
    if (!refProcessCanceled.isNull()) {
        Messages.showErrorDialog(myProject, "Index corruption detected. Please retry the refactoring - indexes will be rebuilt automatically", RefactoringBundle.message("error.title"));
        return;
    }
    if (!anyException.isNull()) {
        //do not proceed if find usages fails
        return;
    }
    assert !refUsages.isNull() : "Null usages from processor " + this;
    if (!preprocessUsages(refUsages))
        return;
    final UsageInfo[] usages = refUsages.get();
    assert usages != null;
    UsageViewDescriptor descriptor = createUsageViewDescriptor(usages);
    boolean isPreview = isPreviewUsages(usages);
    if (!isPreview) {
        isPreview = !ensureElementsWritable(usages, descriptor) || UsageViewUtil.hasReadOnlyUsages(usages);
        if (isPreview) {
            StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("readonly.occurences.found"));
        }
    }
    if (isPreview) {
        for (UsageInfo usage : usages) {
            LOG.assertTrue(usage != null, getClass());
        }
        previewRefactoring(usages);
    } else {
        execute(usages);
    }
}
Also used : UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) Ref(com.intellij.openapi.util.Ref) Language(com.intellij.lang.Language) ThrowableRunnable(com.intellij.util.ThrowableRunnable) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) Computable(com.intellij.openapi.util.Computable) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 33 with Computable

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

the class ExtractMethodHelper method processDuplicates.

public static void processDuplicates(@NotNull final PsiElement callElement, @NotNull final PsiElement generatedMethod, @NotNull final List<PsiElement> scope, @NotNull final SimpleDuplicatesFinder finder, @NotNull final Editor editor, @NotNull final Consumer<Pair<SimpleMatch, PsiElement>> replacer) {
    finder.setReplacement(callElement);
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        replaceDuplicates(callElement, editor, replacer, finder.findDuplicates(scope, generatedMethod));
        return;
    }
    final Project project = callElement.getProject();
    ProgressManager.getInstance().run(new Task.Backgroundable(project, RefactoringBundle.message("searching.for.duplicates"), true) {

        public void run(@NotNull ProgressIndicator indicator) {
            if (myProject == null || myProject.isDisposed())
                return;
            final List<SimpleMatch> duplicates = ApplicationManager.getApplication().runReadAction(new Computable<List<SimpleMatch>>() {

                @Override
                public List<SimpleMatch> compute() {
                    return finder.findDuplicates(scope, generatedMethod);
                }
            });
            ApplicationManager.getApplication().invokeLater(() -> replaceDuplicates(callElement, editor, replacer, duplicates));
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Computable(com.intellij.openapi.util.Computable) ThrowableComputable(com.intellij.openapi.util.ThrowableComputable)

Example 34 with Computable

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

the class CompilerPaths method getModuleOutputPath.

/**
   * The same as {@link #getModuleOutputDirectory} but returns String.
   * The method still returns a non-null value if the output path is specified in Settings but does not exist on disk.
   */
@Nullable
public static String getModuleOutputPath(final Module module, boolean forTestClasses) {
    final String outPathUrl;
    final Application application = ApplicationManager.getApplication();
    final CompilerModuleExtension extension = CompilerModuleExtension.getInstance(module);
    if (extension == null) {
        return null;
    }
    if (forTestClasses) {
        if (application.isDispatchThread()) {
            final String url = extension.getCompilerOutputUrlForTests();
            outPathUrl = url != null ? url : extension.getCompilerOutputUrl();
        } else {
            outPathUrl = application.runReadAction(new Computable<String>() {

                @Override
                public String compute() {
                    final String url = extension.getCompilerOutputUrlForTests();
                    return url != null ? url : extension.getCompilerOutputUrl();
                }
            });
        }
    } else {
        // for ordinary classes
        if (application.isDispatchThread()) {
            outPathUrl = extension.getCompilerOutputUrl();
        } else {
            outPathUrl = application.runReadAction(new Computable<String>() {

                @Override
                public String compute() {
                    return extension.getCompilerOutputUrl();
                }
            });
        }
    }
    return outPathUrl != null ? VirtualFileManager.extractPath(outPathUrl) : null;
}
Also used : Application(com.intellij.openapi.application.Application) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension) Computable(com.intellij.openapi.util.Computable) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with Computable

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

the class MoveClassesOrPackagesUtil method chooseDestinationPackage.

@Nullable
public static PsiDirectory chooseDestinationPackage(Project project, String packageName, @Nullable PsiDirectory baseDir) {
    final PsiManager psiManager = PsiManager.getInstance(project);
    final PackageWrapper packageWrapper = new PackageWrapper(psiManager, packageName);
    final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
    PsiDirectory directory;
    PsiDirectory[] directories = aPackage != null ? aPackage.getDirectories() : null;
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final VirtualFile baseDirVirtualFile = baseDir != null ? baseDir.getVirtualFile() : null;
    final boolean isBaseDirInTestSources = baseDirVirtualFile != null && fileIndex.isInTestSourceContent(baseDirVirtualFile);
    if (directories != null && directories.length == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(directories[0].getVirtualFile()) == isBaseDirInTestSources)) {
        directory = directories[0];
    } else {
        final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
        if (contentSourceRoots.size() == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(contentSourceRoots.get(0)) == isBaseDirInTestSources)) {
            directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {

                @Override
                public PsiDirectory compute() {
                    return RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, contentSourceRoots.get(0));
                }
            });
        } else {
            final VirtualFile sourceRootForFile = chooseSourceRoot(packageWrapper, contentSourceRoots, baseDir);
            if (sourceRootForFile == null)
                return null;
            directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {

                @Override
                public PsiDirectory compute() {
                    return new AutocreatingSingleSourceRootMoveDestination(packageWrapper, sourceRootForFile).getTargetDirectory((PsiDirectory) null);
                }
            });
        }
    }
    return directory;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PackageWrapper(com.intellij.refactoring.PackageWrapper) Computable(com.intellij.openapi.util.Computable) Nullable(org.jetbrains.annotations.Nullable)

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