-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtextdocument.cpp
501 lines (390 loc) · 14.1 KB
/
textdocument.cpp
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
// edbee - Copyright (c) 2012-2025 by Rick Blommers and contributors
// SPDX-License-Identifier: MIT
#include "textdocument.h"
#include <QStringList>
#include "edbee/models/changes/mergablechangegroup.h"
#include "edbee/models/changes/linedatachange.h"
#include "edbee/models/changes/selectionchange.h"
#include "edbee/models/changes/textchange.h"
#include "edbee/models/changes/textchangewithcaret.h"
#include "edbee/models/textlinedata.h"
#include "edbee/models/textdocumentfilter.h"
#include "edbee/models/textrange.h"
#include "edbee/models/textundostack.h"
#include "edbee/debug.h"
namespace edbee {
/// Constructs the textdocument
TextDocument::TextDocument( QObject* obj )
: QObject(obj)
, documentFilter_(nullptr)
, documentFilterRef_(nullptr)
, textLineDataManager_(nullptr)
{
textLineDataManager_ = new TextLineDataManager();
}
/// Destroys the textdocument
TextDocument::~TextDocument()
{
delete textLineDataManager_;
delete documentFilter_;
}
/// This method can be used to change the number of reserved fields by the document
/// Increasing the amount will result in a realoc
/// Decreasting the fieldcount reults in the lost of the 'old' fields
/// At least the 'PredefinedFieldCount' amont of fields are required
/// This method EMPTIES the undo-stack. So after this call all undo history is gone!
void TextDocument::setLineDataFieldsPerLine( int count )
{
Q_ASSERT( count >= PredefinedFieldCount );
lineDataManager()->setFieldsPerLine( count );
textUndoStack()->clear();
}
void TextDocument::giveLineDataManager(TextLineDataManager *manager)
{
delete textLineDataManager_;
textLineDataManager_ = manager;
}
/// This method gives a given data item to a text line
void TextDocument::giveLineData(int line, int field, TextLineData* dataItem)
{
Q_ASSERT(line < lineCount() );
LineDataChange* change = new LineDataChange( line, field );
change->giveLineData( dataItem );
executeAndGiveChange( change, true );
}
/// Returns the line specific data at the given line
/// @param line the line number to retrieve the line data for
/// @param field the field to retrieve the data for
/// @return TextLineData the associated line data
TextLineData* TextDocument::getLineData(int line, int field)
{
int len = lineDataManager()->length();
// Q_ASSERT( len == lineCount() ); FIXME: disabled, issue with renderer retreiving linedata
Q_ASSERT( line < len );
return lineDataManager()->get( line, field );
}
/// Starts an undo group
/// @param group the textchange group that groups the undo operations
void TextDocument::beginUndoGroup(ChangeGroup* group)
{
if( !group ) {
group = new ChangeGroup(nullptr);
}
// if( documentFilter() ) {
// documentFilter()->filterBeginGroup( this, group );
// }
textUndoStack()->beginUndoGroup(group);
}
/// Ends the current undo group
/// @param coalesceId the coalesceId
/// @param flatten should the operation be flatten (flattens undo-group trees)
void TextDocument::endUndoGroup( int coalesceId, bool flatten)
{
// if( documentFilter() ) {
// TextChangeGroup* group = textUndoStack()->currentGroup();
// documentFilter()->filterEndGroup( this, group, coalesceId, flatten );
// }
textUndoStack()->endUndoGroup(coalesceId,flatten);
}
/// Ends the undo group and discards all recorded information
/// Warning it does NOT undo all made changes!!!
void TextDocument::endUndoGroupAndDiscard()
{
textUndoStack()->endUndoGroupAndDiscard();
}
/// this method return true if the undo stack is enabled
bool TextDocument::isUndoCollectionEnabled()
{
return textUndoStack()->isCollectionEnabled();
}
/// Enables or disables the collection of undo commands
void TextDocument::setUndoCollectionEnabled(bool enabled)
{
textUndoStack()->setCollectionEnabled(enabled);
}
/// This method should return true if the current change is the cause of an undo operation
bool TextDocument::isUndoRunning()
{
return textUndoStack()->isUndoRunning();
}
/// Checks if currently an undo operation is running
bool TextDocument::isRedoRunning()
{
return textUndoStack()->isRedoRunning();
}
/// Is it an undo or redo (which means all commands area already available)
bool TextDocument::isUndoOrRedoRunning()
{
return isUndoRunning() || isRedoRunning();
}
/// Checks if the document is in a persited state
bool TextDocument::isPersisted()
{
return textUndoStack()->isPersisted();
}
/// Calc this method to mark current state as persisted
void TextDocument::setPersisted(bool enabled)
{
textUndoStack()->setPersisted(enabled);
}
/// Sets the document filter without tranfering the ownership
void TextDocument::setDocumentFilter(TextDocumentFilter* filter)
{
delete documentFilter_;
documentFilter_ = 0;
documentFilterRef_ = filter;
}
/// this method sets the document filter
/// You can give a 0 pointer to delte the old filter!
void TextDocument::giveDocumentFilter(TextDocumentFilter* filter)
{
delete documentFilter_;
documentFilter_ = filter;
documentFilterRef_ = filter;
}
/// This method returns the document filter
TextDocumentFilter* TextDocument::documentFilter()
{
return documentFilterRef_;
}
/// Start the changes
void TextDocument::beginChanges(TextEditorController* controller)
{
beginUndoGroup( new MergableChangeGroup( controller) );
}
/// Replaces the given rangeset
void TextDocument::replaceRangeSet(TextRangeSet& rangeSet, const QString& textIn, bool stickySelection)
{
return replaceRangeSet(rangeSet, QStringList(textIn), stickySelection);
}
/// replaces the given rangeset
void TextDocument::replaceRangeSet(TextRangeSet& rangeSet, const QStringList& textsIn, bool stickySelection)
{
QStringList texts = textsIn;
if( documentFilter() ) {
documentFilter()->filterReplaceRangeSet( this, rangeSet, texts );
}
rangeSet.beginChanges();
int delta = 0;
int idx = 0, oldRangeCount = 0;
while( idx < (oldRangeCount = rangeSet.rangeCount()) ) {
TextRange& range = rangeSet.range(idx);
QString text = texts.at(idx%texts.size()); // rotating text-fetching
//qlog_info() << idx << ">> " << text << " : delta="<<delta<<", " << range.toString();
range.setCaret( range.caret() + delta );
range.setAnchor( range.anchor() + delta );
//qlog_info() << " => " << range.toString();
delta += (text.length() - range.length());
TextChangeWithCaret* change = new TextChangeWithCaret(range.min(),range.length(),text,-1);
Change* effectiveChange = executeAndGiveChange( change, false );
TextChangeWithCaret* effectiveChangeWithCaret = dynamic_cast<TextChangeWithCaret*>(effectiveChange);
// when a new caret position is supplied (can only happen via a TextDocumentFilter)
// access it and change the caret to the given position
int caret = 0;
if( effectiveChangeWithCaret && effectiveChangeWithCaret->caret() >= 0 ) {
caret = effectiveChangeWithCaret->caret();
// Default caret location is change-independent: old location + length new text
} else {
caret = range.min() + text.length();
}
// sticky selection, keeps the selection around the text
if(stickySelection) {
range.set(range.min(), caret);
} else {
range.setCaret(caret);
range.reset();
}
// next range
if( rangeSet.rangeCount() < oldRangeCount ) {
qlog_info() << "TEST TO SEE IF THIS REALLY HAPPENS!! I think it cannot happen. (but I'm not sure)";
Q_ASSERT(false);
// else we stay at the same location
} else {
++idx;
}
}
rangeSet.endChanges();
}
/// sets the selectioin for the current rangeset
/// The selection may never be empty
/// @param controller the controller to given the selection for
/// @param rangeSet the rangeset with the new selection
void TextDocument::giveSelection( TextEditorController* controller, TextRangeSet* rangeSet)
{
Q_ASSERT(rangeSet->rangeCount()>0);
SelectionChange* selChange = new SelectionChange( controller );
selChange->giveTextRangeSet( rangeSet );
executeAndGiveChange( selChange, 0 );
}
//// end the changes
void TextDocument::endChanges(int coalesceId)
{
endUndoGroup(coalesceId,true);
}
/// call this method to execute a change. The change is first passed to the filter
/// so the documentFilter can handle the processing of the change
/// When not filter is active the 'execute' method is called on the change
///
/// WARNING, you should never Access the change pointer given to this method
/// It's possible the change gets deleted
///
/// This method should return the effective text-change (or 0 if no text-change has been stored)
Change *TextDocument::executeAndGiveChange(Change* change, int coalesceId )
{
if( documentFilter() ) {
return documentFilter()->filterChange( this, change, coalesceId );
} else {
beginUndoGroup(); // automatically group changes together (when changes happen on emission)
change->execute( this );
Change* result = giveChangeWithoutFilter( change, coalesceId );
Q_UNUSED(result)
endUndoGroup(coalesceId, true);
return textUndoStack()->last();
// return result;
}
}
/// Appends the given text to the document
/// @param text the text to append
/// @param coalesceId (default 0) the coalesceId to use. Whe using the same number changes could be merged to one change. CoalesceId of 0 means no merging
void TextDocument::append(const QString& text, int coalesceId )
{
replace( this->length(), 0, text, coalesceId );
}
/// Appends the given text
/// @param text the text to append
/// @param coalesceId (default 0) the coalesceId to use. Whe using the same number changes could be merged to one change. CoalesceId of 0 means no merging
void TextDocument::replace( int offset, int length, const QString& text, int coalesceId )
{
executeAndGiveChange( new TextChange( offset, length, text ), coalesceId);
}
/// Changes the compelte document text
/// @param text the new document text
void TextDocument::setText(const QString& text)
{
replace( 0, length(), text, 0 );
}
/// begins the raw append modes. In raw append mode data is directly streamed
/// to the textdocument-buffer. No undo-data is collected and no events are fired
void TextDocument::rawAppendBegin()
{
setUndoCollectionEnabled(false); // no undo's
buffer()->rawAppendBegin();
}
/// When then raw appending is done. The events are fired that the document has been changed
/// The undo-collection is enabled again
void TextDocument::rawAppendEnd()
{
buffer()->rawAppendEnd();
setUndoCollectionEnabled(true);
}
/// Appends a single char in raw append mode
void TextDocument::rawAppend(QChar c)
{
buffer()->rawAppend(c);
}
/// Appends an array of characters
void TextDocument::rawAppend(const QChar* chars, int length)
{
buffer()->rawAppend(chars,length);
}
/// Returns the length of the document in characters
/// default implementation is to forward this call to the textbuffer
int TextDocument::length()
{
return buffer()->length();
}
/// Returns the number of lines
int TextDocument::lineCount()
{
return buffer()->lineCount();
}
/// Returns the character at the given position
QChar TextDocument::charAt(int idx)
{
return buffer()->charAt(idx);
}
/// returns the char at the given index if the index is valid
/// else the null character is returned
///
/// @param idx the index to retrieve
/// @return the character at the given index or the null-character
QChar TextDocument::charAtOrNull(int idx)
{
if( 0 <= idx && idx < length() ) {
return charAt(idx);
}
return QChar();
}
/// Retrieves the character-offset of the given line
/// @param line the line number (0-based) to retrieve the offset for
/// @return the character offset
int TextDocument::offsetFromLine(int line)
{
return buffer()->offsetFromLine(line);
}
/// returns the line number which contains the given offset
/// @param offset the character offset
/// @return the line number (0 is the first line )
int TextDocument::lineFromOffset(int offset)
{
return buffer()->lineFromOffset(offset);
}
/// return the column position for the given offset and line
/// @param offset the offset position
/// @param line the line number which contains this offset. (When -1 the line number is calculated)
/// @return the column position of the given offset
int TextDocument::columnFromOffsetAndLine(int offset, int line)
{
return buffer()->columnFromOffsetAndLine(offset,line);
}
/// Returns the character offset of the given line and column
/// @param line the line number
/// @param column the column position
/// @return the character offset in the document
int TextDocument::offsetFromLineAndColumn(int line, int column)
{
return buffer()->offsetFromLineAndColumn(line,column);
}
/// Returns the length of the given line
/// @param line the line number
/// @return the line length
int TextDocument::lineLength(int line)
{
return buffer()->lineLength(line);
}
/// returns the length of the given lilne without the newline
/// @param line the line number
/// @return the length of the line excluding the newline character
int TextDocument::lineLengthWithoutNewline(int line)
{
return buffer()->lineLengthWithoutNewline(line);
}
/// Returns the document text as a QString
/// @return the complete document context
QString TextDocument::text()
{
return buffer()->text();
}
/// Returns the given part of the text
/// @param offset the character offset in the document
/// @param length the length of the part in characters
/// @return the text at the given positions
QString TextDocument::textPart(int offset, int length)
{
return buffer()->textPart( offset, length );
}
/// Returns the given line without the trailing \n character
/// @param line the line number to retrieve the data for
/// @return the content at the given line
QString TextDocument::lineWithoutNewline(int line)
{
return buffer()->lineWithoutNewline(line);
}
//// Returns the contents at the given line inclusive the trailing \n character
/// @pparam line the line number to retrieve
/// @return the line at the given position
QString TextDocument::line(int line)
{
return buffer()->line(line);
}
} // edbee