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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
// Scintilla source code edit control
/** @file UndoHistory.cxx
** Manages undo for the document.
**/
// Copyright 1998-2024 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <cstddef>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cstdarg>
#include <climits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include <optional>
#include <algorithm>
#include <memory>
#include "ScintillaTypes.h"
#include "Debugging.h"
#include "Position.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"
#include "SparseVector.h"
#include "ChangeHistory.h"
#include "CellBuffer.h"
#include "UndoHistory.h"
namespace Scintilla::Internal {
UndoActionType::UndoActionType() noexcept : at(ActionType::insert), mayCoalesce(false) {
}
void UndoActions::resize(size_t length) {
types.resize(length);
positions.resize(length);
lengths.resize(length);
}
size_t UndoActions::size() const noexcept {
return types.size();
}
void UndoActions::Create(size_t index, ActionType at_, Sci::Position position_, Sci::Position lenData_, bool mayCoalesce_) noexcept {
types[index].at = at_;
types[index].mayCoalesce = mayCoalesce_;
positions[index] = position_;
lengths[index] = lenData_;
}
const char *ScrapStack::Push(const char *text, size_t length) {
if (current < stack.length()) {
stack.resize(current);
}
stack.append(text, length);
current = stack.length();
return stack.data() + current - length;
}
void ScrapStack::SetCurrent(size_t position) noexcept {
current = position;
}
void ScrapStack::MoveForward(size_t length) noexcept {
if ((current + length) <= stack.length()) {
current += length;
}
}
void ScrapStack::MoveBack(size_t length) noexcept {
if (current >= length) {
current -= length;
}
}
const char *ScrapStack::CurrentText() const noexcept {
return stack.data() + current;
}
const char *ScrapStack::TextAt(size_t position) const noexcept {
return stack.data() + position;
}
// The undo history stores a sequence of user operations that represent the user's view of the
// commands executed on the text.
// Each user operation contains a sequence of text insertion and text deletion actions.
// All the user operations are stored in a list of individual actions with 'start' actions used
// as delimiters between user operations.
// Initially there is one start action in the history.
// As each action is performed, it is recorded in the history. The action may either become
// part of the current user operation or may start a new user operation. If it is to be part of the
// current operation, then it overwrites the current last action. If it is to be part of a new
// operation, it is appended after the current last action.
// After writing the new action, a new start action is appended at the end of the history.
// The decision of whether to start a new user operation is based upon two factors. If a
// compound operation has been explicitly started by calling BeginUndoAction and no matching
// EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
// operation. If there is no outstanding BeginUndoAction call then a new operation is started
// unless it looks as if the new action is caused by the user typing or deleting a stream of text.
// Sequences that look like typing or deletion are coalesced into a single user operation.
UndoHistory::UndoHistory() {
actions.resize(3);
scraps = std::make_unique<ScrapStack>();
actions.Create(currentAction, ActionType::start);
}
UndoHistory::~UndoHistory() noexcept = default;
void UndoHistory::EnsureUndoRoom() {
// Have to test that there is room for 2 more actions in the array
// as two actions may be created by the calling function
if (static_cast<size_t>(currentAction) >= (actions.size() - 2)) {
// Run out of undo nodes so extend the array
actions.resize(actions.size() * 2);
}
}
const char *UndoHistory::AppendAction(ActionType at, Sci::Position position, const char *data, Sci::Position lengthData,
bool &startSequence, bool mayCoalesce) {
EnsureUndoRoom();
//Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
//Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
// actions[currentAction - 1].position, actions[currentAction - 1].lenData);
if (currentAction < savePoint) {
savePoint = -1;
if (!detach) {
detach = currentAction;
}
} else if (detach && (*detach > currentAction)) {
detach = currentAction;
}
const int oldCurrentAction = currentAction;
if (currentAction >= 1) {
if (0 == undoSequenceDepth) {
// Top level actions may not always be coalesced
ptrdiff_t targetAct = currentAction - 1;
// Container actions may forward the coalesce state of Scintilla Actions.
while ((targetAct > 0) && (actions.types[targetAct].at == ActionType::container) && actions.types[targetAct].mayCoalesce) {
targetAct--;
}
// See if current action can be coalesced into previous action
// Will work if both are inserts or deletes and position is same
if ((currentAction == savePoint) || (currentAction == tentativePoint)) {
currentAction++;
} else if (!actions.types[currentAction].mayCoalesce) {
// Not allowed to coalesce if this set
currentAction++;
} else if (!mayCoalesce || !actions.types[targetAct].mayCoalesce) {
currentAction++;
} else if (at == ActionType::container || actions.types[currentAction].at == ActionType::container) {
; // A coalescible containerAction
} else if ((at != actions.types[targetAct].at) && (actions.types[targetAct].at != ActionType::start)) {
currentAction++;
} else if ((at == ActionType::insert) &&
(position != (actions.positions[targetAct] + actions.lengths[targetAct]))) {
// Insertions must be immediately after to coalesce
currentAction++;
} else if (at == ActionType::remove) {
if ((lengthData == 1) || (lengthData == 2)) {
if ((position + lengthData) == actions.positions[targetAct]) {
; // Backspace -> OK
} else if (position == actions.positions[targetAct]) {
; // Delete -> OK
} else {
// Removals must be at same position to coalesce
currentAction++;
}
} else {
// Removals must be of one character to coalesce
currentAction++;
}
} else {
// Action coalesced.
}
} else {
// Actions not at top level are always coalesced unless this is after return to top level
if (!actions.types[currentAction].mayCoalesce)
currentAction++;
}
} else {
currentAction++;
}
startSequence = oldCurrentAction != currentAction;
const char *dataNew = lengthData ? scraps->Push(data, lengthData) : nullptr;
actions.Create(currentAction, at, position, lengthData, mayCoalesce);
currentAction++;
actions.Create(currentAction, ActionType::start);
maxAction = currentAction;
return dataNew;
}
void UndoHistory::BeginUndoAction() {
EnsureUndoRoom();
if (undoSequenceDepth == 0) {
if (actions.types[currentAction].at != ActionType::start) {
currentAction++;
actions.Create(currentAction, ActionType::start);
maxAction = currentAction;
}
actions.types[currentAction].mayCoalesce = false;
}
undoSequenceDepth++;
}
void UndoHistory::EndUndoAction() {
PLATFORM_ASSERT(undoSequenceDepth > 0);
EnsureUndoRoom();
undoSequenceDepth--;
if (0 == undoSequenceDepth) {
if (actions.types[currentAction].at != ActionType::start) {
currentAction++;
actions.Create(currentAction, ActionType::start);
maxAction = currentAction;
}
actions.types[currentAction].mayCoalesce = false;
}
}
void UndoHistory::DropUndoSequence() noexcept {
undoSequenceDepth = 0;
}
void UndoHistory::DeleteUndoHistory() noexcept {
for (int i = 1; i < maxAction; i++) {
actions.lengths[i] = 0;
}
maxAction = 0;
currentAction = 0;
actions.Create(currentAction, ActionType::start);
savePoint = 0;
tentativePoint = -1;
}
void UndoHistory::SetSavePoint() noexcept {
savePoint = currentAction;
detach.reset();
}
bool UndoHistory::IsSavePoint() const noexcept {
return savePoint == currentAction;
}
bool UndoHistory::BeforeSavePoint() const noexcept {
return (savePoint < 0) || (savePoint > currentAction);
}
bool UndoHistory::BeforeReachableSavePoint() const noexcept {
return (savePoint >= 0) && !detach && (savePoint > currentAction);
}
bool UndoHistory::AfterSavePoint() const noexcept {
return (savePoint >= 0) && (savePoint <= currentAction);
}
bool UndoHistory::AfterDetachPoint() const noexcept {
return detach && (*detach < currentAction);
}
void UndoHistory::TentativeStart() noexcept {
tentativePoint = currentAction;
}
void UndoHistory::TentativeCommit() noexcept {
tentativePoint = -1;
// Truncate undo history
maxAction = currentAction;
}
bool UndoHistory::TentativeActive() const noexcept {
return tentativePoint >= 0;
}
int UndoHistory::TentativeSteps() noexcept {
// Drop any trailing startAction
if (actions.types[currentAction].at == ActionType::start && currentAction > 0)
currentAction--;
if (tentativePoint >= 0)
return currentAction - tentativePoint;
return -1;
}
bool UndoHistory::CanUndo() const noexcept {
return (currentAction > 0) && (maxAction > 0);
}
int UndoHistory::StartUndo() noexcept {
// Drop any trailing startAction
if (actions.types[currentAction].at == ActionType::start && currentAction > 0)
currentAction--;
// Count the steps in this action
int act = currentAction;
while (actions.types[act].at != ActionType::start && act > 0) {
act--;
}
return currentAction - act;
}
Action UndoHistory::GetUndoStep() const noexcept {
Action acta {
actions.types[currentAction].at,
actions.types[currentAction].mayCoalesce,
actions.positions[currentAction],
nullptr,
actions.lengths[currentAction]
};
if (acta.lenData) {
acta.data = scraps->CurrentText() - acta.lenData;
}
return acta;
}
void UndoHistory::CompletedUndoStep() noexcept {
scraps->MoveBack(actions.lengths[currentAction]);
currentAction--;
}
bool UndoHistory::CanRedo() const noexcept {
return maxAction > currentAction;
}
int UndoHistory::StartRedo() noexcept {
// Drop any leading startAction
if (currentAction < maxAction && actions.types[currentAction].at == ActionType::start)
currentAction++;
// Count the steps in this action
int act = currentAction;
while (act < maxAction && actions.types[act].at != ActionType::start) {
act++;
}
return act - currentAction;
}
Action UndoHistory::GetRedoStep() const noexcept {
Action acta{
actions.types[currentAction].at,
actions.types[currentAction].mayCoalesce,
actions.positions[currentAction],
nullptr,
actions.lengths[currentAction]
};
if (acta.lenData) {
acta.data = scraps->CurrentText();
}
return acta;
}
void UndoHistory::CompletedRedoStep() noexcept {
scraps->MoveForward(actions.lengths[currentAction]);
currentAction++;
}
}
|