-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBuffer.cpp
More file actions
581 lines (530 loc) · 21 KB
/
TextBuffer.cpp
File metadata and controls
581 lines (530 loc) · 21 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include <iostream>
#include "textBuffer.h"
#include "history.h"
#include "fileDialog.h"
#include <SFML/Graphics.hpp>
#include <fstream>
// For debug purposes: each node has a discrete ID which increments from the previously created node.
std::atomic<int> nodeID{};
int create_id() {
return nodeID++; // Increments the ID for the next node
}
// Essentially just a linked list
Node::Node(const std::string& data, Node* next, int startIndex, int line)
: data(data), next(next), startIndex(startIndex), line(line), nodeID(create_id()) {}
PieceTable::PieceTable(const std::string& initialData) {
mLines = 1; // Line Count
mLastIndex = -1; // Will be used to add continuously typed character to the same node (w/o making a new one)
if (initialData.empty()) {
mPieces = new Node();
mLength = 0;
}
else {
mPieces = new Node(initialData);
mLength = initialData.length();
}
}
std::string PieceTable::print() const {
std::string output = "";
Node* currentNode = mPieces;
while (currentNode) { // Goes through all nodes in order until the chain breaks
output += currentNode->data;
currentNode = currentNode->next;
}
return output;
}
std::string PieceTable::printSelection(int startIndex, int endIndex) const {
std::string output = "";
bool startReached = false;
Node* currentNode = mPieces;
while (currentNode) { // Goes through all nodes in order until the chain breaks
if (!startReached && startIndex <= currentNode->startIndex + currentNode->data.length()){
if (endIndex <= currentNode->startIndex + currentNode->data.length()) {
output += currentNode->data.substr(startIndex - currentNode->startIndex,
endIndex - startIndex);
break;
}
startReached = true;
output += currentNode->data.substr(startIndex - currentNode->startIndex,
currentNode->data.length() - (startIndex - currentNode->startIndex));
} else if (startReached && endIndex <= currentNode->startIndex + currentNode->data.length()) {
output += currentNode->data.substr(0, (endIndex - currentNode->startIndex));
break;
} else if (startReached)
output += currentNode->data;
currentNode = currentNode->next;
}
return output;
}
int PieceTable::insert(int startIndex, const std::string& data) {
//If tab, replace it with three spaces for simplicity sake
std::string modifiedData = data;
if (modifiedData == "\t") {
int whiteSpace = 0; //Counts how much whitespace occurs before index @ line to have an even tab
int line = getCurrentLine(startIndex);
Node* currentNode = mPieces;
while (currentNode) {
if (currentNode->line == line){
if (startIndex <= currentNode->startIndex + currentNode->data.length()) {
for (int i = 0; i < startIndex - currentNode->startIndex; i++)
if (currentNode->data[i] == ' ')
whiteSpace++;
else
whiteSpace = 0;
break;
}
else
for (int i = 0; i < currentNode->data.length(); i++)
if (currentNode->data[i] == ' ')
whiteSpace++;
else
whiteSpace = 0;
}
else if(currentNode->line > line)
break;
currentNode = currentNode->next;
}
modifiedData = std::string(4 - whiteSpace % 4, ' '); // Adds an even amount of whitespace.
}
// If a character was just typed at the previous index AND the cursor did not move, it will just
// be added to the previous node.
if (mLastIndex != -1 && mLastIndex == startIndex - 1 && mLastNode && modifiedData.find('\n') == modifiedData.npos) {
mLastNode->data += modifiedData;
mLength += modifiedData.length();
mLastIndex++;
Node* currentNode = mLastNode->next;
// Update the starting indexes of each node afterwards.
while (currentNode) {
currentNode->startIndex += modifiedData.length();
currentNode = currentNode->next;
}
std::string emptyString = "";
mHistory.addToHistory(modifiedData, emptyString, startIndex, startIndex,
true);
return modifiedData.length();
}
mLastIndex = -1;
mLastNode = nullptr;
// Calls replace function by not replacing any character (start and end index are equal)
replace(startIndex, startIndex, modifiedData);
return modifiedData.length();
}
int PieceTable::remove(int startIndex, int endIndex) {
// If can't erase, return
if (startIndex == endIndex || mLength == 0)
return 0;
// Checks if theres a tab
if (endIndex - startIndex == 1) {
int whiteSpace = 0; //Counts how much whitespace before character, if exactly 4 or divisible by 4, thats a tab
int line = getCurrentLine(endIndex);
Node* currentNode = mPieces;
while (currentNode) {
if (currentNode->line == line){
if (endIndex <= currentNode->startIndex + currentNode->data.length()) {
for (int i = 0; i < endIndex - currentNode->startIndex; i++)
if (currentNode->data[i] == ' ')
whiteSpace++;
else
whiteSpace = 0;
break;
}
else
for (int i = 0; i < currentNode->data.length(); i++)
if (currentNode->data[i] == ' ')
whiteSpace++;
else
whiteSpace = 0;
}
else if(currentNode-> line > line)
break;
currentNode = currentNode->next;
}
if (whiteSpace && whiteSpace % 4 == 0) {
replace(startIndex-3, endIndex, "");
return 4;
}
}
// Calls replace function without any data
replace(startIndex, endIndex, "", false, false, false);
return -(endIndex - startIndex);
}
// To be called when cursor moves to reset last saved node, thus will make a new node next insertion
// Even makes new node if user returns to original last node position, (will be) used for undo/redo procedure.
void PieceTable::resetNodeSave() {
mLastIndex = -1;
mLastNode = nullptr;
}
// Gets the current line the index is located at.
int PieceTable::getCurrentLine(int index) const{
Node* currentNode = mPieces;
while (currentNode) {
if (index <= currentNode->startIndex + currentNode->data.length() || !currentNode->next)
return currentNode->line;
currentNode = currentNode->next;
}
return 1;
}
// Gets the index relative to it's current line. Used for cursor location.
int PieceTable::relativeLineIndex(int index) const {
Node* currentNode = mPieces;
int currentLine = 1;
int characterDisposition = 0;
while (currentNode) {
if (currentLine != currentNode->line) {
characterDisposition = currentNode->startIndex + 1;
currentLine = currentNode->line;
}
if (index <= currentNode->startIndex + currentNode->data.length() || !currentNode->next)
return index - characterDisposition ;
currentNode = currentNode->next;
}
return 0;
}
// Gets the current node the index is located at. For debugging purposes mainly.
Node* PieceTable::getCurrentNode(int index) const {
Node* currentNode = mPieces;
while (currentNode) {
if (index <= currentNode->startIndex + currentNode->data.length() || !currentNode->next)
return currentNode;
currentNode = currentNode->next;
}
return nullptr;
}
// Gets the global index of a requested relative index on a specific line.
int PieceTable::indexOnLine(int index, int line) const {
// If tries going above first line, just go to beginning.
if (line == 0)
return 0;
if (line > mLines)
return mLength;
Node* currentNode = mPieces;
int offset = 0;
bool foundLine = false;
while (currentNode) {
if (currentNode->line == line) {
// Once reaches first node of specified line, adds it's offset.
if (!foundLine) {
offset += currentNode->startIndex;
foundLine = true;
}
// Don't count new lines - ruins up and down arrow traversal
if (currentNode->data.contains('\n'))
offset++;
// If found, return
if (index + offset <= currentNode->startIndex + currentNode->data.length())
return index + offset;
// Otherwise, return last character of the line
else if (!currentNode->next || currentNode->next->line != line)
return currentNode->startIndex + currentNode->data.length();
}
currentNode = currentNode->next;
}
return mLength;
}
// Main text manipulation functionality
int PieceTable::replace(int startIndex, int endIndex, const std::string& data, bool isRecalled, bool ignoreUndo, bool resetNode) {
if (startIndex < 0 || endIndex > mLength || startIndex > endIndex)
return 0;
if (resetNode)
resetNodeSave();
// Counts how many linebreaks there are to calculate new lines, as well as changes tabs to 4 spaces
int LineBreaks = 0;
std::string modifiedData;
std::string removedData = "";
if (startIndex != endIndex && !ignoreUndo) {
removedData = printSelection(startIndex, endIndex);
}
for (char c : data) {
if (c == '\n') {
LineBreaks++;
modifiedData.push_back('\n');
}
else if (c == '\t')
modifiedData += " ";
else
modifiedData.push_back(c);
}
if ((LineBreaks == 1 && modifiedData[0] != '\n') || LineBreaks > 1) {
int change = 0;
int start = 0;
int lastEndIndex = -1;
mHistory.addToHistory(modifiedData, removedData, startIndex, endIndex, false);
while (true) {
int pos = modifiedData.find('\n', start + 1);
if (pos == std::string::npos) {
change += replace(lastEndIndex, lastEndIndex,
modifiedData.substr(start), false, true);
break;
}
if (lastEndIndex == -1) {
change += replace(startIndex, endIndex, modifiedData.substr(start, (pos - start)),
false, true);
lastEndIndex = startIndex + (pos - start);
}
else {
change += replace(lastEndIndex, lastEndIndex,
modifiedData.substr(start, (pos - start)), false, true);
lastEndIndex += (pos - start);
}
start = pos;
}
return change;
}
// Modify length, adds new text and removes displaced text
int change = modifiedData.length() - (endIndex - startIndex);
mLength += change;
Node* prevNode = nullptr;
Node* currentNode = mPieces;
// Finds startIndex location
while (currentNode && startIndex > currentNode->startIndex + currentNode->data.length()){
prevNode = currentNode;
currentNode = prevNode->next;
}
// If located at the end, will just add a new node
if (!currentNode) {
if (!prevNode)
mPieces = new Node(modifiedData);
else if (prevNode->data.empty()) // If last node is empty, attempt to replace it
prevNode = new Node(modifiedData, nullptr, prevNode->startIndex + modifiedData.length(),
prevNode->line + LineBreaks);
else
prevNode->next = new Node(modifiedData, nullptr, prevNode->startIndex + modifiedData.length(),
prevNode->line + LineBreaks);
mLines += LineBreaks;
return data.length() - (endIndex - startIndex);
}
// If start index is located in the middle of the node, split that node into two.
if (startIndex < currentNode->startIndex + currentNode->data.length()) {
int offset = startIndex - currentNode->startIndex;
int length = currentNode->data.length();
std::string prevData;
if (offset > -length){
int usedOffset = offset >= 0 ? offset : length + offset;
if (usedOffset > length) usedOffset = length;
prevData = currentNode->data.substr(0, usedOffset);
}
else
prevData = "";
std::string nextData;
if (offset < length){
int usedOffset = offset >= 0 ? offset : length + offset;
if (usedOffset < 0) usedOffset = 0;
nextData = currentNode->data.substr(usedOffset);
}
else
nextData = "";
Node* newNode = new Node(nextData, currentNode->next, currentNode->startIndex + offset,
currentNode->line);
currentNode->next = newNode;
currentNode->data = prevData;
}
// Records last node before manipulation as an anchor to attach new nodes to later
Node* anchorNode = currentNode;
prevNode = anchorNode;
currentNode = prevNode->next;
// Adds all nodes to be replaced to deletion pile
std::vector<Node*> toDelete = {};
while (currentNode && endIndex >= currentNode->startIndex + currentNode->data.length()) {
toDelete.push_back(currentNode);
prevNode = currentNode;
currentNode = prevNode->next;
}
// If end index is in the middle of a node, split the node and add the first half to the deletion pile
if (currentNode && endIndex < currentNode->startIndex + currentNode->data.length()) {
int offset = endIndex - currentNode->startIndex;
int length = currentNode->data.length();
std::string prevData;
if (offset > -length){
int usedOffset = offset >= 0 ? offset : length + offset;
if (usedOffset > length) usedOffset = length;
prevData = currentNode->data.substr(0, usedOffset);
}
else
prevData = "";
std::string nextData;
if (offset < length){
int usedOffset = offset >= 0 ? offset : length + offset;
if (usedOffset < 0) usedOffset = 0;
nextData = currentNode->data.substr(usedOffset);
}
else
nextData = "";
Node* newNode = new Node(nextData, currentNode->next, currentNode->startIndex + offset,
currentNode->line);
currentNode->next = newNode;
currentNode->data = prevData;
toDelete.push_back(currentNode);
}
// Creates first node after end index
Node* firstRightNode;
if (!toDelete.empty())
firstRightNode = toDelete.back()->next;// Either becomes first node after the deleted nodes
else
firstRightNode = anchorNode->next; // Or first node after our original anchor point if no data to be added
anchorNode->next = nullptr;
// Deletes all nodes in pile
int lineBreaksRemoved = 0;
while (!toDelete.empty()) {
Node* nodeToDelete = toDelete.back();
toDelete.pop_back();
if (nodeToDelete) {
for (int i = 0; i < nodeToDelete->data.length(); i++)
if (nodeToDelete->data[i] == '\n')
lineBreaksRemoved++; // If deleting line breaks, alter the line count
delete nodeToDelete;
nodeToDelete = nullptr; // Dereferences to avoid memory leak
}
}
// Updates after final change in lines
mLines += LineBreaks - lineBreaksRemoved;
// Creates node to either store the new data or just becomes the first node on the right if no data
Node* newDataNode;
if (!modifiedData.empty()) {
int line = anchorNode->line + LineBreaks;
if (line < anchorNode->line)
line = anchorNode->line;
newDataNode = new Node(modifiedData, firstRightNode,
anchorNode->startIndex + anchorNode->data.length(), line);
mLastNode = newDataNode;
}
else{
newDataNode = firstRightNode;
mLastNode = nullptr;
}
// Attaches to our original anchor
anchorNode->next = newDataNode;
// If nodes exist after replacement, alter all nodes' start indexes and line numbers.
if (firstRightNode) {
prevNode = newDataNode;
Node* nextNode = firstRightNode;
while (nextNode) {
if (modifiedData.empty())
nextNode->startIndex += change;
else
nextNode->startIndex = prevNode->startIndex + prevNode->data.length();
nextNode->line += LineBreaks - lineBreaksRemoved;
prevNode = nextNode;
nextNode = prevNode->next;
}
}
if (!ignoreUndo)
mHistory.addToHistory(modifiedData, removedData, startIndex, endIndex, endIndex == mLastIndex && !mLastNode);
mLastIndex = startIndex;
return data.length() - (endIndex - startIndex);
}
sf::Vector2i PieceTable::undo() {
Action* action = mHistory.getLastAction();
if (action){
if (!action->data.empty())
replace(action->startIndex, action->startIndex + action->data.length(), "", false,
true);
if (!action->removedData.empty())
replace(action->startIndex, action->startIndex, action->removedData, false,
true);
int left = !action->multipleActions ? action->startIndex : -1;
int right = action->startIndex + action->removedData.length();
if (left != -1 && abs(left - right) == 1)
left = -1;
return sf::Vector2i(left, right);
}
return sf::Vector2i(-1, -1);
}
int PieceTable::redo() {
Action* action = mHistory.getLastUndo();
if (action) {
if (action->startIndex != action->endIndex)
replace(action->startIndex, action->endIndex, "", false, true);
if (!action->data.empty())
replace(action->startIndex, action->startIndex, action->data, false, true);
return action->startIndex + action->data.length();
}
return -1;
}
int PieceTable::length() const {
return mLength;
}
int PieceTable::lines() const {
return mLines;
}
// Counts number of nodes currently in piece table
int PieceTable::countNodes() const {
int output = 0;
Node* currentNode = mPieces;
while (currentNode) {
output += 1;
currentNode = currentNode->next;
}
return output;
}
// Returns main node for node analysis. Should only ever be accessible during debug.
Node* PieceTable::mainNode() const {
return mPieces;
}
void PieceTable::open(std::ifstream& file) {
mHistory.clearHistory();
remove(0, mLength);
std::string line;
while (std::getline(file, line)) {
replace(mLength, mLength, line + '\n', false, true);
}
}
void PieceTable::save() {
std::string file = print();
if (ShowSaveDialogAndWriteString(file)) {
std::cout << "File was successfully saved.\n";
} else {
std::cout << "Save was canceled or failed.\n";
}
}
//Test Cases
void Tests() {
PieceTable Table("hey world");
std::cout << Table.print() << '\n';
Table.replace(5, 7, "!!!");
std::cout << Table.print() << '\n';
Table.remove(5, 8);
std::cout << Table.print() << '\n';
Table.insert(5, "or");
std::cout << Table.print() << '\n';
Table.remove(2, 6);
std::cout << Table.print() << '\n';
Table.insert(0, "A new word: ");
std::cout << Table.print() << '\n';
Table.insert(15, "a");
std::cout << Table.print() << '\n';
Table.remove(0, 0);
std::cout << Table.print() << '\n';
Table.replace(1, 5, "n existing");
std::cout << Table.print() << '\n';
std::cout << "The length of this string is " << Table.length() << " and the amount of nodes is " << Table.countNodes() << '\n';
}
std::vector<std::array<int, 3>> PieceTable::getSelectionBoxes(int startIndex, int endIndex) {
std::vector<std::array<int, 3>> output;
int currentOffset = 0;
int lastIndex = -1;
Node* currNode = mPieces;
auto pushLine = [&](int end) {
output.push_back({lastIndex - (currNode->line > 1 && lastIndex > 0 ? 1 : 0),
end - (currNode->line > 1 ? 1 : 0), currNode->line});
};
while (currNode) {
if (lastIndex == -1 && startIndex <= currNode->startIndex + currNode->data.length())
lastIndex = startIndex - currentOffset;
if (endIndex <= currNode->startIndex + currNode->data.length()) {
pushLine(endIndex - currentOffset);
lastIndex = -1;
break;
}
if (!currNode->next)
pushLine(static_cast<int>(currNode->data.length()) + currNode->startIndex - currentOffset);
if (currNode->next && currNode->next->line != currNode->line) {
if (lastIndex != -1) {
pushLine(static_cast<int>(currNode->data.length()) + currNode->startIndex - currentOffset + 1);
lastIndex = 0;
}
currentOffset = currNode->next->startIndex;
}
currNode = currNode->next;
}
return output;
}