-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorCore.cpp
More file actions
367 lines (325 loc) · 10.8 KB
/
EditorCore.cpp
File metadata and controls
367 lines (325 loc) · 10.8 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
#include "EditorCore.h"
#include <iostream>
EditorCore::EditorCore(){
pieceTable = new PieceTable();
textBuffer.addCharacter(Character('\n'));
executor.setGapBuffer(&(this->gapBuffer));
executor.setPieceTable((this->pieceTable));
executor.setTextBuffer(&(this->textBuffer));
//insert empty line without commands
Index ptr = gapBuffer.insert(0);
Index ind = pieceTable->add();
gapBuffer[ptr] = ind;
}
EditorCore::~EditorCore(){
delete pieceTable;
}
void EditorCore::removeInLine(LineNo line, Index columnStart, Index columnEnd){
Position pos1 = getPiecePosition(line, columnStart);
Position pos2 = getPiecePosition(line, columnEnd);
bool between1 = (pos1.textPosition == GET(pos1.pieceIndex).getTextEnd());
bool between2 = (pos2.textPosition == GET(pos2.pieceIndex).getTextEnd());
if(between1){
pos1.pieceIndex = GET(pos1.pieceIndex).getNext();
pos1.textPosition = GET(pos1.pieceIndex).getTextStart();
}
if(pos1.pieceIndex == pos2.pieceIndex){
if(between1 && between2){
RemovePieceSequenceRedoCmd cmd;
cmd.pieceStart = pos1.pieceIndex;
cmd.pieceLast = pos2.pieceIndex;
cmd.piecePreStartTextEnd = GET(GET(pos1.pieceIndex).getPrev()).getTextEnd();
cmd.pieceSucLastTextStart = GET(GET(pos2.pieceIndex).getNext()).getTextStart();
executor.removePieceSequenceRedo(cmd);
}else if(between1){
executor.setTextStart(pos2.pieceIndex, pos2.textPosition);
}else if(between2){
executor.setTextEnd(pos1.pieceIndex, pos2.textPosition);
}else{
InsertInLineBetweenPieceRedoCmd cmd;
cmd.pieceBefore = pos1.pieceIndex;
cmd.textStart = pos2.textPosition;
cmd.textEnd = GET(pos2.pieceIndex).getTextEnd();
executor.insertInLineBetweenPieceRedo(cmd);
executor.setTextEnd(pos1.pieceIndex, pos1.textPosition);
}
}else{
Index pre = between1? GET(pos1.pieceIndex).getPrev():pos1.pieceIndex;
Index next = between2? GET(pos2.pieceIndex).getNext():pos2.pieceIndex;
if((!between1)&&(!between2)&& GET(pos1.pieceIndex).getNext()== pos2.pieceIndex){
executor.setTextEnd(pos1.pieceIndex, pos1.textPosition);
executor.setTextStart(pos2.pieceIndex, pos2.textPosition);
}else{
RemovePieceSequenceRedoCmd cmd;
cmd.pieceStart = GET(pre).getNext();//pos1.pieceIndex;
cmd.pieceLast = GET(next).getPrev();
cmd.piecePreStartTextEnd = between1? GET(pre).getTextEnd():pos1.textPosition;
cmd.pieceSucLastTextStart = between2? GET(next).getTextStart(): pos2.textPosition;
executor.removePieceSequenceRedo(cmd);
}
}
}
void EditorCore::removeOneLineWithoutLineBreak(LineNo line){
RemoveOneLineWithoutLineBreakRedoCmd cmd;
cmd.tail = *gapBuffer.get(line);
executor.removeOneLineWithoutLineBreakRedo(cmd);
}
void EditorCore::undo(){
executor.undo();
}
void EditorCore::redo(){
executor.redo();
}
void EditorCore::insertInLine(LineNo line, Index column, const Character* chs, Length length){
Position position = getPiecePosition(line, column);
insertInLine(position, chs, length);
}
void EditorCore::insertInLine(const Position& position, const Character* chs, Length length){
Index headIndex = gapBuffer[position.line];
Index rover = headIndex;
Index textStart = textBuffer.getEnd();
textBuffer.addCharacters(chs, length);
Index textEnd = textBuffer.getEnd();
if(position.textPosition < GET(position.pieceIndex).getTextEnd()){
InsertInLineInPieceRedoCmd cmd;
cmd.leftTextStart = GET(position.pieceIndex).getTextStart();
cmd.leftTextEnd = position.textPosition;
cmd.rightTextStart = position.textPosition;
cmd.rightTextEnd = GET(position.pieceIndex).getTextEnd();
cmd.ori = position.pieceIndex;
cmd.oriTextStart = textStart;
cmd.oriTextEnd = textEnd;
executor.insertInLineInPieceRedo(cmd);
lastEdit = position.pieceIndex;
}
else if(position.textPosition == GET(position.pieceIndex).getTextEnd()){
Index txtEnd = GET(position.pieceIndex).getTextEnd();
Index txtStart = GET(position.pieceIndex).getTextStart();
bool isTailPiece = (txtEnd==txtStart)? true : (textBuffer.data(txtStart)->ch=='\n');
bool append = (txtEnd == textStart); //textStart is the original textEnd
if((append&& (!isTailPiece)) /*lastEdit == position.pieceIndex*/){
executor.setTextEnd(position.pieceIndex,GET(position.pieceIndex).getTextEnd()+ textEnd- textStart);
}else{
InsertInLineBetweenPieceRedoCmd cmd;
cmd.pieceBefore = position.pieceIndex;
cmd.textStart = textStart;
cmd.textEnd = textEnd;
executor.insertInLineBetweenPieceRedo(cmd);
}
}
else{
assert(false);
}
}
void EditorCore::insertLineBreak(LineNo line, Index column){
Position pos = getPiecePosition(line,column);
//corresponding to the default text '\n'
Index textStart = 0;
Index textEnd = 1;
//has linebreak line
if(GET(pos.headIndex).getTextEnd()-GET(pos.headIndex).getTextStart() !=0){
//executor.setTextStartEnd(gapBuffer[ind],textStart,textEnd);
}else{
//give old line a linebreak
executor.setTextStartEnd(pos.headIndex, textStart, textEnd);
textStart = 0;
textEnd = 0;
}
if(pos.textPosition == GET(pos.pieceIndex).getTextEnd()){
if(GET(pos.pieceIndex).getNext() != pos.headIndex){
InsertBreakBetweenPieceRedoCmd cmd;
cmd.pieceIndex = pos.pieceIndex;
cmd.line = pos.line;
cmd.textStart = textStart;
cmd.textEnd = textEnd;
executor.insertBreakBetweenPieceRedo(cmd);
}else{
Index ind = executor.insertEmptyLine(line + 1);
}
}else if(pos.textPosition < GET(pos.pieceIndex).getTextEnd()){
InsertBreakInPieceRedoCmd cmd;
cmd.textStart = textStart;
cmd.textEnd = textEnd;
cmd.textSplitPoint = pos.textPosition;
cmd.pieceIndex = pos.pieceIndex;
cmd.lineNo = line;
executor.insertBreakInPieceRedo(cmd);
}
}
Length EditorCore::getLineLength(LineNo line) const{
Length length = 0;
getLineText(line, nullptr,length);
return length;
}
void EditorCore::getLineText(LineNo line, Character* data, Length& length) const{
Index* pt = gapBuffer.get(line);
Index rover = *pt;
length = 0;
rover = pieceTable->nextIndex(rover);
Index head = rover;
do{
Piece& piece = pieceTable->get(rover);
Length len = piece.getTextEnd()-piece.getTextStart();
if(data != nullptr){
std::memcpy(data +length, textBuffer.data(piece.getTextStart()), len);
}
length += len;
rover = pieceTable->nextIndex(rover);
}while(rover != head);
return;
}
String EditorCore::getLineText(LineNo line) const{
String text;
Index* pt = gapBuffer.get(line);
Index rover = *pt;
Length length = 0;
rover = pieceTable->nextIndex(rover);
Index head = rover;
do{
Piece& piece = pieceTable->get(rover);
Index start = piece.getTextStart();
Length len = piece.getTextEnd() - start;
const Character* chs = textBuffer.data(start);
for(Length i = 0; i< len; ++i){
text.push_back(chs[i].ch);
}
length += len;
rover = pieceTable->nextIndex(rover);
}while(rover != head);
return text;
}
Length EditorCore::getLineCount() const{
return gapBuffer.size();
}
String EditorCore::getContent() const{
Length l = gapBuffer.size();
Length lineLength = 0;
Length totalLength = 0;
for(LineNo i =0; i<l;++i){
lineLength = getLineLength(i);
totalLength += lineLength;
}
String text;
for(LineNo i = 0; i < l; ++i){
Index* ptr = gapBuffer.get(i);
Index rover = GET(*ptr).getNext();
Index head = rover;
do{
auto start = GET(rover).getTextStart();
auto end = GET(rover).getTextEnd();
for(Index j = start; j < end;++j){
text.push_back(textBuffer.data(j)->ch);
}
rover = GET(rover).getNext();
}while(rover != head);
}
return text;
}
void EditorCore::_appendText(const String& text){
Index ind = gapBuffer.size();
//last line is always an line without breakline
Length len = getLineLength(ind - 1);
_insertText(ind - 1, len, text);
}
void EditorCore::_insertText(LineNo line, Index column, const String& text){
const Character* chs = (const Character*)(text.data());
insertText(line,column, chs, text.size());
}
bool EditorCore::hasBreakLine(LineNo id) const{
auto& it = GET(*gapBuffer.get(id));
return (it.getTextEnd() - it.getTextStart()) != 0;
}
void EditorCore::insertText(LineNo line, Index column, const Character* chs, Length length){
Index prev = 0;
for(Length i= 0; i <= length; ++i){
if((i== length) ||(chs[i].ch == '\n')){
if(i - prev >0){
insertInLine(line, column, chs+prev, i - prev);
}
if(chs[i].ch == '\n'){
insertLineBreak(line, column + i - prev);
}
line += 1;
column = 0;
prev = i+1;
}
}
}
void EditorCore::_removeText(LineNo lineStart,Index columnStart, LineNo lineLast, Index columnEnd){
Position pos = getPiecePosition(lineLast, columnEnd);
Piece& piece = pieceTable->get(pos.pieceIndex);
bool last = (piece.getNext()== pos.headIndex) && piece.getTextEnd() == pos.textPosition;
if(lineStart == lineLast){
if(columnStart == 0 && last){
removeOneLine(lineStart);
LOG
}else{
removeInLine(lineStart, columnStart, columnEnd);
LOG
}
}else{
//we remove from last to first so that previous line id won't be affected
if(last){
removeOneLineWithoutLineBreak(lineLast);
LOG
}else{
removeInLine(lineLast, 0, columnEnd);
LOG
}
for(LineNo i=lineLast- 1; i > lineStart;--i){
removeOneLine(i);
LOG
}
if(columnStart == 0){
removeOneLine(lineStart);
LOG
}else{
Length firstLineEnd = getLineLength(lineStart);
if(hasBreakLine(lineStart)){
firstLineEnd -= 1;
}
removeInLine(lineStart,columnStart, firstLineEnd);
removeLineBreak(lineStart);
LOG
}
}
}
void EditorCore::removeOneLine(LineNo id){
/* deprecated
Index* ptr = gapBuffer.get(id);
LineCmd cmd(CmdType::InsertLine, id);
cmd.pieceIndex = *ptr;
gapBuffer.remove(id);
Index tail = GET(cmd.pieceIndex).getNext();
pieceTable->erase(cmd.pieceIndex, tail);
executor.addCmd(&cmd);
*/
RemoveLineRedoCmd cmd;
cmd.line = id;
executor.removeLineRedo(cmd);
}
void EditorCore::removeLineBreak(LineNo id){
RemoveLineBreakRedoCmd cmd;
cmd.line = id;
executor.removeLineBreakRedo(cmd);
executor.removeLineBreak(id);
}
Position EditorCore::getPiecePosition(LineNo id, Index column){
Position pos;
pos.line = id;
pos.column = column;
pos.headIndex = *gapBuffer.get(id);
Piece* head = &(pieceTable->get(pos.headIndex));
Piece* rover = head;
Length l = 0;
do{
if(column <= l){
pos.pieceIndex = pieceTable->getAddress(*rover);
pos.textPosition = rover->getTextEnd() - (l-column);
break;
}
rover = &(pieceTable->get(rover->getNext()));
l += rover->getTextEnd() - rover->getTextStart();
}while(head != rover);
return pos;
}