use of com.intellij.openapi.command.undo.UndoableAction 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);
}
use of com.intellij.openapi.command.undo.UndoableAction in project intellij-community by JetBrains.
the class UndoableGroup method isInsideStartFinishGroup.
boolean isInsideStartFinishGroup(boolean isUndo, boolean isInsideStartFinishGroup) {
final List<FinishMarkAction> finishMarks = new ArrayList<>();
final List<StartMarkAction> startMarks = new ArrayList<>();
for (UndoableAction action : myActions) {
if (action instanceof StartMarkAction) {
startMarks.add((StartMarkAction) action);
} else if (action instanceof FinishMarkAction) {
finishMarks.add((FinishMarkAction) action);
}
}
final int startNmb = startMarks.size();
final int finishNmb = finishMarks.size();
if (startNmb != finishNmb) {
if (isUndo) {
if (finishNmb > startNmb) {
return true;
} else {
return false;
}
} else {
if (startNmb > finishNmb) {
return true;
} else {
return false;
}
}
}
return isInsideStartFinishGroup;
}
use of com.intellij.openapi.command.undo.UndoableAction 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.UndoableAction in project intellij-community by JetBrains.
the class Configuration method replaceInjectionsWithUndo.
public static <T> void replaceInjectionsWithUndo(final Project project, final T add, final T remove, final List<? extends PsiElement> psiElementsToRemove, final PairProcessor<T, T> actualProcessor) {
final UndoableAction action = new GlobalUndoableAction() {
@Override
public void undo() {
actualProcessor.process(remove, add);
}
@Override
public void redo() {
actualProcessor.process(add, remove);
}
};
final List<PsiFile> psiFiles = ContainerUtil.mapNotNull(psiElementsToRemove, o -> o instanceof PsiCompiledElement ? null : o.getContainingFile());
new WriteCommandAction.Simple(project, "Language Injection Configuration Update", PsiUtilCore.toPsiFileArray(psiFiles)) {
@Override
public void run() {
for (PsiElement annotation : psiElementsToRemove) {
if (!annotation.isValid())
continue;
annotation.delete();
}
actualProcessor.process(add, remove);
UndoManager.getInstance(project).undoableActionPerformed(action);
}
@Override
protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
}
}.execute();
}
use of com.intellij.openapi.command.undo.UndoableAction in project intellij-community by JetBrains.
the class BaseRefactoringProcessor method doRefactoring.
private void doRefactoring(@NotNull final Collection<UsageInfo> usageInfoSet) {
for (Iterator<UsageInfo> iterator = usageInfoSet.iterator(); iterator.hasNext(); ) {
UsageInfo usageInfo = iterator.next();
final PsiElement element = usageInfo.getElement();
if (element == null || !isToBeChanged(usageInfo)) {
iterator.remove();
}
}
LocalHistoryAction action = LocalHistory.getInstance().startAction(getCommandName());
final UsageInfo[] writableUsageInfos = usageInfoSet.toArray(new UsageInfo[usageInfoSet.size()]);
try {
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
RefactoringListenerManagerImpl listenerManager = (RefactoringListenerManagerImpl) RefactoringListenerManager.getInstance(myProject);
myTransaction = listenerManager.startTransaction();
final Map<RefactoringHelper, Object> preparedData = new LinkedHashMap<>();
final Runnable prepareHelpersRunnable = new Runnable() {
@Override
public void run() {
for (final RefactoringHelper helper : Extensions.getExtensions(RefactoringHelper.EP_NAME)) {
Object operation = ApplicationManager.getApplication().runReadAction(new Computable<Object>() {
@Override
public Object compute() {
return helper.prepareOperation(writableUsageInfos);
}
});
preparedData.put(helper, operation);
}
}
};
ProgressManager.getInstance().runProcessWithProgressSynchronously(prepareHelpersRunnable, "Prepare ...", false, myProject);
Runnable performRefactoringRunnable = () -> {
final String refactoringId = getRefactoringId();
if (refactoringId != null) {
RefactoringEventData data = getBeforeData();
if (data != null) {
data.addUsages(usageInfoSet);
}
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, data);
}
try {
if (refactoringId != null) {
UndoableAction action1 = new UndoRefactoringAction(myProject, refactoringId);
UndoManager.getInstance(myProject).undoableActionPerformed(action1);
}
performRefactoring(writableUsageInfos);
} finally {
if (refactoringId != null) {
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, getAfterData(writableUsageInfos));
}
}
};
ApplicationImpl app = (ApplicationImpl) ApplicationManagerEx.getApplicationEx();
if (Registry.is("run.refactorings.under.progress")) {
app.runWriteActionWithProgressInDispatchThread(getCommandName(), myProject, null, null, indicator -> performRefactoringRunnable.run());
} else {
app.runWriteAction(performRefactoringRunnable);
}
DumbService.getInstance(myProject).completeJustSubmittedTasks();
for (Map.Entry<RefactoringHelper, Object> e : preparedData.entrySet()) {
//noinspection unchecked
e.getKey().performOperation(myProject, e.getValue());
}
myTransaction.commit();
app.runWriteAction(() -> performPsiSpoilingRefactoring());
} finally {
action.finish();
}
int count = writableUsageInfos.length;
if (count > 0) {
StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.refactoring.result", count));
} else {
if (!isPreviewUsages(writableUsageInfos)) {
StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.noUsages"));
}
}
}
Aggregations