Search in sources :

Example 1 with UsageViewDescriptor

use of com.intellij.usageView.UsageViewDescriptor in project intellij-community by JetBrains.

the class BaseRefactoringProcessor method previewRefactoring.

protected void previewRefactoring(@NotNull UsageInfo[] usages) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (!PREVIEW_IN_TESTS)
            throw new RuntimeException("Unexpected preview in tests: " + StringUtil.join(usages, info -> info.toString(), ", "));
        ensureElementsWritable(usages, createUsageViewDescriptor(usages));
        execute(usages);
        return;
    }
    final UsageViewDescriptor viewDescriptor = createUsageViewDescriptor(usages);
    final PsiElement[] elements = viewDescriptor.getElements();
    final PsiElement2UsageTargetAdapter[] targets = PsiElement2UsageTargetAdapter.convert(elements);
    Factory<UsageSearcher> factory = new Factory<UsageSearcher>() {

        @Override
        public UsageSearcher create() {
            return new UsageInfoSearcherAdapter() {

                @Override
                public void generate(@NotNull final Processor<Usage> processor) {
                    ApplicationManager.getApplication().runReadAction(new Runnable() {

                        @Override
                        public void run() {
                            for (int i = 0; i < elements.length; i++) {
                                elements[i] = targets[i].getElement();
                            }
                            refreshElements(elements);
                        }
                    });
                    processUsages(processor, myProject);
                }

                @NotNull
                @Override
                protected UsageInfo[] findUsages() {
                    return BaseRefactoringProcessor.this.findUsages();
                }
            };
        }
    };
    showUsageView(viewDescriptor, factory, usages);
}
Also used : Language(com.intellij.lang.Language) LocalHistoryAction(com.intellij.history.LocalHistoryAction) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) RefactoringListenerManager(com.intellij.refactoring.listeners.RefactoringListenerManager) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Messages(com.intellij.openapi.ui.Messages) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) DumbService(com.intellij.openapi.project.DumbService) RefactoringTransaction(com.intellij.refactoring.listeners.impl.RefactoringTransaction) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonProjectFileWritingAccessProvider(com.intellij.openapi.fileEditor.impl.NonProjectFileWritingAccessProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.jetbrains.annotations.Nullable) ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) LocalHistory(com.intellij.history.LocalHistory) NotNull(org.jetbrains.annotations.NotNull) Factory(com.intellij.openapi.util.Factory) Ref(com.intellij.openapi.util.Ref) java.util(java.util) RefactoringListenerManagerImpl(com.intellij.refactoring.listeners.impl.RefactoringListenerManagerImpl) Computable(com.intellij.openapi.util.Computable) UsageInfo(com.intellij.usageView.UsageInfo) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) StatusBarUtil(com.intellij.openapi.wm.impl.status.StatusBarUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DataManager(com.intellij.ide.DataManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ThrowableRunnable(com.intellij.util.ThrowableRunnable) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.usages(com.intellij.usages) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) GuiUtils(com.intellij.ui.GuiUtils) UsageViewUtil(com.intellij.usageView.UsageViewUtil) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) TestOnly(org.jetbrains.annotations.TestOnly) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) UndoManager(com.intellij.openapi.command.undo.UndoManager) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) TransactionGuard(com.intellij.openapi.application.TransactionGuard) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) Processor(com.intellij.util.Processor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Factory(com.intellij.openapi.util.Factory) NotNull(org.jetbrains.annotations.NotNull) ThrowableRunnable(com.intellij.util.ThrowableRunnable) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Example 2 with UsageViewDescriptor

use of com.intellij.usageView.UsageViewDescriptor 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)

Aggregations

Language (com.intellij.lang.Language)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 Computable (com.intellij.openapi.util.Computable)2 EmptyRunnable (com.intellij.openapi.util.EmptyRunnable)2 Ref (com.intellij.openapi.util.Ref)2 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 LocalHistory (com.intellij.history.LocalHistory)1 LocalHistoryAction (com.intellij.history.LocalHistoryAction)1 DataManager (com.intellij.ide.DataManager)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 TransactionGuard (com.intellij.openapi.application.TransactionGuard)1 ApplicationManagerEx (com.intellij.openapi.application.ex.ApplicationManagerEx)1 ApplicationImpl (com.intellij.openapi.application.impl.ApplicationImpl)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 UndoConfirmationPolicy (com.intellij.openapi.command.UndoConfirmationPolicy)1 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)1 UndoManager (com.intellij.openapi.command.undo.UndoManager)1 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)1 Logger (com.intellij.openapi.diagnostic.Logger)1