Search in sources :

Example 6 with RefactoringElementListener

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

the class MoveClassesOrPackagesProcessor method performRefactoring.

protected void performRefactoring(@NotNull UsageInfo[] usages) {
    try {
        final Map<PsiClass, Boolean> allClasses = new HashMap<>();
        for (PsiElement element : myElementsToMove) {
            if (element instanceof PsiClass) {
                final PsiClass psiClass = (PsiClass) element;
                if (allClasses.containsKey(psiClass)) {
                    continue;
                }
                for (MoveAllClassesInFileHandler fileHandler : Extensions.getExtensions(MoveAllClassesInFileHandler.EP_NAME)) {
                    fileHandler.processMoveAllClassesInFile(allClasses, psiClass, myElementsToMove);
                }
            }
        }
        for (PsiElement element : myElementsToMove) {
            if (element instanceof PsiClass) {
                MoveClassesOrPackagesUtil.prepareMoveClass((PsiClass) element);
            }
        }
        final Map<PsiElement, PsiElement> oldToNewElementsMapping = new HashMap<>();
        for (int idx = 0; idx < myElementsToMove.length; idx++) {
            PsiElement element = myElementsToMove[idx];
            final RefactoringElementListener elementListener = getTransaction().getElementListener(element);
            if (element instanceof PsiPackage) {
                final PsiDirectory[] directories = ((PsiPackage) element).getDirectories();
                final PsiPackage newElement = MoveClassesOrPackagesUtil.doMovePackage((PsiPackage) element, myMoveDestination);
                LOG.assertTrue(newElement != null, element);
                oldToNewElementsMapping.put(element, newElement);
                int i = 0;
                final PsiDirectory[] newDirectories = newElement.getDirectories();
                if (newDirectories.length == 1) {
                    //everything is moved in one directory
                    for (PsiDirectory directory : directories) {
                        oldToNewElementsMapping.put(directory, newDirectories[0]);
                    }
                } else {
                    for (PsiDirectory directory : directories) {
                        if (myMoveDestination.verify(directory) != null) {
                            //e.g. directory is excluded so there is no source root for it, hence target directory would be missed from newDirectories
                            continue;
                        }
                        oldToNewElementsMapping.put(directory, newDirectories[i++]);
                    }
                }
                element = newElement;
            } else if (element instanceof PsiClass) {
                final PsiClass psiClass = (PsiClass) element;
                final PsiClass newElement = MoveClassesOrPackagesUtil.doMoveClass(psiClass, myMoveDestination.getTargetDirectory(element.getContainingFile()), allClasses.get(psiClass));
                oldToNewElementsMapping.put(element, newElement);
                element = newElement;
            } else {
                LOG.error("Unexpected element to move: " + element);
            }
            elementListener.elementMoved(element);
            myElementsToMove[idx] = element;
        }
        for (PsiElement element : myElementsToMove) {
            if (element instanceof PsiClass) {
                MoveClassesOrPackagesUtil.finishMoveClass((PsiClass) element);
            }
        }
        myNonCodeUsages = CommonMoveUtil.retargetUsages(usages, oldToNewElementsMapping);
        if (myOpenInEditor) {
            EditorHelper.openFilesInEditor(myElementsToMove);
        }
    } catch (IncorrectOperationException e) {
        myNonCodeUsages = new NonCodeUsageInfo[0];
        RefactoringUIUtil.processIncorrectOperation(myProject, e);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 7 with RefactoringElementListener

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

the class MoveMembersProcessor method performRefactoring.

protected void performRefactoring(@NotNull final UsageInfo[] usages) {
    try {
        PsiClass targetClass = JavaPsiFacade.getInstance(myProject).findClass(myOptions.getTargetClassName(), GlobalSearchScope.projectScope(myProject));
        if (targetClass == null)
            return;
        // collect anchors to place moved members at
        final Map<PsiMember, SmartPsiElementPointer<PsiElement>> anchors = new HashMap<>();
        final Map<PsiMember, PsiMember> anchorsInSourceClass = new HashMap<>();
        for (PsiMember member : myMembersToMove) {
            final MoveMemberHandler handler = MoveMemberHandler.EP_NAME.forLanguage(member.getLanguage());
            if (handler != null) {
                final PsiElement anchor = handler.getAnchor(member, targetClass, myMembersToMove);
                if (anchor instanceof PsiMember && myMembersToMove.contains((PsiMember) anchor)) {
                    anchorsInSourceClass.put(member, (PsiMember) anchor);
                } else {
                    anchors.put(member, anchor == null ? null : SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(anchor));
                }
            }
        }
        // correct references to moved members from the outside
        ArrayList<MoveMembersUsageInfo> otherUsages = new ArrayList<>();
        for (UsageInfo usageInfo : usages) {
            MoveMembersUsageInfo usage = (MoveMembersUsageInfo) usageInfo;
            if (!usage.reference.isValid())
                continue;
            final MoveMemberHandler handler = MoveMemberHandler.EP_NAME.forLanguage(usageInfo.getElement().getLanguage());
            if (handler != null) {
                if (handler.changeExternalUsage(myOptions, usage))
                    continue;
            }
            otherUsages.add(usage);
        }
        // correct references inside moved members and outer references to Inner Classes
        final Map<PsiMember, PsiMember> movedMembers = new HashMap<>();
        for (PsiMember member : myMembersToMove) {
            ArrayList<PsiReference> refsToBeRebind = new ArrayList<>();
            for (Iterator<MoveMembersUsageInfo> iterator = otherUsages.iterator(); iterator.hasNext(); ) {
                MoveMembersUsageInfo info = iterator.next();
                if (member.equals(info.member)) {
                    PsiReference ref = info.getReference();
                    if (ref != null) {
                        refsToBeRebind.add(ref);
                    }
                    iterator.remove();
                }
            }
            final RefactoringElementListener elementListener = getTransaction().getElementListener(member);
            final MoveMemberHandler handler = MoveMemberHandler.EP_NAME.forLanguage(member.getLanguage());
            if (handler != null) {
                final PsiElement anchor;
                if (anchorsInSourceClass.containsKey(member)) {
                    final PsiMember memberInSourceClass = anchorsInSourceClass.get(member);
                    //anchor should be already moved as myMembersToMove contains members in order they appear in source class
                    anchor = memberInSourceClass != null ? movedMembers.get(memberInSourceClass) : null;
                } else {
                    final SmartPsiElementPointer<PsiElement> pointer = anchors.get(member);
                    anchor = pointer != null ? pointer.getElement() : null;
                }
                PsiMember newMember = handler.doMove(myOptions, member, anchor, targetClass);
                movedMembers.put(member, newMember);
                elementListener.elementMoved(newMember);
                fixModifierList(member, newMember, usages);
                for (PsiReference reference : refsToBeRebind) {
                    reference.bindToElement(newMember);
                }
            }
        }
        // qualifier info must be decoded after members are moved
        final MoveMemberHandler handler = MoveMemberHandler.EP_NAME.forLanguage(myTargetClass.getLanguage());
        if (handler != null)
            handler.decodeContextInfo(myTargetClass);
        myMembersToMove.clear();
        if (myMoveCallback != null) {
            myMoveCallback.refactoringCompleted();
        }
        if (myOpenInEditor && !movedMembers.isEmpty()) {
            final PsiMember item = ContainerUtil.getFirstItem(movedMembers.values());
            if (item != null) {
                EditorHelper.openInEditor(item);
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

Example 8 with RefactoringElementListener

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

the class ChangeSignatureProcessorBase method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    RefactoringTransaction transaction = getTransaction();
    final ChangeInfo changeInfo = myChangeInfo;
    final RefactoringElementListener elementListener = transaction == null ? null : transaction.getElementListener(changeInfo.getMethod());
    final String fqn = CopyReferenceAction.elementToFqn(changeInfo.getMethod());
    if (fqn != null) {
        UndoableAction action = new BasicUndoableAction() {

            @Override
            public void undo() {
                if (elementListener instanceof UndoRefactoringElementListener) {
                    ((UndoRefactoringElementListener) elementListener).undoElementMovedOrRenamed(changeInfo.getMethod(), fqn);
                }
            }

            @Override
            public void redo() {
            }
        };
        UndoManager.getInstance(myProject).undoableActionPerformed(action);
    }
    try {
        doChangeSignature(changeInfo, usages);
        final PsiElement method = changeInfo.getMethod();
        LOG.assertTrue(method.isValid());
        if (elementListener != null && changeInfo.isNameChanged()) {
            elementListener.elementRenamed(method);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) RefactoringTransaction(com.intellij.refactoring.listeners.impl.RefactoringTransaction) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) IncorrectOperationException(com.intellij.util.IncorrectOperationException) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) PsiElement(com.intellij.psi.PsiElement)

Example 9 with RefactoringElementListener

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

the class RunConfigurationRefactoringElementListenerProvider method getListener.

@Override
public RefactoringElementListener getListener(final PsiElement element) {
    RefactoringElementListenerComposite composite = null;
    final List<RunConfiguration> configurations = RunManager.getInstance(element.getProject()).getAllConfigurationsList();
    for (RunConfiguration configuration : configurations) {
        if (configuration instanceof RefactoringListenerProvider) {
            // todo: perhaps better way to handle listeners?
            RefactoringElementListener listener;
            try {
                listener = ((RefactoringListenerProvider) configuration).getRefactoringElementListener(element);
            } catch (Exception e) {
                LOG.error(e);
                continue;
            }
            if (listener != null) {
                if (configuration instanceof LocatableConfiguration) {
                    listener = new NameGeneratingListenerDecorator((LocatableConfiguration) configuration, listener);
                }
                if (composite == null) {
                    composite = new RefactoringElementListenerComposite();
                }
                composite.addListener(listener);
            }
        }
    }
    return composite;
}
Also used : RunConfiguration(com.intellij.execution.configurations.RunConfiguration) LocatableConfiguration(com.intellij.execution.configurations.LocatableConfiguration) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) RefactoringListenerProvider(com.intellij.execution.configurations.RefactoringListenerProvider)

Example 10 with RefactoringElementListener

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

the class CoverageJavaRunConfigurationExtension method wrapElementListener.

@Override
public RefactoringElementListener wrapElementListener(PsiElement element, RunConfigurationBase configuration, RefactoringElementListener listener) {
    if (!isApplicableFor(configuration)) {
        return listener;
    }
    final JavaCoverageEnabledConfiguration coverageEnabledConfiguration = JavaCoverageEnabledConfiguration.getFrom(configuration);
    if (coverageEnabledConfiguration != null) {
        final Project project = configuration.getProject();
        final ClassFilter[] patterns = coverageEnabledConfiguration.getCoveragePatterns();
        final String[] filters = getFilters(coverageEnabledConfiguration);
        if (patterns != null) {
            assert filters != null;
            if (element instanceof PsiClass) {
                final String qualifiedName = ((PsiClass) element).getQualifiedName();
                final int idx = ArrayUtil.find(filters, qualifiedName);
                if (idx > -1) {
                    final RefactoringListeners.Accessor<PsiClass> accessor = new MyClassAccessor(project, patterns, idx, filters);
                    final RefactoringElementListener classListener = RefactoringListeners.getClassOrPackageListener(element, accessor);
                    if (classListener != null) {
                        listener = appendListener(listener, classListener);
                    }
                } else if (qualifiedName != null) {
                    final String packageName = StringUtil.getPackageName(qualifiedName);
                    if (!StringUtil.isEmpty(packageName)) {
                        for (int i = 0; i < filters.length; i++) {
                            String filter = filters[i];
                            if (filter.equals(packageName + ".*")) {
                                listener = appendListener(listener, new RefactoringListeners.RefactorPackageByClass(new MyClassAccessor(project, patterns, i, filters)));
                                break;
                            }
                        }
                    }
                }
            } else if (element instanceof PsiPackage) {
                final String qualifiedName = ((PsiPackage) element).getQualifiedName();
                for (int i = 0, filtersLength = filters.length; i < filtersLength; i++) {
                    if (filters[i].startsWith(qualifiedName + ".")) {
                        final RefactoringElementListener packageListener;
                        if (filters[i].endsWith("*")) {
                            packageListener = RefactoringListeners.getListener((PsiPackage) element, new MyPackageAccessor(project, patterns, i, filters));
                        } else {
                            packageListener = RefactoringListeners.getClassOrPackageListener(element, new MyClassAccessor(project, patterns, i, filters));
                        }
                        if (packageListener != null) {
                            listener = appendListener(listener, packageListener);
                        }
                    }
                }
            }
        }
    }
    return listener;
}
Also used : RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage) JavaCoverageEnabledConfiguration(com.intellij.execution.configurations.coverage.JavaCoverageEnabledConfiguration) Project(com.intellij.openapi.project.Project) ClassFilter(com.intellij.ui.classFilter.ClassFilter) RefactoringListeners(com.intellij.execution.junit.RefactoringListeners)

Aggregations

RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)14 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 UndoRefactoringElementListener (com.intellij.refactoring.listeners.UndoRefactoringElementListener)4 Nullable (org.jetbrains.annotations.Nullable)4 PsiElement (com.intellij.psi.PsiElement)3 Project (com.intellij.openapi.project.Project)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFile (com.intellij.psi.PsiFile)2 RefactoringElementListenerComposite (com.intellij.refactoring.listeners.RefactoringElementListenerComposite)2 RefactoringElementListenerProvider (com.intellij.refactoring.listeners.RefactoringElementListenerProvider)2 NonCodeUsageInfo (com.intellij.refactoring.util.NonCodeUsageInfo)2 UsageInfo (com.intellij.usageView.UsageInfo)2 HashMap (com.intellij.util.containers.HashMap)2 LocatableConfiguration (com.intellij.execution.configurations.LocatableConfiguration)1 RefactoringListenerProvider (com.intellij.execution.configurations.RefactoringListenerProvider)1 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)1 JavaCoverageEnabledConfiguration (com.intellij.execution.configurations.coverage.JavaCoverageEnabledConfiguration)1 RefactoringListeners (com.intellij.execution.junit.RefactoringListeners)1 DescriptiveNameUtil (com.intellij.lang.findUsages.DescriptiveNameUtil)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1