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));
}
}
});
}
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);
}
}
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");
}
}
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;
}
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);
}
Aggregations