Search in sources :

Example 11 with RefactoringElementListener

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

the class RenameProcessor method performRefactoring.

@Override
public void performRefactoring(@NotNull UsageInfo[] usages) {
    List<Runnable> postRenameCallbacks = new ArrayList<>();
    final MultiMap<PsiElement, UsageInfo> classified = classifyUsages(myAllRenames.keySet(), usages);
    for (final PsiElement element : myAllRenames.keySet()) {
        String newName = myAllRenames.get(element);
        final RefactoringElementListener elementListener = getTransaction().getElementListener(element);
        final RenamePsiElementProcessor renamePsiElementProcessor = RenamePsiElementProcessor.forElement(element);
        Runnable postRenameCallback = renamePsiElementProcessor.getPostRenameCallback(element, newName, elementListener);
        final Collection<UsageInfo> infos = classified.get(element);
        try {
            RenameUtil.doRename(element, newName, infos.toArray(new UsageInfo[infos.size()]), myProject, elementListener);
        } catch (final IncorrectOperationException e) {
            RenameUtil.showErrorMessage(e, element, myProject);
            return;
        }
        if (postRenameCallback != null) {
            postRenameCallbacks.add(postRenameCallback);
        }
    }
    for (Runnable runnable : postRenameCallbacks) {
        runnable.run();
    }
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        }
    }
    myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
    if (!mySkippedUsages.isEmpty()) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
            ApplicationManager.getApplication().invokeLater(() -> {
                final IdeFrame ideFrame = WindowManager.getInstance().getIdeFrame(myProject);
                if (ideFrame != null) {
                    StatusBarEx statusBar = (StatusBarEx) ideFrame.getStatusBar();
                    HyperlinkListener listener = new HyperlinkListener() {

                        @Override
                        public void hyperlinkUpdate(HyperlinkEvent e) {
                            if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
                                return;
                            Messages.showMessageDialog("<html>Following usages were safely skipped:<br>" + StringUtil.join(mySkippedUsages, unresolvableCollisionUsageInfo -> unresolvableCollisionUsageInfo.getDescription(), "<br>") + "</html>", "Not All Usages Were Renamed", null);
                        }
                    };
                    statusBar.notifyProgressByBalloon(MessageType.WARNING, "<html><body>Unable to rename certain usages. <a href=\"\">Browse</a></body></html>", null, listener);
                }
            }, ModalityState.NON_MODAL);
        }
    }
}
Also used : DescriptiveNameUtil(com.intellij.lang.findUsages.DescriptiveNameUtil) IdeFrame(com.intellij.openapi.wm.IdeFrame) java.util(java.util) MessageType(com.intellij.openapi.ui.MessageType) HyperlinkEvent(javax.swing.event.HyperlinkEvent) ModalityState(com.intellij.openapi.application.ModalityState) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) UsageInfo(com.intellij.usageView.UsageInfo) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) AutomaticRenamer(com.intellij.refactoring.rename.naming.AutomaticRenamer) ContainerUtil(com.intellij.util.containers.ContainerUtil) CopyFilesOrDirectoriesHandler(com.intellij.refactoring.copy.CopyFilesOrDirectoriesHandler) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) HyperlinkListener(javax.swing.event.HyperlinkListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException) StatusBarEx(com.intellij.openapi.wm.ex.StatusBarEx) StringUtil(com.intellij.openapi.util.text.StringUtil) WindowManager(com.intellij.openapi.wm.WindowManager) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) LightElement(com.intellij.psi.impl.light.LightElement) AutomaticRenamerFactory(com.intellij.refactoring.rename.naming.AutomaticRenamerFactory) UsageViewUtil(com.intellij.usageView.UsageViewUtil) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) ApplicationManager(com.intellij.openapi.application.ApplicationManager) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) HyperlinkEvent(javax.swing.event.HyperlinkEvent) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) IdeFrame(com.intellij.openapi.wm.IdeFrame) HyperlinkListener(javax.swing.event.HyperlinkListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException) StatusBarEx(com.intellij.openapi.wm.ex.StatusBarEx) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo)

Example 12 with RefactoringElementListener

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

the class RefactoringTransactionImpl method addAffectedElement.

private void addAffectedElement(PsiElement oldElement) {
    if (myOldElementToListenerListMap.get(oldElement) != null)
        return;
    ArrayList<RefactoringElementListener> listenerList = new ArrayList<>();
    for (RefactoringElementListenerProvider provider : myListenerProviders) {
        try {
            final RefactoringElementListener listener = provider.getListener(oldElement);
            if (listener != null) {
                listenerList.add(listener);
            }
        } catch (Throwable e) {
            LOG.error(e);
        }
    }
    myOldElementToListenerListMap.put(oldElement, listenerList);
}
Also used : RefactoringElementListenerProvider(com.intellij.refactoring.listeners.RefactoringElementListenerProvider) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) ArrayList(java.util.ArrayList)

Example 13 with RefactoringElementListener

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

the class RefactoringTransactionImpl method getElementListener.

@Override
public RefactoringElementListener getElementListener(PsiElement oldElement) {
    RefactoringElementListener listener = myOldElementToTransactionListenerMap.get(oldElement);
    if (listener == null) {
        listener = new MyRefactoringElementListener(oldElement);
        myOldElementToTransactionListenerMap.put(oldElement, listener);
    }
    return listener;
}
Also used : RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener)

Example 14 with RefactoringElementListener

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

the class TestsPattern method getListener.

@Nullable
@Override
public RefactoringElementListener getListener(PsiElement element, JUnitConfiguration configuration) {
    final RefactoringElementListenerComposite composite = new RefactoringElementListenerComposite();
    final JUnitConfiguration.Data data = configuration.getPersistentData();
    final Set<String> patterns = data.getPatterns();
    for (final String pattern : patterns) {
        final PsiClass testClass = getTestClass(configuration.getProject(), pattern.trim());
        if (testClass != null && testClass.equals(element)) {
            final RefactoringElementListener listeners = RefactoringListeners.getListeners(testClass, new RefactoringListeners.Accessor<PsiClass>() {

                private String myOldName = testClass.getQualifiedName();

                @Override
                public void setName(String qualifiedName) {
                    final Set<String> replaced = new LinkedHashSet<>();
                    for (String currentPattern : patterns) {
                        if (myOldName.equals(currentPattern)) {
                            replaced.add(qualifiedName);
                            myOldName = qualifiedName;
                        } else {
                            replaced.add(currentPattern);
                        }
                    }
                    patterns.clear();
                    patterns.addAll(replaced);
                }

                @Override
                public PsiClass getPsiElement() {
                    return testClass;
                }

                @Override
                public void setPsiElement(PsiClass psiElement) {
                    if (psiElement == testClass) {
                        setName(psiElement.getQualifiedName());
                    }
                }
            });
            if (listeners != null) {
                composite.addListener(listeners);
            }
        }
    }
    return composite;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) Nullable(org.jetbrains.annotations.Nullable)

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