blob: 5895026caaf5af206d9709af5c7e3095ef19c0a1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.command.impl;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.command.undo.DocumentReference;
import com.intellij.openapi.command.undo.DocumentReferenceManager;
import com.intellij.openapi.command.undo.UndoConstants;
import com.intellij.openapi.command.undo.UndoManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;
public class DocumentUndoProvider implements Disposable {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.command.impl.DocumentUndoProvider");
private static final Key<Boolean> UNDOING_EDITOR_CHANGE = Key.create("DocumentUndoProvider.UNDOING_EDITOR_CHANGE");
private final Project myProject;
DocumentUndoProvider(Project project) {
MyEditorDocumentListener documentListener = new MyEditorDocumentListener();
myProject = project;
EditorFactory.getInstance().getEventMulticaster().addDocumentListener(documentListener, this);
}
@Override
public void dispose() {
}
private UndoManagerImpl getUndoManager() {
return (UndoManagerImpl)(myProject == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(myProject));
}
public static void startDocumentUndo(@Nullable Document doc) {
if (doc != null) doc.putUserData(UNDOING_EDITOR_CHANGE, Boolean.TRUE);
}
public static void finishDocumentUndo(@Nullable Document doc) {
if (doc != null) doc.putUserData(UNDOING_EDITOR_CHANGE, null);
}
private class MyEditorDocumentListener extends DocumentAdapter {
@Override
public void documentChanged(final DocumentEvent e) {
Document document = e.getDocument();
// if we don't ignore copy's events, we will receive notification
// for the same event twice (from original document too)
// and undo will work incorrectly
if (UndoManagerImpl.isCopy(document)) return;
if (allEditorsAreViewersFor(document)) return;
if (!shouldRecordActions(document)) return;
UndoManagerImpl undoManager = getUndoManager();
if (!undoManager.isActive() || !isUndoable(document)) {
registerNonUndoableAction(document);
return;
}
if (undoManager.isUndoInProgress() || undoManager.isRedoInProgress()) {
if (document.getUserData(UNDOING_EDITOR_CHANGE) != Boolean.TRUE) {
LOG.error("Do not change documents during undo as it will break undo sequence.");
}
}
registerUndoableAction(e);
}
private boolean shouldRecordActions(final Document document) {
if (document.getUserData(UndoConstants.DONT_RECORD_UNDO) == Boolean.TRUE) return false;
final VirtualFile vFile = FileDocumentManager.getInstance().getFile(document);
return vFile == null || vFile.getUserData(UndoConstants.DONT_RECORD_UNDO) != Boolean.TRUE;
}
private boolean allEditorsAreViewersFor(Document document) {
Editor[] editors = EditorFactory.getInstance().getEditors(document);
if (editors.length == 0) return false;
for (Editor editor : editors) {
if (!editor.isViewer()) return false;
}
return true;
}
private void registerUndoableAction(DocumentEvent e) {
getUndoManager().undoableActionPerformed(new EditorChangeAction(e));
}
private void registerNonUndoableAction(final Document document) {
DocumentReference ref = DocumentReferenceManager.getInstance().create(document);
getUndoManager().nonundoableActionPerformed(ref, false);
}
private boolean isUndoable(Document document) {
if (!UndoManagerImpl.isRefresh()) return true;
return getUndoManager().isUndoOrRedoAvailable(DocumentReferenceManager.getInstance().create(document));
}
}
}
|