Search in sources :

Example 1 with AutomaticRenamer

use of com.intellij.refactoring.rename.naming.AutomaticRenamer 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 2 with AutomaticRenamer

use of com.intellij.refactoring.rename.naming.AutomaticRenamer 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)2 AutomaticRenamer (com.intellij.refactoring.rename.naming.AutomaticRenamer)2 UsageInfo (com.intellij.usageView.UsageInfo)2 MultiMap (com.intellij.util.containers.MultiMap)2 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)1 Computable (com.intellij.openapi.util.Computable)1 AutomaticRenamerFactory (com.intellij.refactoring.rename.naming.AutomaticRenamerFactory)1 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)1 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)1 NonCodeUsageInfo (com.intellij.refactoring.util.NonCodeUsageInfo)1 RelatedUsageInfo (com.intellij.refactoring.util.RelatedUsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 NotNull (org.jetbrains.annotations.NotNull)1