Search in sources :

Example 36 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class IntroduceParameterProcessor method getAfterData.

@Nullable
@Override
protected RefactoringEventData getAfterData(@NotNull UsageInfo[] usages) {
    final PsiParameter parameter = JavaIntroduceParameterMethodUsagesProcessor.getAnchorParameter(myMethodToReplaceIn);
    final RefactoringEventData afterData = new RefactoringEventData();
    afterData.addElement(parameter);
    return afterData;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class BaseRefactoringProcessor method doRefactoring.

private void doRefactoring(@NotNull final Collection<UsageInfo> usageInfoSet) {
    for (Iterator<UsageInfo> iterator = usageInfoSet.iterator(); iterator.hasNext(); ) {
        UsageInfo usageInfo = iterator.next();
        final PsiElement element = usageInfo.getElement();
        if (element == null || !isToBeChanged(usageInfo)) {
            iterator.remove();
        }
    }
    LocalHistoryAction action = LocalHistory.getInstance().startAction(getCommandName());
    final UsageInfo[] writableUsageInfos = usageInfoSet.toArray(new UsageInfo[usageInfoSet.size()]);
    try {
        PsiDocumentManager.getInstance(myProject).commitAllDocuments();
        RefactoringListenerManagerImpl listenerManager = (RefactoringListenerManagerImpl) RefactoringListenerManager.getInstance(myProject);
        myTransaction = listenerManager.startTransaction();
        final Map<RefactoringHelper, Object> preparedData = new LinkedHashMap<>();
        final Runnable prepareHelpersRunnable = new Runnable() {

            @Override
            public void run() {
                for (final RefactoringHelper helper : Extensions.getExtensions(RefactoringHelper.EP_NAME)) {
                    Object operation = ApplicationManager.getApplication().runReadAction(new Computable<Object>() {

                        @Override
                        public Object compute() {
                            return helper.prepareOperation(writableUsageInfos);
                        }
                    });
                    preparedData.put(helper, operation);
                }
            }
        };
        ProgressManager.getInstance().runProcessWithProgressSynchronously(prepareHelpersRunnable, "Prepare ...", false, myProject);
        Runnable performRefactoringRunnable = () -> {
            final String refactoringId = getRefactoringId();
            if (refactoringId != null) {
                RefactoringEventData data = getBeforeData();
                if (data != null) {
                    data.addUsages(usageInfoSet);
                }
                myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, data);
            }
            try {
                if (refactoringId != null) {
                    UndoableAction action1 = new UndoRefactoringAction(myProject, refactoringId);
                    UndoManager.getInstance(myProject).undoableActionPerformed(action1);
                }
                performRefactoring(writableUsageInfos);
            } finally {
                if (refactoringId != null) {
                    myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, getAfterData(writableUsageInfos));
                }
            }
        };
        ApplicationImpl app = (ApplicationImpl) ApplicationManagerEx.getApplicationEx();
        if (Registry.is("run.refactorings.under.progress")) {
            app.runWriteActionWithProgressInDispatchThread(getCommandName(), myProject, null, null, indicator -> performRefactoringRunnable.run());
        } else {
            app.runWriteAction(performRefactoringRunnable);
        }
        DumbService.getInstance(myProject).completeJustSubmittedTasks();
        for (Map.Entry<RefactoringHelper, Object> e : preparedData.entrySet()) {
            //noinspection unchecked
            e.getKey().performOperation(myProject, e.getValue());
        }
        myTransaction.commit();
        app.runWriteAction(() -> performPsiSpoilingRefactoring());
    } finally {
        action.finish();
    }
    int count = writableUsageInfos.length;
    if (count > 0) {
        StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.refactoring.result", count));
    } else {
        if (!isPreviewUsages(writableUsageInfos)) {
            StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.noUsages"));
        }
    }
}
Also used : ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl) RefactoringListenerManagerImpl(com.intellij.refactoring.listeners.impl.RefactoringListenerManagerImpl) ThrowableRunnable(com.intellij.util.ThrowableRunnable) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) LocalHistoryAction(com.intellij.history.LocalHistoryAction) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) MultiMap(com.intellij.util.containers.MultiMap) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 38 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class BaseRefactoringProcessor method showConflicts.

protected boolean showConflicts(@NotNull MultiMap<PsiElement, String> conflicts, @Nullable final UsageInfo[] usages) {
    if (!conflicts.isEmpty() && ApplicationManager.getApplication().isUnitTestMode()) {
        if (!ConflictsInTestsException.isTestIgnore())
            throw new ConflictsInTestsException(conflicts.values());
        return true;
    }
    if (myPrepareSuccessfulSwingThreadCallback != null && !conflicts.isEmpty()) {
        final String refactoringId = getRefactoringId();
        if (refactoringId != null) {
            RefactoringEventData conflictUsages = new RefactoringEventData();
            conflictUsages.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts.values());
            myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).conflictsDetected(refactoringId, conflictUsages);
        }
        final ConflictsDialog conflictsDialog = prepareConflictsDialog(conflicts, usages);
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    prepareSuccessful();
    return true;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog)

Example 39 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class ChangeSignatureProcessorBase method getAfterData.

@Nullable
@Override
protected RefactoringEventData getAfterData(@NotNull UsageInfo[] usages) {
    RefactoringEventData data = new RefactoringEventData();
    data.addElement(getChangeInfo().getMethod());
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class RenameProcessor method preprocessUsages.

@Override
public boolean preprocessUsages(@NotNull final Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    RenameUtil.addConflictDescriptions(usagesIn, conflicts);
    RenamePsiElementProcessor.forElement(myPrimaryElement).findExistingNameConflicts(myPrimaryElement, myNewName, conflicts, myAllRenames);
    if (!conflicts.isEmpty()) {
        final RefactoringEventData conflictData = new RefactoringEventData();
        conflictData.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts.values());
        myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).conflictsDetected("refactoring.rename", conflictData);
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new ConflictsInTestsException(conflicts.values());
        }
        ConflictsDialog conflictsDialog = prepareConflictsDialog(conflicts, refUsages.get());
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    final List<UsageInfo> variableUsages = new ArrayList<>();
    if (!myRenamers.isEmpty()) {
        if (!findRenamedVariables(variableUsages))
            return false;
        final LinkedHashMap<PsiElement, String> renames = new LinkedHashMap<>();
        for (final AutomaticRenamer renamer : myRenamers) {
            final List<? extends PsiNamedElement> variables = renamer.getElements();
            for (final PsiNamedElement variable : variables) {
                final String newName = renamer.getNewName(variable);
                if (newName != null) {
                    addElement(variable, newName);
                    prepareRenaming(variable, newName, renames);
                }
            }
        }
        if (!renames.isEmpty()) {
            for (PsiElement element : renames.keySet()) {
                assertNonCompileElement(element);
            }
            myAllRenames.putAll(renames);
            final Runnable runnable = () -> {
                for (final Map.Entry<PsiElement, String> entry : renames.entrySet()) {
                    final UsageInfo[] usages = ApplicationManager.getApplication().runReadAction(new Computable<UsageInfo[]>() {

                        @Override
                        public UsageInfo[] compute() {
                            return RenameUtil.findUsages(entry.getKey(), entry.getValue(), mySearchInComments, mySearchTextOccurrences, myAllRenames);
                        }
                    });
                    Collections.addAll(variableUsages, usages);
                }
            };
            if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, RefactoringBundle.message("searching.for.variables"), true, myProject)) {
                return false;
            }
        }
    }
    final int[] choice = myAllRenames.size() > 1 ? new int[] { -1 } : null;
    try {
        for (Iterator<Map.Entry<PsiElement, String>> iterator = myAllRenames.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry<PsiElement, String> entry = iterator.next();
            if (entry.getKey() instanceof PsiFile) {
                final PsiFile file = (PsiFile) entry.getKey();
                final PsiDirectory containingDirectory = file.getContainingDirectory();
                if (CopyFilesOrDirectoriesHandler.checkFileExist(containingDirectory, choice, file, entry.getValue(), "Rename")) {
                    iterator.remove();
                    continue;
                }
            }
            RenameUtil.checkRename(entry.getKey(), entry.getValue());
        }
    } catch (IncorrectOperationException e) {
        CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("rename.title"), e.getMessage(), getHelpID(), myProject);
        return false;
    }
    final Set<UsageInfo> usagesSet = ContainerUtil.newLinkedHashSet(usagesIn);
    usagesSet.addAll(variableUsages);
    final List<UnresolvableCollisionUsageInfo> conflictUsages = RenameUtil.removeConflictUsages(usagesSet);
    if (conflictUsages != null) {
        mySkippedUsages.addAll(conflictUsages);
    }
    refUsages.set(usagesSet.toArray(new UsageInfo[usagesSet.size()]));
    prepareSuccessful();
    return PsiElementRenameHandler.canRename(myProject, null, myPrimaryElement);
}
Also used : AutomaticRenamer(com.intellij.refactoring.rename.naming.AutomaticRenamer) MultiMap(com.intellij.util.containers.MultiMap) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo) Computable(com.intellij.openapi.util.Computable) IncorrectOperationException(com.intellij.util.IncorrectOperationException) MultiMap(com.intellij.util.containers.MultiMap)

Aggregations

RefactoringEventData (com.intellij.refactoring.listeners.RefactoringEventData)51 Nullable (org.jetbrains.annotations.Nullable)36 NotNull (org.jetbrains.annotations.NotNull)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Project (com.intellij.openapi.project.Project)5 UsageInfo (com.intellij.usageView.UsageInfo)5 ArrayList (java.util.ArrayList)5 Result (com.intellij.openapi.application.Result)4 Logger (com.intellij.openapi.diagnostic.Logger)4 com.intellij.psi (com.intellij.psi)4 RefactoringEventListener (com.intellij.refactoring.listeners.RefactoringEventListener)4 MultiMap (com.intellij.util.containers.MultiMap)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)3 CommandProcessor (com.intellij.openapi.command.CommandProcessor)3 EditorColors (com.intellij.openapi.editor.colors.EditorColors)3 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)3 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)3 WindowManager (com.intellij.openapi.wm.WindowManager)3 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)3