Search in sources :

Example 1 with BasicUndoableAction

use of com.intellij.openapi.command.undo.BasicUndoableAction 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 2 with BasicUndoableAction

use of com.intellij.openapi.command.undo.BasicUndoableAction 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 3 with BasicUndoableAction

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

the class LRUPopupBuilder method changeLanguageWithUndo.

private static void changeLanguageWithUndo(@NotNull Project project, @NotNull final Language t, @NotNull final VirtualFile[] sortedFiles, @NotNull final PerFileMappings<Language> mappings) throws UnexpectedUndoException {
    ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(sortedFiles);
    if (status.hasReadonlyFiles())
        return;
    final Set<VirtualFile> matchedExtensions = ContainerUtil.newLinkedHashSet();
    final Map<VirtualFile, Language> oldMapping = ContainerUtil.newHashMap();
    for (VirtualFile file : sortedFiles) {
        oldMapping.put(file, mappings.getMapping(file));
        if (ScratchUtil.hasMatchingExtension(project, file)) {
            matchedExtensions.add(file);
        }
    }
    BasicUndoableAction action = new BasicUndoableAction(sortedFiles) {

        @Override
        public void undo() throws UnexpectedUndoException {
            for (VirtualFile file : sortedFiles) {
                mappings.setMapping(file, oldMapping.get(file));
            }
        }

        @Override
        public void redo() throws UnexpectedUndoException {
            for (VirtualFile file : sortedFiles) {
                mappings.setMapping(file, t);
            }
        }
    };
    action.redo();
    UndoManager.getInstance(project).undoableActionPerformed(action);
    for (VirtualFile file : matchedExtensions) {
        try {
            ScratchUtil.updateFileExtension(project, file);
        } catch (IOException ignored) {
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Language(com.intellij.lang.Language) IOException(java.io.IOException) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) ReadonlyStatusHandler(com.intellij.openapi.vfs.ReadonlyStatusHandler)

Example 4 with BasicUndoableAction

use of com.intellij.openapi.command.undo.BasicUndoableAction 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 5 with BasicUndoableAction

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

the class TemplateState method start.

public void start(@NotNull TemplateImpl template, @Nullable final PairProcessor<String, String> processor, @Nullable Map<String, String> predefinedVarValues) {
    LOG.assertTrue(!myStarted, "Already started");
    myStarted = true;
    final PsiFile file = getPsiFile();
    myTemplate = substituteTemplate(file, myEditor.getCaretModel().getOffset(), template);
    myProcessor = processor;
    DocumentReference[] refs = myDocument != null ? new DocumentReference[] { DocumentReferenceManager.getInstance().create(myDocument) } : null;
    UndoManager.getInstance(myProject).undoableActionPerformed(new BasicUndoableAction(refs) {

        @Override
        public void undo() {
            if (!isDisposed()) {
                fireTemplateCancelled();
                LookupManager.getInstance(myProject).hideActiveLookup();
                int oldVar = myCurrentVariableNumber;
                setCurrentVariableNumber(-1);
                currentVariableChanged(oldVar);
            }
        }

        @Override
        public void redo() {
        //TODO:
        // throw new UnexpectedUndoException("Not implemented");
        }
    });
    myTemplateIndented = false;
    myCurrentVariableNumber = -1;
    mySegments = new TemplateSegments(myEditor);
    myPrevTemplate = myTemplate;
    //myArgument = argument;
    myPredefinedVariableValues = predefinedVarValues;
    if (myTemplate.isInline()) {
        int caretOffset = myEditor.getCaretModel().getOffset();
        myTemplateRange = myDocument.createRangeMarker(caretOffset, caretOffset + myTemplate.getTemplateText().length());
    } else {
        preprocessTemplate(file, myEditor.getCaretModel().getOffset(), myTemplate.getTemplateText());
        int caretOffset = myEditor.getCaretModel().getOffset();
        myTemplateRange = myDocument.createRangeMarker(caretOffset, caretOffset);
    }
    myTemplateRange.setGreedyToLeft(true);
    myTemplateRange.setGreedyToRight(true);
    processAllExpressions(myTemplate);
}
Also used : PsiFile(com.intellij.psi.PsiFile) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Aggregations

BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)9 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)3 DocumentReference (com.intellij.openapi.command.undo.DocumentReference)2 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 UndoRefactoringElementListener (com.intellij.refactoring.listeners.UndoRefactoringElementListener)2 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)2 Language (com.intellij.lang.Language)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Editor (com.intellij.openapi.editor.Editor)1 ReadonlyStatusHandler (com.intellij.openapi.vfs.ReadonlyStatusHandler)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)1 RefactoringTransaction (com.intellij.refactoring.listeners.impl.RefactoringTransaction)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 SmartList (com.intellij.util.SmartList)1 AnswerPlaceholderSubtaskInfo (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo)1