RequestPullDlg: Speed up filling branch combo box
[TortoiseGit.git] / ext / scintilla / src / CellBuffer.cxx
blob6bc3a725a0e68da2ce227dbe6acae85630ea30b6
1 // Scintilla source code edit control
2 /** @file CellBuffer.cxx
3 ** Manages a buffer of cells.
4 **/
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdarg.h>
13 #include <algorithm>
15 #include "Platform.h"
17 #include "Scintilla.h"
18 #include "SplitVector.h"
19 #include "Partitioning.h"
20 #include "CellBuffer.h"
21 #include "UniConversion.h"
23 #ifdef SCI_NAMESPACE
24 using namespace Scintilla;
25 #endif
27 LineVector::LineVector() : starts(256), perLine(0) {
28 Init();
31 LineVector::~LineVector() {
32 starts.DeleteAll();
35 void LineVector::Init() {
36 starts.DeleteAll();
37 if (perLine) {
38 perLine->Init();
42 void LineVector::SetPerLine(PerLine *pl) {
43 perLine = pl;
46 void LineVector::InsertText(int line, int delta) {
47 starts.InsertText(line, delta);
50 void LineVector::InsertLine(int line, int position, bool lineStart) {
51 starts.InsertPartition(line, position);
52 if (perLine) {
53 if ((line > 0) && lineStart)
54 line--;
55 perLine->InsertLine(line);
59 void LineVector::SetLineStart(int line, int position) {
60 starts.SetPartitionStartPosition(line, position);
63 void LineVector::RemoveLine(int line) {
64 starts.RemovePartition(line);
65 if (perLine) {
66 perLine->RemoveLine(line);
70 int LineVector::LineFromPosition(int pos) const {
71 return starts.PartitionFromPosition(pos);
74 Action::Action() {
75 at = startAction;
76 position = 0;
77 data = 0;
78 lenData = 0;
79 mayCoalesce = false;
82 Action::~Action() {
83 Destroy();
86 void Action::Create(actionType at_, int position_, const char *data_, int lenData_, bool mayCoalesce_) {
87 delete []data;
88 data = NULL;
89 position = position_;
90 at = at_;
91 if (lenData_) {
92 data = new char[lenData_];
93 memcpy(data, data_, lenData_);
95 lenData = lenData_;
96 mayCoalesce = mayCoalesce_;
99 void Action::Destroy() {
100 delete []data;
101 data = 0;
104 void Action::Grab(Action *source) {
105 delete []data;
107 position = source->position;
108 at = source->at;
109 data = source->data;
110 lenData = source->lenData;
111 mayCoalesce = source->mayCoalesce;
113 // Ownership of source data transferred to this
114 source->position = 0;
115 source->at = startAction;
116 source->data = 0;
117 source->lenData = 0;
118 source->mayCoalesce = true;
121 // The undo history stores a sequence of user operations that represent the user's view of the
122 // commands executed on the text.
123 // Each user operation contains a sequence of text insertion and text deletion actions.
124 // All the user operations are stored in a list of individual actions with 'start' actions used
125 // as delimiters between user operations.
126 // Initially there is one start action in the history.
127 // As each action is performed, it is recorded in the history. The action may either become
128 // part of the current user operation or may start a new user operation. If it is to be part of the
129 // current operation, then it overwrites the current last action. If it is to be part of a new
130 // operation, it is appended after the current last action.
131 // After writing the new action, a new start action is appended at the end of the history.
132 // The decision of whether to start a new user operation is based upon two factors. If a
133 // compound operation has been explicitly started by calling BeginUndoAction and no matching
134 // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
135 // operation. If there is no outstanding BeginUndoAction call then a new operation is started
136 // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
137 // Sequences that look like typing or deletion are coalesced into a single user operation.
139 UndoHistory::UndoHistory() {
141 lenActions = 100;
142 actions = new Action[lenActions];
143 maxAction = 0;
144 currentAction = 0;
145 undoSequenceDepth = 0;
146 savePoint = 0;
148 actions[currentAction].Create(startAction);
151 UndoHistory::~UndoHistory() {
152 delete []actions;
153 actions = 0;
156 void UndoHistory::EnsureUndoRoom() {
157 // Have to test that there is room for 2 more actions in the array
158 // as two actions may be created by the calling function
159 if (currentAction >= (lenActions - 2)) {
160 // Run out of undo nodes so extend the array
161 int lenActionsNew = lenActions * 2;
162 Action *actionsNew = new Action[lenActionsNew];
163 for (int act = 0; act <= currentAction; act++)
164 actionsNew[act].Grab(&actions[act]);
165 delete []actions;
166 lenActions = lenActionsNew;
167 actions = actionsNew;
171 const char *UndoHistory::AppendAction(actionType at, int position, const char *data, int lengthData,
172 bool &startSequence, bool mayCoalesce) {
173 EnsureUndoRoom();
174 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
175 //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
176 // actions[currentAction - 1].position, actions[currentAction - 1].lenData);
177 if (currentAction < savePoint) {
178 savePoint = -1;
180 int oldCurrentAction = currentAction;
181 if (currentAction >= 1) {
182 if (0 == undoSequenceDepth) {
183 // Top level actions may not always be coalesced
184 int targetAct = -1;
185 const Action *actPrevious = &(actions[currentAction + targetAct]);
186 // Container actions may forward the coalesce state of Scintilla Actions.
187 while ((actPrevious->at == containerAction) && actPrevious->mayCoalesce) {
188 targetAct--;
189 actPrevious = &(actions[currentAction + targetAct]);
191 // See if current action can be coalesced into previous action
192 // Will work if both are inserts or deletes and position is same
193 #if defined(_MSC_VER) && defined(_PREFAST_)
194 // Visual Studio 2013 Code Analysis wrongly believes actions can be NULL at its next reference
195 __analysis_assume(actions);
196 #endif
197 if (currentAction == savePoint) {
198 currentAction++;
199 } else if (!actions[currentAction].mayCoalesce) {
200 // Not allowed to coalesce if this set
201 currentAction++;
202 } else if (!mayCoalesce || !actPrevious->mayCoalesce) {
203 currentAction++;
204 } else if (at == containerAction || actions[currentAction].at == containerAction) {
205 ; // A coalescible containerAction
206 } else if ((at != actPrevious->at) && (actPrevious->at != startAction)) {
207 currentAction++;
208 } else if ((at == insertAction) &&
209 (position != (actPrevious->position + actPrevious->lenData))) {
210 // Insertions must be immediately after to coalesce
211 currentAction++;
212 } else if (at == removeAction) {
213 if ((lengthData == 1) || (lengthData == 2)) {
214 if ((position + lengthData) == actPrevious->position) {
215 ; // Backspace -> OK
216 } else if (position == actPrevious->position) {
217 ; // Delete -> OK
218 } else {
219 // Removals must be at same position to coalesce
220 currentAction++;
222 } else {
223 // Removals must be of one character to coalesce
224 currentAction++;
226 } else {
227 // Action coalesced.
230 } else {
231 // Actions not at top level are always coalesced unless this is after return to top level
232 if (!actions[currentAction].mayCoalesce)
233 currentAction++;
235 } else {
236 currentAction++;
238 startSequence = oldCurrentAction != currentAction;
239 int actionWithData = currentAction;
240 actions[currentAction].Create(at, position, data, lengthData, mayCoalesce);
241 currentAction++;
242 actions[currentAction].Create(startAction);
243 maxAction = currentAction;
244 return actions[actionWithData].data;
247 void UndoHistory::BeginUndoAction() {
248 EnsureUndoRoom();
249 if (undoSequenceDepth == 0) {
250 if (actions[currentAction].at != startAction) {
251 currentAction++;
252 actions[currentAction].Create(startAction);
253 maxAction = currentAction;
255 actions[currentAction].mayCoalesce = false;
257 undoSequenceDepth++;
260 void UndoHistory::EndUndoAction() {
261 PLATFORM_ASSERT(undoSequenceDepth > 0);
262 EnsureUndoRoom();
263 undoSequenceDepth--;
264 if (0 == undoSequenceDepth) {
265 if (actions[currentAction].at != startAction) {
266 currentAction++;
267 actions[currentAction].Create(startAction);
268 maxAction = currentAction;
270 actions[currentAction].mayCoalesce = false;
274 void UndoHistory::DropUndoSequence() {
275 undoSequenceDepth = 0;
278 void UndoHistory::DeleteUndoHistory() {
279 for (int i = 1; i < maxAction; i++)
280 actions[i].Destroy();
281 maxAction = 0;
282 currentAction = 0;
283 actions[currentAction].Create(startAction);
284 savePoint = 0;
287 void UndoHistory::SetSavePoint() {
288 savePoint = currentAction;
291 bool UndoHistory::IsSavePoint() const {
292 return savePoint == currentAction;
295 bool UndoHistory::CanUndo() const {
296 return (currentAction > 0) && (maxAction > 0);
299 int UndoHistory::StartUndo() {
300 // Drop any trailing startAction
301 if (actions[currentAction].at == startAction && currentAction > 0)
302 currentAction--;
304 // Count the steps in this action
305 int act = currentAction;
306 while (actions[act].at != startAction && act > 0) {
307 act--;
309 return currentAction - act;
312 const Action &UndoHistory::GetUndoStep() const {
313 return actions[currentAction];
316 void UndoHistory::CompletedUndoStep() {
317 currentAction--;
320 bool UndoHistory::CanRedo() const {
321 return maxAction > currentAction;
324 int UndoHistory::StartRedo() {
325 // Drop any leading startAction
326 if (actions[currentAction].at == startAction && currentAction < maxAction)
327 currentAction++;
329 // Count the steps in this action
330 int act = currentAction;
331 while (actions[act].at != startAction && act < maxAction) {
332 act++;
334 return act - currentAction;
337 const Action &UndoHistory::GetRedoStep() const {
338 return actions[currentAction];
341 void UndoHistory::CompletedRedoStep() {
342 currentAction++;
345 CellBuffer::CellBuffer() {
346 readOnly = false;
347 utf8LineEnds = 0;
348 collectingUndo = true;
351 CellBuffer::~CellBuffer() {
354 char CellBuffer::CharAt(int position) const {
355 return substance.ValueAt(position);
358 void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) const {
359 if (lengthRetrieve < 0)
360 return;
361 if (position < 0)
362 return;
363 if ((position + lengthRetrieve) > substance.Length()) {
364 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", position,
365 lengthRetrieve, substance.Length());
366 return;
368 substance.GetRange(buffer, position, lengthRetrieve);
371 char CellBuffer::StyleAt(int position) const {
372 return style.ValueAt(position);
375 void CellBuffer::GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const {
376 if (lengthRetrieve < 0)
377 return;
378 if (position < 0)
379 return;
380 if ((position + lengthRetrieve) > style.Length()) {
381 Platform::DebugPrintf("Bad GetStyleRange %d for %d of %d\n", position,
382 lengthRetrieve, style.Length());
383 return;
385 style.GetRange(reinterpret_cast<char *>(buffer), position, lengthRetrieve);
388 const char *CellBuffer::BufferPointer() {
389 return substance.BufferPointer();
392 const char *CellBuffer::RangePointer(int position, int rangeLength) {
393 return substance.RangePointer(position, rangeLength);
396 int CellBuffer::GapPosition() const {
397 return substance.GapPosition();
400 // The char* returned is to an allocation owned by the undo history
401 const char *CellBuffer::InsertString(int position, const char *s, int insertLength, bool &startSequence) {
402 // InsertString and DeleteChars are the bottleneck though which all changes occur
403 const char *data = s;
404 if (!readOnly) {
405 if (collectingUndo) {
406 // Save into the undo/redo stack, but only the characters - not the formatting
407 // This takes up about half load time
408 data = uh.AppendAction(insertAction, position, s, insertLength, startSequence);
411 BasicInsertString(position, s, insertLength);
413 return data;
416 bool CellBuffer::SetStyleAt(int position, char styleValue, char mask) {
417 styleValue &= mask;
418 char curVal = style.ValueAt(position);
419 if ((curVal & mask) != styleValue) {
420 style.SetValueAt(position, static_cast<char>((curVal & ~mask) | styleValue));
421 return true;
422 } else {
423 return false;
427 bool CellBuffer::SetStyleFor(int position, int lengthStyle, char styleValue, char mask) {
428 bool changed = false;
429 PLATFORM_ASSERT(lengthStyle == 0 ||
430 (lengthStyle > 0 && lengthStyle + position <= style.Length()));
431 while (lengthStyle--) {
432 char curVal = style.ValueAt(position);
433 if ((curVal & mask) != styleValue) {
434 style.SetValueAt(position, static_cast<char>((curVal & ~mask) | styleValue));
435 changed = true;
437 position++;
439 return changed;
442 // The char* returned is to an allocation owned by the undo history
443 const char *CellBuffer::DeleteChars(int position, int deleteLength, bool &startSequence) {
444 // InsertString and DeleteChars are the bottleneck though which all changes occur
445 PLATFORM_ASSERT(deleteLength > 0);
446 const char *data = 0;
447 if (!readOnly) {
448 if (collectingUndo) {
449 // Save into the undo/redo stack, but only the characters - not the formatting
450 // The gap would be moved to position anyway for the deletion so this doesn't cost extra
451 data = substance.RangePointer(position, deleteLength);
452 data = uh.AppendAction(removeAction, position, data, deleteLength, startSequence);
455 BasicDeleteChars(position, deleteLength);
457 return data;
460 int CellBuffer::Length() const {
461 return substance.Length();
464 void CellBuffer::Allocate(int newSize) {
465 substance.ReAllocate(newSize);
466 style.ReAllocate(newSize);
469 void CellBuffer::SetLineEndTypes(int utf8LineEnds_) {
470 if (utf8LineEnds != utf8LineEnds_) {
471 utf8LineEnds = utf8LineEnds_;
472 ResetLineEnds();
476 void CellBuffer::SetPerLine(PerLine *pl) {
477 lv.SetPerLine(pl);
480 int CellBuffer::Lines() const {
481 return lv.Lines();
484 int CellBuffer::LineStart(int line) const {
485 if (line < 0)
486 return 0;
487 else if (line >= Lines())
488 return Length();
489 else
490 return lv.LineStart(line);
493 bool CellBuffer::IsReadOnly() const {
494 return readOnly;
497 void CellBuffer::SetReadOnly(bool set) {
498 readOnly = set;
501 void CellBuffer::SetSavePoint() {
502 uh.SetSavePoint();
505 bool CellBuffer::IsSavePoint() const {
506 return uh.IsSavePoint();
509 // Without undo
511 void CellBuffer::InsertLine(int line, int position, bool lineStart) {
512 lv.InsertLine(line, position, lineStart);
515 void CellBuffer::RemoveLine(int line) {
516 lv.RemoveLine(line);
519 bool CellBuffer::UTF8LineEndOverlaps(int position) const {
520 unsigned char bytes[] = {
521 static_cast<unsigned char>(substance.ValueAt(position-2)),
522 static_cast<unsigned char>(substance.ValueAt(position-1)),
523 static_cast<unsigned char>(substance.ValueAt(position)),
524 static_cast<unsigned char>(substance.ValueAt(position+1)),
526 return UTF8IsSeparator(bytes) || UTF8IsSeparator(bytes+1) || UTF8IsNEL(bytes+1);
529 void CellBuffer::ResetLineEnds() {
530 // Reinitialize line data -- too much work to preserve
531 lv.Init();
533 int position = 0;
534 int length = Length();
535 int lineInsert = 1;
536 bool atLineStart = true;
537 lv.InsertText(lineInsert-1, length);
538 unsigned char chBeforePrev = 0;
539 unsigned char chPrev = 0;
540 for (int i = 0; i < length; i++) {
541 unsigned char ch = substance.ValueAt(position + i);
542 if (ch == '\r') {
543 InsertLine(lineInsert, (position + i) + 1, atLineStart);
544 lineInsert++;
545 } else if (ch == '\n') {
546 if (chPrev == '\r') {
547 // Patch up what was end of line
548 lv.SetLineStart(lineInsert - 1, (position + i) + 1);
549 } else {
550 InsertLine(lineInsert, (position + i) + 1, atLineStart);
551 lineInsert++;
553 } else if (utf8LineEnds) {
554 unsigned char back3[3] = {chBeforePrev, chPrev, ch};
555 if (UTF8IsSeparator(back3) || UTF8IsNEL(back3+1)) {
556 InsertLine(lineInsert, (position + i) + 1, atLineStart);
557 lineInsert++;
560 chBeforePrev = chPrev;
561 chPrev = ch;
565 void CellBuffer::BasicInsertString(int position, const char *s, int insertLength) {
566 if (insertLength == 0)
567 return;
568 PLATFORM_ASSERT(insertLength > 0);
570 unsigned char chAfter = substance.ValueAt(position);
571 bool breakingUTF8LineEnd = false;
572 if (utf8LineEnds && UTF8IsTrailByte(chAfter)) {
573 breakingUTF8LineEnd = UTF8LineEndOverlaps(position);
576 substance.InsertFromArray(position, s, 0, insertLength);
577 style.InsertValue(position, insertLength, 0);
579 int lineInsert = lv.LineFromPosition(position) + 1;
580 bool atLineStart = lv.LineStart(lineInsert-1) == position;
581 // Point all the lines after the insertion point further along in the buffer
582 lv.InsertText(lineInsert-1, insertLength);
583 unsigned char chBeforePrev = substance.ValueAt(position - 2);
584 unsigned char chPrev = substance.ValueAt(position - 1);
585 if (chPrev == '\r' && chAfter == '\n') {
586 // Splitting up a crlf pair at position
587 InsertLine(lineInsert, position, false);
588 lineInsert++;
590 if (breakingUTF8LineEnd) {
591 RemoveLine(lineInsert);
593 unsigned char ch = ' ';
594 for (int i = 0; i < insertLength; i++) {
595 ch = s[i];
596 if (ch == '\r') {
597 InsertLine(lineInsert, (position + i) + 1, atLineStart);
598 lineInsert++;
599 } else if (ch == '\n') {
600 if (chPrev == '\r') {
601 // Patch up what was end of line
602 lv.SetLineStart(lineInsert - 1, (position + i) + 1);
603 } else {
604 InsertLine(lineInsert, (position + i) + 1, atLineStart);
605 lineInsert++;
607 } else if (utf8LineEnds) {
608 unsigned char back3[3] = {chBeforePrev, chPrev, ch};
609 if (UTF8IsSeparator(back3) || UTF8IsNEL(back3+1)) {
610 InsertLine(lineInsert, (position + i) + 1, atLineStart);
611 lineInsert++;
614 chBeforePrev = chPrev;
615 chPrev = ch;
617 // Joining two lines where last insertion is cr and following substance starts with lf
618 if (chAfter == '\n') {
619 if (ch == '\r') {
620 // End of line already in buffer so drop the newly created one
621 RemoveLine(lineInsert - 1);
623 } else if (utf8LineEnds && !UTF8IsAscii(chAfter)) {
624 // May have end of UTF-8 line end in buffer and start in insertion
625 for (int j = 0; j < UTF8SeparatorLength-1; j++) {
626 unsigned char chAt = substance.ValueAt(position + insertLength + j);
627 unsigned char back3[3] = {chBeforePrev, chPrev, chAt};
628 if (UTF8IsSeparator(back3)) {
629 InsertLine(lineInsert, (position + insertLength + j) + 1, atLineStart);
630 lineInsert++;
632 if ((j == 0) && UTF8IsNEL(back3+1)) {
633 InsertLine(lineInsert, (position + insertLength + j) + 1, atLineStart);
634 lineInsert++;
636 chBeforePrev = chPrev;
637 chPrev = chAt;
642 void CellBuffer::BasicDeleteChars(int position, int deleteLength) {
643 if (deleteLength == 0)
644 return;
646 if ((position == 0) && (deleteLength == substance.Length())) {
647 // If whole buffer is being deleted, faster to reinitialise lines data
648 // than to delete each line.
649 lv.Init();
650 } else {
651 // Have to fix up line positions before doing deletion as looking at text in buffer
652 // to work out which lines have been removed
654 int lineRemove = lv.LineFromPosition(position) + 1;
655 lv.InsertText(lineRemove-1, - (deleteLength));
656 unsigned char chPrev = substance.ValueAt(position - 1);
657 unsigned char chBefore = chPrev;
658 unsigned char chNext = substance.ValueAt(position);
659 bool ignoreNL = false;
660 if (chPrev == '\r' && chNext == '\n') {
661 // Move back one
662 lv.SetLineStart(lineRemove, position);
663 lineRemove++;
664 ignoreNL = true; // First \n is not real deletion
666 if (utf8LineEnds && UTF8IsTrailByte(chNext)) {
667 if (UTF8LineEndOverlaps(position)) {
668 RemoveLine(lineRemove);
672 unsigned char ch = chNext;
673 for (int i = 0; i < deleteLength; i++) {
674 chNext = substance.ValueAt(position + i + 1);
675 if (ch == '\r') {
676 if (chNext != '\n') {
677 RemoveLine(lineRemove);
679 } else if (ch == '\n') {
680 if (ignoreNL) {
681 ignoreNL = false; // Further \n are real deletions
682 } else {
683 RemoveLine(lineRemove);
685 } else if (utf8LineEnds) {
686 if (!UTF8IsAscii(ch)) {
687 unsigned char next3[3] = {ch, chNext,
688 static_cast<unsigned char>(substance.ValueAt(position + i + 2))};
689 if (UTF8IsSeparator(next3) || UTF8IsNEL(next3)) {
690 RemoveLine(lineRemove);
695 ch = chNext;
697 // May have to fix up end if last deletion causes cr to be next to lf
698 // or removes one of a crlf pair
699 char chAfter = substance.ValueAt(position + deleteLength);
700 if (chBefore == '\r' && chAfter == '\n') {
701 // Using lineRemove-1 as cr ended line before start of deletion
702 RemoveLine(lineRemove - 1);
703 lv.SetLineStart(lineRemove - 1, position + 1);
706 substance.DeleteRange(position, deleteLength);
707 style.DeleteRange(position, deleteLength);
710 bool CellBuffer::SetUndoCollection(bool collectUndo) {
711 collectingUndo = collectUndo;
712 uh.DropUndoSequence();
713 return collectingUndo;
716 bool CellBuffer::IsCollectingUndo() const {
717 return collectingUndo;
720 void CellBuffer::BeginUndoAction() {
721 uh.BeginUndoAction();
724 void CellBuffer::EndUndoAction() {
725 uh.EndUndoAction();
728 void CellBuffer::AddUndoAction(int token, bool mayCoalesce) {
729 bool startSequence;
730 uh.AppendAction(containerAction, token, 0, 0, startSequence, mayCoalesce);
733 void CellBuffer::DeleteUndoHistory() {
734 uh.DeleteUndoHistory();
737 bool CellBuffer::CanUndo() const {
738 return uh.CanUndo();
741 int CellBuffer::StartUndo() {
742 return uh.StartUndo();
745 const Action &CellBuffer::GetUndoStep() const {
746 return uh.GetUndoStep();
749 void CellBuffer::PerformUndoStep() {
750 const Action &actionStep = uh.GetUndoStep();
751 if (actionStep.at == insertAction) {
752 BasicDeleteChars(actionStep.position, actionStep.lenData);
753 } else if (actionStep.at == removeAction) {
754 BasicInsertString(actionStep.position, actionStep.data, actionStep.lenData);
756 uh.CompletedUndoStep();
759 bool CellBuffer::CanRedo() const {
760 return uh.CanRedo();
763 int CellBuffer::StartRedo() {
764 return uh.StartRedo();
767 const Action &CellBuffer::GetRedoStep() const {
768 return uh.GetRedoStep();
771 void CellBuffer::PerformRedoStep() {
772 const Action &actionStep = uh.GetRedoStep();
773 if (actionStep.at == insertAction) {
774 BasicInsertString(actionStep.position, actionStep.data, actionStep.lenData);
775 } else if (actionStep.at == removeAction) {
776 BasicDeleteChars(actionStep.position, actionStep.lenData);
778 uh.CompletedRedoStep();