Search in sources :

Example 11 with RefactoringEventData

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

the class VariableInplaceRenamer method performRefactoringRename.

protected void performRefactoringRename(final String newName, final StartMarkAction markAction) {
    final String refactoringId = getRefactoringId();
    try {
        PsiNamedElement elementToRename = getVariable();
        if (refactoringId != null) {
            final RefactoringEventData beforeData = new RefactoringEventData();
            beforeData.addElement(elementToRename);
            beforeData.addStringProperties(myOldName);
            myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, beforeData);
        }
        if (!isIdentifier(newName, myLanguage)) {
            return;
        }
        if (elementToRename != null) {
            new WriteCommandAction(myProject, getCommandName()) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    renameSynthetic(newName);
                }
            }.execute();
        }
        AutomaticRenamerFactory[] factories = Extensions.getExtensions(AutomaticRenamerFactory.EP_NAME);
        for (AutomaticRenamerFactory renamerFactory : factories) {
            if (elementToRename != null && renamerFactory.isApplicable(elementToRename)) {
                final List<UsageInfo> usages = new ArrayList<>();
                final AutomaticRenamer renamer = renamerFactory.createRenamer(elementToRename, newName, new ArrayList<>());
                if (renamer.hasAnythingToRename()) {
                    if (!ApplicationManager.getApplication().isUnitTestMode()) {
                        final AutomaticRenamingDialog renamingDialog = new AutomaticRenamingDialog(myProject, renamer);
                        if (!renamingDialog.showAndGet()) {
                            continue;
                        }
                    }
                    final Runnable runnable = () -> ApplicationManager.getApplication().runReadAction(() -> renamer.findUsages(usages, false, false));
                    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, RefactoringBundle.message("searching.for.variables"), true, myProject)) {
                        return;
                    }
                    if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, PsiUtilCore.toPsiElementArray(renamer.getElements())))
                        return;
                    final Runnable performAutomaticRename = () -> {
                        CommandProcessor.getInstance().markCurrentCommandAsGlobal(myProject);
                        final UsageInfo[] usageInfos = usages.toArray(new UsageInfo[usages.size()]);
                        final MultiMap<PsiElement, UsageInfo> classified = RenameProcessor.classifyUsages(renamer.getElements(), usageInfos);
                        for (final PsiNamedElement element : renamer.getElements()) {
                            final String newElementName = renamer.getNewName(element);
                            if (newElementName != null) {
                                final Collection<UsageInfo> infos = classified.get(element);
                                RenameUtil.doRename(element, newElementName, infos.toArray(new UsageInfo[infos.size()]), myProject, RefactoringElementListener.DEAF);
                            }
                        }
                    };
                    final WriteCommandAction writeCommandAction = new WriteCommandAction(myProject, getCommandName()) {

                        @Override
                        protected void run(@NotNull Result result) throws Throwable {
                            performAutomaticRename.run();
                        }
                    };
                    if (ApplicationManager.getApplication().isUnitTestMode()) {
                        writeCommandAction.execute();
                    } else {
                        ApplicationManager.getApplication().invokeLater(() -> writeCommandAction.execute());
                    }
                }
            }
        }
    } finally {
        if (refactoringId != null) {
            final RefactoringEventData afterData = new RefactoringEventData();
            afterData.addElement(getVariable());
            afterData.addStringProperties(newName);
            myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, afterData);
        }
        try {
            ((EditorImpl) InjectedLanguageUtil.getTopLevelEditor(myEditor)).stopDumbLater();
        } finally {
            FinishMarkAction.finish(myProject, myEditor, markAction);
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) AutomaticRenamer(com.intellij.refactoring.rename.naming.AutomaticRenamer) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) MultiMap(com.intellij.util.containers.MultiMap) AutomaticRenamerFactory(com.intellij.refactoring.rename.naming.AutomaticRenamerFactory) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Collection(java.util.Collection) UsageInfo(com.intellij.usageView.UsageInfo)

Example 12 with RefactoringEventData

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

the class SafeDeleteProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usages = refUsages.get();
    ArrayList<String> conflicts = new ArrayList<>();
    for (PsiElement element : myElements) {
        for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
            if (delegate.handlesElement(element)) {
                Collection<String> foundConflicts = delegate instanceof SafeDeleteProcessorDelegateBase ? ((SafeDeleteProcessorDelegateBase) delegate).findConflicts(element, myElements, usages) : delegate.findConflicts(element, myElements);
                if (foundConflicts != null) {
                    conflicts.addAll(foundConflicts);
                }
                break;
            }
        }
    }
    final HashMap<PsiElement, UsageHolder> elementsToUsageHolders = sortUsages(usages);
    final Collection<UsageHolder> usageHolders = elementsToUsageHolders.values();
    for (UsageHolder usageHolder : usageHolders) {
        if (usageHolder.hasUnsafeUsagesInCode()) {
            conflicts.add(usageHolder.getDescription());
        }
    }
    if (!conflicts.isEmpty()) {
        final RefactoringEventData conflictData = new RefactoringEventData();
        conflictData.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts);
        myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).conflictsDetected("refactoring.safeDelete", conflictData);
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            if (!ConflictsInTestsException.isTestIgnore())
                throw new ConflictsInTestsException(conflicts);
        } else {
            UnsafeUsagesDialog dialog = new UnsafeUsagesDialog(ArrayUtil.toStringArray(conflicts), myProject);
            if (!dialog.showAndGet()) {
                final int exitCode = dialog.getExitCode();
                // dialog is always dismissed;
                prepareSuccessful();
                if (exitCode == UnsafeUsagesDialog.VIEW_USAGES_EXIT_CODE) {
                    showUsages(Arrays.stream(usages).filter(usage -> usage instanceof SafeDeleteReferenceUsageInfo && !((SafeDeleteReferenceUsageInfo) usage).isSafeDelete()).toArray(UsageInfo[]::new), usages);
                }
                return false;
            } else {
                myPreviewNonCodeUsages = false;
            }
        }
    }
    UsageInfo[] preprocessedUsages = usages;
    for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
        preprocessedUsages = delegate.preprocessUsages(myProject, preprocessedUsages);
        if (preprocessedUsages == null)
            return false;
    }
    final UsageInfo[] filteredUsages = UsageViewUtil.removeDuplicatedUsages(preprocessedUsages);
    // dialog is always dismissed
    prepareSuccessful();
    if (filteredUsages == null) {
        return false;
    }
    refUsages.set(filteredUsages);
    return true;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) SafeDeleteReferenceUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) SafeDeleteReferenceUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceUsageInfo) SafeDeleteCustomUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteCustomUsageInfo) SafeDeleteReferenceSimpleDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceSimpleDeleteUsageInfo) SafeDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteUsageInfo)

Example 13 with RefactoringEventData

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

the class RenameProcessor method getAfterData.

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

Example 14 with RefactoringEventData

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

the class AbstractInplaceIntroducer method performRefactoring.

@Override
protected boolean performRefactoring() {
    if (!ensureValid())
        return false;
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        final String refactoringId = getRefactoringId();
        if (refactoringId != null) {
            final RefactoringEventData beforeData = new RefactoringEventData();
            final V localVariable = getLocalVariable();
            if (localVariable != null) {
                beforeData.addElement(localVariable);
            } else {
                final E beforeExpr = getBeforeExpr();
                if (beforeExpr != null) {
                    beforeData.addElement(beforeExpr);
                }
            }
            myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, beforeData);
        }
        performIntroduce();
    }, getCommandName(), getCommandName());
    V variable = getVariable();
    if (variable != null) {
        saveSettings(variable);
    }
    return false;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData)

Example 15 with RefactoringEventData

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

the class AbstractInplaceIntroducer method moveOffsetAfter.

@Override
protected void moveOffsetAfter(boolean success) {
    if (getLocalVariable() != null && getLocalVariable().isValid()) {
        myEditor.getCaretModel().moveToOffset(getLocalVariable().getTextOffset());
        myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    } else if (getExprMarker() != null) {
        final RangeMarker exprMarker = getExprMarker();
        if (exprMarker.isValid()) {
            myEditor.getCaretModel().moveToOffset(exprMarker.getStartOffset());
            myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
        }
    }
    super.moveOffsetAfter(success);
    if (myLocalMarker != null && !isRestart()) {
        myLocalMarker.dispose();
    }
    if (success) {
        performPostIntroduceTasks();
        final String refactoringId = getRefactoringId();
        if (refactoringId != null) {
            final RefactoringEventData afterData = new RefactoringEventData();
            afterData.addElement(getVariable());
            myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, afterData);
        }
    }
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData)

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