Search in sources :

Example 1 with UndoRefactoringElementListener

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

the class TestNGConfiguration method getRefactoringElementListener.

@Nullable
public RefactoringElementListener getRefactoringElementListener(final PsiElement element) {
    if (data.TEST_OBJECT.equals(TestType.PACKAGE.getType())) {
        if (!(element instanceof PsiPackage))
            return null;
        final RefactoringElementListener listener = RefactoringListeners.getListener((PsiPackage) element, myPackage);
        return RunConfigurationExtension.wrapRefactoringElementListener(element, this, listener);
    } else if (data.TEST_OBJECT.equals(TestType.CLASS.getType())) {
        if (!(element instanceof PsiClass) && !(element instanceof PsiPackage))
            return null;
        final RefactoringElementListener listener = RefactoringListeners.getClassOrPackageListener(element, myClass);
        return RunConfigurationExtension.wrapRefactoringElementListener(element, this, listener);
    } else if (data.TEST_OBJECT.equals(TestType.METHOD.getType())) {
        if (!(element instanceof PsiMethod)) {
            final RefactoringElementListener listener = RefactoringListeners.getClassOrPackageListener(element, myClass);
            return RunConfigurationExtension.wrapRefactoringElementListener(element, this, listener);
        }
        final PsiMethod method = (PsiMethod) element;
        if (!method.getName().equals(data.getMethodName()))
            return null;
        if (!method.getContainingClass().equals(myClass.getPsiElement()))
            return null;
        class Listener extends RefactoringElementAdapter implements UndoRefactoringElementListener {

            public void elementRenamedOrMoved(@NotNull final PsiElement newElement) {
                data.setTestMethod(PsiLocation.fromPsiElement((PsiMethod) newElement));
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                final int methodIdx = oldQualifiedName.indexOf("#") + 1;
                if (methodIdx <= 0 || methodIdx >= oldQualifiedName.length())
                    return;
                data.METHOD_NAME = oldQualifiedName.substring(methodIdx);
            }
        }
        return RunConfigurationExtension.wrapRefactoringElementListener(element, this, new Listener());
    }
    return null;
}
Also used : RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) NotNull(org.jetbrains.annotations.NotNull) RefactoringElementAdapter(com.intellij.refactoring.listeners.RefactoringElementAdapter) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with UndoRefactoringElementListener

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

the class RenameUtil method doRename.

public static void doRename(final PsiElement element, String newName, UsageInfo[] usages, final Project project, @Nullable final RefactoringElementListener listener) throws IncorrectOperationException {
    final RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element);
    final String fqn = element instanceof PsiFile ? ((PsiFile) element).getVirtualFile().getPath() : CopyReferenceAction.elementToFqn(element);
    if (fqn != null) {
        UndoableAction action = new BasicUndoableAction() {

            @Override
            public void undo() throws UnexpectedUndoException {
                if (listener instanceof UndoRefactoringElementListener) {
                    ((UndoRefactoringElementListener) listener).undoElementMovedOrRenamed(element, fqn);
                }
            }

            @Override
            public void redo() throws UnexpectedUndoException {
            }
        };
        UndoManager.getInstance(project).undoableActionPerformed(action);
    }
    processor.renameElement(element, newName, usages, listener);
}
Also used : UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction)

Example 3 with UndoRefactoringElementListener

use of com.intellij.refactoring.listeners.UndoRefactoringElementListener 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)

Aggregations

UndoRefactoringElementListener (com.intellij.refactoring.listeners.UndoRefactoringElementListener)3 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)2 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)2 RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)2 PsiElement (com.intellij.psi.PsiElement)1 RefactoringElementAdapter (com.intellij.refactoring.listeners.RefactoringElementAdapter)1 RefactoringTransaction (com.intellij.refactoring.listeners.impl.RefactoringTransaction)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1