Search in sources :

Example 1 with UnexpectedUndoException

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

the class DynamicDialog method doOKAction.

@Override
protected void doOKAction() {
    super.doOKAction();
    mySettings.setContainingClassName((String) myClassComboBox.getSelectedItem());
    mySettings.setStatic(myStaticCheckBox.isSelected());
    GrTypeElement typeElement = getEnteredTypeName();
    if (typeElement == null) {
        mySettings.setType(CommonClassNames.JAVA_LANG_OBJECT);
    } else {
        PsiType type = typeElement.getType();
        if (type instanceof PsiPrimitiveType) {
            type = TypesUtil.boxPrimitiveType(type, typeElement.getManager(), ProjectScope.getAllScope(myProject));
        }
        final String typeQualifiedName = type.getCanonicalText();
        if (typeQualifiedName != null) {
            mySettings.setType(typeQualifiedName);
        } else {
            mySettings.setType(type.getPresentableText());
        }
    }
    final Document document = PsiDocumentManager.getInstance(myProject).getDocument(myContext.getContainingFile());
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {

            @Override
            public void undo() throws UnexpectedUndoException {
                final DItemElement itemElement;
                if (mySettings.isMethod()) {
                    final List<ParamInfo> myPairList = mySettings.getParams();
                    final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairList);
                    itemElement = myDynamicManager.findConcreteDynamicMethod(mySettings.getContainingClassName(), mySettings.getName(), argumentsTypes);
                } else {
                    itemElement = myDynamicManager.findConcreteDynamicProperty(mySettings.getContainingClassName(), mySettings.getName());
                }
                if (itemElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                final DClassElement classElement = myDynamicManager.getClassElementByItem(itemElement);
                if (classElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                removeElement(itemElement);
                if (classElement.getMethods().isEmpty() && classElement.getProperties().isEmpty()) {
                    myDynamicManager.removeClassElement(classElement);
                }
            }

            @Override
            public void redo() throws UnexpectedUndoException {
                addElement(mySettings);
            }
        });
        addElement(mySettings);
    }, "Add dynamic element", null);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) List(java.util.List) Document(com.intellij.openapi.editor.Document) DItemElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)

Example 2 with UnexpectedUndoException

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

the class CCChangePlaceholderVisibility method performAnswerPlaceholderAction.

@Override
protected void performAnswerPlaceholderAction(@NotNull CCState state) {
    AnswerPlaceholder placeholder = state.getAnswerPlaceholder();
    if (placeholder == null) {
        return;
    }
    EduUtils.runUndoableAction(state.getProject(), getName(), new BasicUndoableAction(state.getEditor().getDocument()) {

        @Override
        public void undo() throws UnexpectedUndoException {
            setVisible(placeholder, isVisible(), state);
        }

        @Override
        public void redo() throws UnexpectedUndoException {
            setVisible(placeholder, !isVisible(), state);
        }
    }, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction)

Example 3 with UnexpectedUndoException

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

the class CCDeleteAnswerPlaceholder method deletePlaceholder.

private static void deletePlaceholder(@NotNull CCState state) {
    Project project = state.getProject();
    TaskFile taskFile = state.getTaskFile();
    AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
    EduUtils.runUndoableAction(project, "Delete Answer Placeholder", new CCAddAnswerPlaceholder.AddAction(answerPlaceholder, taskFile, state.getEditor()) {

        @Override
        public void undo() throws UnexpectedUndoException {
            super.redo();
        }

        @Override
        public void redo() throws UnexpectedUndoException {
            super.undo();
        }
    });
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException)

Example 4 with UnexpectedUndoException

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

the class SuppressForTestsScopeFix method doFix.

@Override
protected void doFix(final Project project, ProblemDescriptor descriptor) {
    addRemoveTestsScope(project, true);
    final VirtualFile vFile = descriptor.getPsiElement().getContainingFile().getVirtualFile();
    UndoManager.getInstance(project).undoableActionPerformed(new BasicUndoableAction(vFile) {

        @Override
        public void undo() throws UnexpectedUndoException {
            addRemoveTestsScope(project, false);
        }

        @Override
        public void redo() throws UnexpectedUndoException {
            addRemoveTestsScope(project, true);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction)

Example 5 with UnexpectedUndoException

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

Aggregations

UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)6 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)3 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)3 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)2 GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)1 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 AnswerPlaceholderSubtaskInfo (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo)1 THashSet (gnu.trove.THashSet)1 List (java.util.List)1 DClassElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement)1 DItemElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)1 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)1