forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndoRedo.cs
More file actions
242 lines (214 loc) · 8.29 KB
/
UndoRedo.cs
File metadata and controls
242 lines (214 loc) · 8.29 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
private void RemoveEditsAfterUndo()
{
// If there is some sort of edit after an undo, forget
// any edit items that were undone.
int removeCount = _edits.Count - _undoEditIndex;
if (removeCount > 0)
{
_edits.RemoveRange(_undoEditIndex, removeCount);
if (_editGroupStart >= 0)
{
// Adjust the edit group start if we are started a group.
_editGroupStart -= removeCount;
}
}
}
private void SaveEditItem(EditItem editItem)
{
if (_statusIsErrorMessage)
{
// After an edit, clear the error message
ClearStatusMessage(render: true);
}
RemoveEditsAfterUndo();
_edits.Add(editItem);
_undoEditIndex = _edits.Count;
}
private void StartEditGroup()
{
if (_editGroupStart != -1)
{
// Nesting not supported.
throw new InvalidOperationException();
}
RemoveEditsAfterUndo();
_editGroupStart = _edits.Count;
}
private void EndEditGroup(Action<ConsoleKeyInfo?, object> instigator = null, object instigatorArg = null)
{
var groupEditCount = _edits.Count - _editGroupStart;
var groupedEditItems = _edits.GetRange(_editGroupStart, groupEditCount);
_edits.RemoveRange(_editGroupStart, groupEditCount);
SaveEditItem(GroupedEdit.Create(groupedEditItems, instigator, instigatorArg));
_editGroupStart = -1;
}
/// <summary>
/// Undo a previous edit.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void Undo(ConsoleKeyInfo? key = null, object arg = null)
{
if (_singleton._undoEditIndex > 0)
{
if (_singleton._statusIsErrorMessage)
{
// After an edit, clear the error message
_singleton.ClearStatusMessage(render: false);
}
_singleton._edits[_singleton._undoEditIndex - 1].Undo();
_singleton._undoEditIndex--;
if (_singleton._options.EditMode == EditMode.Vi && _singleton._current >= _singleton._buffer.Length)
{
_singleton._current = Math.Max(0, _singleton._buffer.Length - 1);
}
_singleton.Render();
}
else
{
Ding();
}
}
/// <summary>
/// Undo an undo.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void Redo(ConsoleKeyInfo? key = null, object arg = null)
{
if (_singleton._undoEditIndex < _singleton._edits.Count)
{
_singleton._edits[_singleton._undoEditIndex].Redo();
_singleton._undoEditIndex++;
_singleton.Render();
}
else
{
Ding();
}
}
abstract class EditItem
{
public Action<ConsoleKeyInfo?, object> _instigator = null;
public object _instigatorArg = null;
public abstract void Undo();
public abstract void Redo();
}
[DebuggerDisplay("Insert '{_insertedCharacter}' ({_insertStartPosition})")]
class EditItemInsertChar : EditItem
{
// The character inserted is not needed for undo, only for redo
private char _insertedCharacter;
private int _insertStartPosition;
public static EditItem Create(char character, int position)
{
return new EditItemInsertChar
{
_insertedCharacter = character,
_insertStartPosition = position
};
}
public override void Undo()
{
Debug.Assert(_singleton._buffer[_insertStartPosition] == _insertedCharacter, "Character to undo is not what it should be");
_singleton._buffer.Remove(_insertStartPosition, 1);
_singleton._current = _insertStartPosition;
}
public override void Redo()
{
_singleton._buffer.Insert(_insertStartPosition, _insertedCharacter);
_singleton._current++;
}
}
[DebuggerDisplay("Insert '{_insertedString}' ({_insertStartPosition})")]
class EditItemInsertString : EditItem
{
// The string inserted tells us the length to delete on undo.
// The contents of the string are only needed for redo.
private string _insertedString;
private int _insertStartPosition;
public static EditItem Create(string str, int position)
{
return new EditItemInsertString
{
_insertedString = str,
_insertStartPosition = position
};
}
public override void Undo()
{
Debug.Assert(_singleton._buffer.ToString(_insertStartPosition, _insertedString.Length).Equals(_insertedString),
"Character to undo is not what it should be");
_singleton._buffer.Remove(_insertStartPosition, _insertedString.Length);
_singleton._current = _insertStartPosition;
}
public override void Redo()
{
_singleton._buffer.Insert(_insertStartPosition, _insertedString);
_singleton._current += _insertedString.Length;
}
}
[DebuggerDisplay("Delete '{_deletedString}' ({_deleteStartPosition})")]
class EditItemDelete : EditItem
{
private string _deletedString;
private int _deleteStartPosition;
public static EditItem Create(string str, int position, Action<ConsoleKeyInfo?, object> instigator = null, object instigatorArg = null)
{
return new EditItemDelete
{
_deletedString = str,
_deleteStartPosition = position,
_instigator = instigator,
_instigatorArg = instigatorArg
};
}
public override void Undo()
{
_singleton._buffer.Insert(_deleteStartPosition, _deletedString);
_singleton._current = _deleteStartPosition + _deletedString.Length;
}
public override void Redo()
{
_singleton._buffer.Remove(_deleteStartPosition, _deletedString.Length);
_singleton._current = _deleteStartPosition;
}
}
class GroupedEdit : EditItem
{
internal List<EditItem> _groupedEditItems;
public static EditItem Create(List<EditItem> groupedEditItems, Action<ConsoleKeyInfo?, object> instigator = null, object instigatorArg = null)
{
return new GroupedEdit
{
_groupedEditItems = groupedEditItems,
_instigator = instigator,
_instigatorArg = instigatorArg
};
}
public override void Undo()
{
for (int i = _groupedEditItems.Count - 1; i >= 0; i--)
{
_groupedEditItems[i].Undo();
}
}
public override void Redo()
{
foreach (var editItem in _groupedEditItems)
{
editItem.Redo();
}
}
}
}
}