Search in sources :

Example 1 with UndoManager

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

the class GroovyOptimizeImportsFix method invokeOnTheFlyImportOptimizer.

public static void invokeOnTheFlyImportOptimizer(@NotNull final Runnable runnable, @NotNull final PsiFile file, @NotNull final Editor editor) {
    final long stamp = editor.getDocument().getModificationStamp();
    Project project = file.getProject();
    TransactionGuard.submitTransaction(project, () -> {
        if (editor.isDisposed() || editor.getDocument().getModificationStamp() != stamp)
            return;
        //no need to optimize imports on the fly during undo/redo
        final UndoManager undoManager = UndoManager.getInstance(project);
        if (undoManager.isUndoInProgress() || undoManager.isRedoInProgress())
            return;
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        String beforeText = file.getText();
        final long oldStamp = editor.getDocument().getModificationStamp();
        DocumentUtil.writeInRunUndoTransparentAction(runnable);
        if (oldStamp != editor.getDocument().getModificationStamp()) {
            String afterText = file.getText();
            if (Comparing.strEqual(beforeText, afterText)) {
                String path = file.getViewProvider().getVirtualFile().getPath();
                LOG.error("Import optimizer  hasn't optimized any imports", new Attachment(path, afterText));
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) UndoManager(com.intellij.openapi.command.undo.UndoManager) Attachment(com.intellij.openapi.diagnostic.Attachment)

Example 2 with UndoManager

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

the class DiffUtil method putNonundoableOperation.

public static void putNonundoableOperation(@Nullable Project project, @NotNull Document document) {
    UndoManager undoManager = project != null ? UndoManager.getInstance(project) : UndoManager.getGlobalInstance();
    if (undoManager != null) {
        DocumentReference ref = DocumentReferenceManager.getInstance().create(document);
        undoManager.nonundoableActionPerformed(ref, false);
    }
}
Also used : UndoManager(com.intellij.openapi.command.undo.UndoManager) DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Example 3 with UndoManager

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

the class CommandProcessorImpl method finishCommand.

@Override
public void finishCommand(final Project project, final Object command, final Throwable throwable) {
    if (myCurrentCommand != command)
        return;
    final boolean failed;
    try {
        if (throwable instanceof AbnormalCommandTerminationException) {
            final AbnormalCommandTerminationException rollback = (AbnormalCommandTerminationException) throwable;
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                throw new RuntimeException(rollback);
            }
            failed = true;
        } else if (throwable != null) {
            failed = true;
            if (throwable instanceof Error) {
                throw (Error) throwable;
            } else if (throwable instanceof RuntimeException)
                throw (RuntimeException) throwable;
            CommandLog.LOG.error(throwable);
        } else {
            failed = false;
        }
    } finally {
        super.finishCommand(project, command, throwable);
    }
    if (failed) {
        if (project != null) {
            FileEditor editor = new FocusBasedCurrentEditorProvider().getCurrentEditor();
            final UndoManager undoManager = UndoManager.getInstance(project);
            if (undoManager.isUndoAvailable(editor)) {
                undoManager.undo(editor);
            }
        }
        Messages.showErrorDialog(project, "Cannot perform operation. Too complex, sorry.", "Failed to Perform Operation");
    }
}
Also used : FileEditor(com.intellij.openapi.fileEditor.FileEditor) UndoManager(com.intellij.openapi.command.undo.UndoManager) AbnormalCommandTerminationException(com.intellij.openapi.command.AbnormalCommandTerminationException)

Example 4 with UndoManager

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

the class ChangeFileEncodingAction method chosen.

// returns true if charset was changed, false if failed
protected boolean chosen(final Document document, final Editor editor, @Nullable final VirtualFile virtualFile, byte[] bytes, @NotNull final Charset charset) {
    if (virtualFile == null)
        return false;
    String text = document.getText();
    EncodingUtil.Magic8 isSafeToConvert = EncodingUtil.isSafeToConvertTo(virtualFile, text, bytes, charset);
    EncodingUtil.Magic8 isSafeToReload = EncodingUtil.isSafeToReloadIn(virtualFile, text, bytes, charset);
    final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
    final Charset oldCharset = virtualFile.getCharset();
    final Runnable undo;
    final Runnable redo;
    if (isSafeToConvert == EncodingUtil.Magic8.ABSOLUTELY && isSafeToReload == EncodingUtil.Magic8.ABSOLUTELY) {
        //change and forget
        undo = () -> EncodingManager.getInstance().setEncoding(virtualFile, oldCharset);
        redo = () -> EncodingManager.getInstance().setEncoding(virtualFile, charset);
    } else {
        IncompatibleEncodingDialog dialog = new IncompatibleEncodingDialog(virtualFile, charset, isSafeToReload, isSafeToConvert);
        dialog.show();
        if (dialog.getExitCode() == IncompatibleEncodingDialog.RELOAD_EXIT_CODE) {
            undo = () -> EncodingUtil.reloadIn(virtualFile, oldCharset);
            redo = () -> EncodingUtil.reloadIn(virtualFile, charset);
        } else if (dialog.getExitCode() == IncompatibleEncodingDialog.CONVERT_EXIT_CODE) {
            undo = () -> EncodingUtil.saveIn(document, editor, virtualFile, oldCharset);
            redo = () -> EncodingUtil.saveIn(document, editor, virtualFile, charset);
        } else {
            return false;
        }
    }
    final UndoableAction action = new GlobalUndoableAction(virtualFile) {

        @Override
        public void undo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(undo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }

        @Override
        public void redo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(redo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }
    };
    redo.run();
    CommandProcessor.getInstance().executeCommand(project, () -> {
        UndoManager undoManager = project == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(project);
        undoManager.undoableActionPerformed(action);
    }, "Change encoding for '" + virtualFile.getName() + "'", null, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) UndoManager(com.intellij.openapi.command.undo.UndoManager) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) Charset(java.nio.charset.Charset) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) Application(com.intellij.openapi.application.Application)

Example 5 with UndoManager

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

the class FileEncodingTest method globalUndo.

private void globalUndo() {
    UndoManager myManager = UndoManager.getInstance(getProject());
    assertTrue("undo is not available", myManager.isUndoAvailable(null));
    myManager.undo(null);
}
Also used : UndoManager(com.intellij.openapi.command.undo.UndoManager)

Aggregations

UndoManager (com.intellij.openapi.command.undo.UndoManager)12 Project (com.intellij.openapi.project.Project)4 FileEditor (com.intellij.openapi.fileEditor.FileEditor)3 DataContext (com.intellij.openapi.actionSystem.DataContext)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 Template (com.intellij.codeInsight.template.Template)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Presentation (com.intellij.openapi.actionSystem.Presentation)1 Application (com.intellij.openapi.application.Application)1 AbnormalCommandTerminationException (com.intellij.openapi.command.AbnormalCommandTerminationException)1 DocumentReference (com.intellij.openapi.command.undo.DocumentReference)1 GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)1 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)1 Attachment (com.intellij.openapi.diagnostic.Attachment)1 Document (com.intellij.openapi.editor.Document)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 PsiFile (com.intellij.psi.PsiFile)1 Charset (java.nio.charset.Charset)1