Search in sources :

Example 1 with DocumentReference

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

the class UndoRedoStacksHolder method clearStacks.

void clearStacks(boolean clearGlobal, @NotNull Set<DocumentReference> refs) {
    for (LinkedList<UndoableGroup> each : getAffectedStacks(clearGlobal, refs)) {
        while (!each.isEmpty()) {
            clearStacksFrom(each.getLast());
        }
    }
    Set<DocumentReference> stacksToDrop = new THashSet<>();
    for (Map.Entry<DocumentReference, LinkedList<UndoableGroup>> each : myDocumentStacks.entrySet()) {
        if (each.getValue().isEmpty())
            stacksToDrop.add(each.getKey());
    }
    for (DocumentReference each : stacksToDrop) {
        myDocumentStacks.remove(each);
    }
    cleanWeaklyTrackedEmptyStacks(myDocumentsWithStacks);
    cleanWeaklyTrackedEmptyStacks(myNonlocalVirtualFilesWithStacks);
}
Also used : HashMap(com.intellij.util.containers.HashMap) THashSet(gnu.trove.THashSet) DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Example 2 with DocumentReference

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

the class UndoableGroup method getDocumentToSetBulkMode.

private static DocumentEx getDocumentToSetBulkMode(UndoableAction action) {
    // not allowed in bulk update.
    if (!(action instanceof EditorChangeAction))
        return null;
    //noinspection ConstantConditions
    DocumentReference newDocumentRef = action.getAffectedDocuments()[0];
    if (newDocumentRef == null)
        return null;
    VirtualFile file = newDocumentRef.getFile();
    if (file != null && !file.isValid())
        return null;
    return (DocumentEx) newDocumentRef.getDocument();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Example 3 with DocumentReference

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

the class FinishMarkAction method finish.

public static void finish(final Project project, final Editor editor, @Nullable final StartMarkAction startAction) {
    if (startAction == null)
        return;
    CommandProcessor.getInstance().executeCommand(project, () -> {
        DocumentReference reference = DocumentReferenceManager.getInstance().create(editor.getDocument());
        UndoManager.getInstance(project).undoableActionPerformed(new FinishMarkAction(reference, startAction));
        StartMarkAction.markFinished(project);
    }, "finish", null);
}
Also used : DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Example 4 with DocumentReference

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

the class UndoRedo method execute.

public boolean execute(boolean drop, boolean isInsideStartFinishGroup) {
    if (!myUndoableGroup.isUndoable()) {
        reportCannotUndo(CommonBundle.message("cannot.undo.error.contains.nonundoable.changes.message"), myUndoableGroup.getAffectedDocuments());
        return false;
    }
    Set<DocumentReference> clashing = getStackHolder().collectClashingActions(myUndoableGroup);
    if (!clashing.isEmpty()) {
        reportCannotUndo(CommonBundle.message("cannot.undo.error.other.affected.files.changed.message"), clashing);
        return false;
    }
    if (!isInsideStartFinishGroup && myUndoableGroup.shouldAskConfirmation(isRedo())) {
        if (!askUser())
            return false;
    } else {
        if (restore(getBeforeState(), true)) {
            setBeforeState(new EditorAndState(myEditor, myEditor.getState(FileEditorStateLevel.UNDO)));
            return true;
        }
    }
    Collection<VirtualFile> readOnlyFiles = collectReadOnlyAffectedFiles();
    if (!readOnlyFiles.isEmpty()) {
        final Project project = myManager.getProject();
        final VirtualFile[] files = VfsUtil.toVirtualFileArray(readOnlyFiles);
        if (project == null) {
            return false;
        }
        final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(files);
        if (operationStatus.hasReadonlyFiles()) {
            return false;
        }
    }
    Collection<Document> readOnlyDocuments = collectReadOnlyDocuments();
    if (!readOnlyDocuments.isEmpty()) {
        for (Document document : readOnlyDocuments) {
            document.fireReadOnlyModificationAttempt();
        }
        return false;
    }
    getStackHolder().removeFromStacks(myUndoableGroup);
    if (!drop) {
        getReverseStackHolder().addToStacks(myUndoableGroup);
    }
    performAction();
    restore(getAfterState(), false);
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Document(com.intellij.openapi.editor.Document) DocumentReference(com.intellij.openapi.command.undo.DocumentReference) ReadonlyStatusHandler(com.intellij.openapi.vfs.ReadonlyStatusHandler)

Example 5 with DocumentReference

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

DocumentReference (com.intellij.openapi.command.undo.DocumentReference)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 NotNull (org.jetbrains.annotations.NotNull)3 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)2 THashSet (gnu.trove.THashSet)2 UndoManager (com.intellij.openapi.command.undo.UndoManager)1 Document (com.intellij.openapi.editor.Document)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 Project (com.intellij.openapi.project.Project)1 ReadonlyStatusHandler (com.intellij.openapi.vfs.ReadonlyStatusHandler)1 PsiFile (com.intellij.psi.PsiFile)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 RenameJavaVariableProcessor (com.intellij.refactoring.rename.RenameJavaVariableProcessor)1 RenameXmlAttributeProcessor (com.intellij.refactoring.rename.RenameXmlAttributeProcessor)1 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)1 HashMap (com.intellij.util.containers.HashMap)1 File (java.io.File)1