Search in sources :

Example 6 with UndoableAction

use of com.intellij.openapi.command.undo.UndoableAction 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 7 with UndoableAction

use of com.intellij.openapi.command.undo.UndoableAction in project intellij-community by JetBrains.

the class UndoableGroup method doUndoOrRedo.

private void doUndoOrRedo(final boolean isUndo) {
    final boolean wrapInBulkUpdate = myActions.size() > 50;
    // perform undo action by action, setting bulk update flag if possible
    // if multiple consecutive actions share a document, then set the bulk flag only once
    final UnexpectedUndoException[] exception = { null };
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Set<DocumentEx> bulkDocuments = new THashSet<>();
        try {
            for (final UndoableAction action : isUndo ? ContainerUtil.iterateBackward(myActions) : myActions) {
                if (wrapInBulkUpdate) {
                    DocumentEx newDocument = getDocumentToSetBulkMode(action);
                    if (newDocument == null) {
                        for (DocumentEx document : bulkDocuments) {
                            document.setInBulkUpdate(false);
                        }
                        bulkDocuments.clear();
                    } else if (bulkDocuments.add(newDocument)) {
                        newDocument.setInBulkUpdate(true);
                    }
                }
                if (isUndo) {
                    action.undo();
                } else {
                    action.redo();
                }
            }
        } catch (UnexpectedUndoException e) {
            exception[0] = e;
        } finally {
            for (DocumentEx bulkDocument : bulkDocuments) {
                bulkDocument.setInBulkUpdate(false);
            }
        }
    });
    if (exception[0] != null)
        reportUndoProblem(exception[0], isUndo);
    commitAllDocuments();
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) THashSet(gnu.trove.THashSet)

Example 8 with UndoableAction

use of com.intellij.openapi.command.undo.UndoableAction in project android by JetBrains.

the class AndroidDbManager method processAddOrRemove.

public void processAddOrRemove(final AndroidDataSource dataSource, final boolean add) {
    final Project project = myDbFacade.getProject();
    final UndoableAction action = new GlobalUndoableAction() {

        public void undo() throws UnexpectedUndoException {
            if (add) {
                removeDataSourceInner(project, dataSource);
            } else {
                addDataSourceInner(project, dataSource);
            }
        }

        public void redo() throws UnexpectedUndoException {
            if (add) {
                addDataSourceInner(project, dataSource);
            } else {
                removeDataSourceInner(project, dataSource);
            }
        }
    };
    final String commandName = add ? DatabaseMessages.message("command.name.add.data.source") : DatabaseMessages.message("command.name.remove.data.source");
    new WriteCommandAction(project, commandName) {

        protected void run(@NotNull final Result result) throws Throwable {
            action.redo();
            UndoManager.getInstance(project).undoableActionPerformed(action);
        }

        @Override
        protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
            return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) Result(com.intellij.openapi.application.Result)

Aggregations

UndoableAction (com.intellij.openapi.command.undo.UndoableAction)8 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)3 GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)3 PsiElement (com.intellij.psi.PsiElement)3 UndoConfirmationPolicy (com.intellij.openapi.command.UndoConfirmationPolicy)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Project (com.intellij.openapi.project.Project)2 UndoRefactoringElementListener (com.intellij.refactoring.listeners.UndoRefactoringElementListener)2 LocalHistoryAction (com.intellij.history.LocalHistoryAction)1 Application (com.intellij.openapi.application.Application)1 Result (com.intellij.openapi.application.Result)1 ApplicationImpl (com.intellij.openapi.application.impl.ApplicationImpl)1 UndoManager (com.intellij.openapi.command.undo.UndoManager)1 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 EmptyRunnable (com.intellij.openapi.util.EmptyRunnable)1 PsiCompiledElement (com.intellij.psi.PsiCompiledElement)1 PsiFile (com.intellij.psi.PsiFile)1 RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)1 RefactoringEventData (com.intellij.refactoring.listeners.RefactoringEventData)1