Search in sources :

Example 1 with UndoableAction

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);
}
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 2 with UndoableAction

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;
}
Also used : UndoableAction(com.intellij.openapi.command.undo.UndoableAction)

Example 3 with UndoableAction

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;
}
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 4 with UndoableAction

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();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) 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) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 5 with UndoableAction

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"));
        }
    }
}
Also used : ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl) RefactoringListenerManagerImpl(com.intellij.refactoring.listeners.impl.RefactoringListenerManagerImpl) ThrowableRunnable(com.intellij.util.ThrowableRunnable) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) LocalHistoryAction(com.intellij.history.LocalHistoryAction) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) MultiMap(com.intellij.util.containers.MultiMap) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

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