-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTextManager.cs
More file actions
454 lines (413 loc) · 17.3 KB
/
TextManager.cs
File metadata and controls
454 lines (413 loc) · 17.3 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ST.Library.UI.STTextBox
{
public class TextManager : IEnumerable<TextLine>
{
public int LineCount {
get { return m_nLineCount; }
}
public int TextLength {
get { return m_nTextLength; }
}
public TextLine this[int nIndex] {
get {
if (nIndex < 0 || nIndex >= m_nLineCount) {
throw new ArgumentOutOfRangeException("nIndex");
}
return m_lines[nIndex];
}
}
private int m_nLineCount;
private int m_nTextLength;
private TextLine[] m_lines;
public TextManager() {
m_lines = new TextLine[2];
m_lines[0] = new TextLine();
m_nLineCount++;
}
public event TextManagerLineEventHandler LineAdded;
public event TextManagerLineEventHandler LineRemoved;
public event TextManagerLineEventHandler LineChanged;
public event TextManagerTextEventHandler TextChanged;
public event TextManagerTextEventHandler TextStartChange;
public event EventHandler LineCountChanged;
#region Event
protected virtual void OnTextStartChange(TextManagerTextEventArgs e) {
if (this.TextStartChange != null) {
this.TextStartChange(this, e);
}
}
protected virtual void OnLineAdded(TextManagerLineEventArgs e) {
if (this.LineAdded != null) {
this.LineAdded(this, e);
}
}
protected virtual void OnRemoveLine(TextManagerLineEventArgs e) {
if (this.LineRemoved != null) {
this.LineRemoved(this, e);
}
}
protected virtual void OnTextChanged(TextManagerTextEventArgs e) {
if (this.TextChanged != null) {
this.TextChanged(this, e);
}
}
protected virtual void OnLineChanged(TextManagerLineEventArgs e) {
if (this.LineChanged != null) {
this.LineChanged(this, e);
}
}
protected virtual void OnLineCountChanged(EventArgs e) {
if (this.LineCountChanged != null) {
this.LineCountChanged(this, e);
}
}
#endregion
public string GetText() {
return this.GetStringBuilder(0, m_nTextLength).ToString();
}
public string GetText(int nIndex, int nLen) {
return this.GetStringBuilder(nIndex, nLen).ToString();
}
public StringBuilder GetStringBuilder() {
return this.GetStringBuilder(0, m_nTextLength);
}
public StringBuilder GetStringBuilder(int nIndex, int nLen) {
StringBuilder sb = new StringBuilder();
if (nLen == 0) {
return sb;
}
int nIndexStart = this.GetLineIndexFromCharIndex(nIndex);
int nIndexEnd = this.GetLineIndexFromCharIndex(nIndex + nLen);
TextLine stLineStart = this[nIndexStart];
TextLine stLineEnd = this[nIndexEnd];
if (stLineStart == stLineEnd) {
sb.Append(stLineStart.RawString.Substring(nIndex - stLineStart.IndexOfFirstChar, nLen));
return sb;
}
int nLenAdded = 0;
sb.Append(stLineStart.RawString.Substring(nIndex - stLineStart.IndexOfFirstChar));
nLenAdded += sb.Length;
for (int i = nIndexStart + 1; i < nIndexEnd; i++) {
sb.Append(this[i].RawString);
nLenAdded += this[i].RawString.Length;
}
sb.Append(stLineEnd.RawString.Substring(0, nLen - nLenAdded));
return sb;
}
public TextHistoryRecord SetText(string strText) {
return this.SetText(0, m_nTextLength, strText);
}
public TextHistoryRecord SetText(int nIndex, string strText) {
return this.SetText(nIndex, 0, strText);
}
/// <summary>
/// Modify the source text
/// </summary>
/// <param name="nIndex">The index from source string</param>
/// <param name="nLen">The length of the text to be deleted</param>
/// <param name="strText">The text that will be add</param>
public TextHistoryRecord SetText(int nIndex, int nLen, string strText) {
return this.SetText(nIndex, nLen, strText, false);
}
public TextHistoryRecord[] RunHistory(TextHistoryRecord[] histories) {
if (histories == null || histories.Length == 0) {
return new TextHistoryRecord[0];
}
int nLineCount = m_nLineCount;
this.OnTextStartChange(new TextManagerTextEventArgs(new List<TextHistoryRecord>(histories)));
var ret = new TextHistoryRecord[histories.Length];
for (int i = 0; i < histories.Length; i++) {
var h = histories[i];
ret[i] = this.SetText(h.Index, h.OldText.Length, h.NewText, true);
}
if (m_nLineCount != nLineCount) {
this.OnLineCountChanged(EventArgs.Empty);
}
this.OnTextChanged(new TextManagerTextEventArgs(new List<TextHistoryRecord>(ret)));
return ret;
}
public int Clear() {
int nRet = m_nTextLength;
m_lines = new TextLine[2];
m_lines[0] = new TextLine();
m_nLineCount = 1;
m_nTextLength = 0;
return nRet;
}
public TextLine GetLineFromCharIndex(int nIndex) {
nIndex = this.GetLineIndexFromCharIndex(nIndex);
return m_lines[nIndex];
}
public int GetLineIndexFromCharIndex(int nIndex) {
if (m_nLineCount == 0) {
return -1;
}
if (nIndex <= 0) {
return 0;
}
if (nIndex >= m_lines[m_nLineCount - 1].IndexOfFirstChar + m_lines[m_nLineCount - 1].RawString.Length) {
return m_nLineCount - 1;
}
int nLeft = 0, nRight = m_nLineCount - 1, nMid = 0;
while (nLeft <= nRight) {
nMid = (nRight + nLeft) >> 1;
if (nIndex >= m_lines[nMid].IndexOfFirstChar + m_lines[nMid].RawString.Length) {
nLeft = nMid + 1;
} else if (nIndex < m_lines[nMid].IndexOfFirstChar) {
nRight = nMid - 1;
} else {
return nMid;
}
}
return -1;
}
public int GetLineIndexFromLine(TextLine line) {
int nLeft = 0, nRight = m_nLineCount - 1, nMid = 0;
while (nLeft <= nRight) {
nMid = (nLeft + nRight) >> 1;
if (line.IndexOfFirstChar < m_lines[nMid].IndexOfFirstChar) {
nRight = nMid - 1;
} else if (line.IndexOfFirstChar > m_lines[nMid].IndexOfFirstChar) {
nLeft = nMid + 1;
} else {
if (m_lines[nMid] == line) {
return nMid;
}
return -1;
}
}
return -1;
}
public TextHistoryRecord[] InsertAtStart(int nLineIndex, int nLineCount, string strText) {
if (nLineIndex < 0) {
nLineCount += nLineCount;
nLineIndex = 0;
}
if (nLineIndex >= m_nLineCount) {
return new TextHistoryRecord[0];
}
if (nLineIndex + nLineCount > m_nLineCount) {
nLineCount = m_nLineCount - nLineIndex;
}
TextHistoryRecord[] lst = new TextHistoryRecord[nLineCount];
this.OnTextStartChange(new TextManagerTextEventArgs(new List<TextHistoryRecord>(lst)));
int nAddCounter = 0;
for (int i = 0; i < nLineCount; i++) {
var line = m_lines[nLineIndex + i];
line.RawString = strText + line.RawString;
line.IndexOfFirstChar += nAddCounter;
var thr = new TextHistoryRecord() {
Index = line.IndexOfFirstChar,
OldText = string.Empty,
NewText = strText
};
lst[i] = thr;
this.OnLineChanged(new TextManagerLineEventArgs(nLineIndex + i, line, thr));
nAddCounter += strText.Length;
}
for (int i = nLineIndex + nLineCount; i < m_nLineCount; i++) {
m_lines[i].IndexOfFirstChar += nAddCounter;
}
m_nTextLength += nAddCounter;
this.OnTextChanged(new TextManagerTextEventArgs(new List<TextHistoryRecord>(lst)));
return lst;
}
public List<TextHistoryRecord> TrimStartTab(int nLineIndex, int nLineCount, int nTabSize) {
List<TextHistoryRecord> lst = new List<TextHistoryRecord>();
if (nLineIndex < 0) {
nLineCount += nLineCount;
nLineIndex = 0;
}
if (nLineIndex >= m_nLineCount) {
return lst;
}
if (nLineIndex + nLineCount > m_nLineCount) {
nLineCount = m_nLineCount - nLineIndex;
}
bool bStarted = false;
int nRemoveCounter = 0, nRemoveLen = 0;
for (int i = 0; i < nLineCount; i++) {
nRemoveLen = 0;
var thr = new TextHistoryRecord();
var line = m_lines[nLineIndex + i];
if (line.RawString[0] == '\t') {
nRemoveLen = 1;
} else {
while (nRemoveLen < nTabSize) {
switch (line.RawString[nRemoveLen]) {
case ' ':
nRemoveLen++;
continue;
case '\t':
nRemoveLen++;
continue;
default:
break;
}
break;
}
}
line.IndexOfFirstChar -= nRemoveCounter;
if (nRemoveLen == 0) continue;
if (!bStarted) {
this.OnTextStartChange(new TextManagerTextEventArgs(lst));
bStarted = true;
}
thr.OldText = line.RawString.Substring(0, nRemoveLen);
thr.Index = line.IndexOfFirstChar;
thr.NewText = "";
line.RawString = line.RawString.Substring(nRemoveLen);
nRemoveCounter += nRemoveLen;
this.OnLineChanged(new TextManagerLineEventArgs(nLineIndex + i, line, thr));
lst.Add(thr);
}
if (lst.Count == 0) return lst;
for (int i = nLineIndex + nLineCount; i < m_nLineCount; i++) {
m_lines[i].IndexOfFirstChar -= nRemoveCounter;
}
m_nTextLength -= nRemoveCounter;
this.OnTextChanged(new TextManagerTextEventArgs(lst));
return lst;
}
#region Private
private TextHistoryRecord SetText(int nIndex, int nLen, string strText, bool isHistory) {
if (strText == null) {
strText = "";
}
string strOld = string.Empty;
if (nLen != 0) {
strOld = this.GetText(nIndex, nLen);
}
if (strOld == strText) {
return TextHistoryRecord.Empty;
}
var historyRecord = new TextHistoryRecord() {
Index = nIndex,
OldText = strOld,
NewText = strText
};
if (!isHistory) {
var lstHistory = new List<TextHistoryRecord>() { historyRecord };
this.OnTextStartChange(new TextManagerTextEventArgs(lstHistory));
historyRecord = lstHistory[0];
}
List<TextLine> lst = this.GetTexLines(historyRecord.NewText);
int nIndexStartLine = this.GetLineIndexFromCharIndex(nIndex);
int nIndexEndLine = this.GetLineIndexFromCharIndex(nIndex + nLen);
var line_start = m_lines[nIndexStartLine];
var line_end = m_lines[nIndexEndLine];
string strLeft = line_start.RawString.Substring(0, nIndex - line_start.IndexOfFirstChar);
string strRight = line_end.RawString.Substring(nIndex + nLen - line_end.IndexOfFirstChar);
lst[0].RawString = strLeft + lst[0].RawString;
lst[0].IndexOfFirstChar = line_start.IndexOfFirstChar;
lst[lst.Count - 1].RawString += strRight;
int nIncrement = historyRecord.NewText.Length - nLen;
m_nTextLength += nIncrement;
line_start.RawString = lst[0].RawString;
this.OnLineChanged(new TextManagerLineEventArgs(strLeft.Length, line_start, historyRecord));
int nShouldRemoveLines = nIndexEndLine - nIndexStartLine;
int nShouldAddLines = lst.Count - 1 - nShouldRemoveLines;
for (int i = nIndexStartLine + 1; i <= nIndexEndLine; i++) {
this.OnRemoveLine(new TextManagerLineEventArgs(i, m_lines[i], historyRecord));
}
if (nShouldAddLines > 0) {
this.InsertEmptyLines(nIndexStartLine, nShouldAddLines, nIncrement);
} else {
this.RemoveLines(nIndexStartLine + 1, -nShouldAddLines, nIncrement, historyRecord);
}
for (int i = 1, j = nIndexStartLine + 1; i < lst.Count; i++, j++) {
var prev_line = m_lines[j - 1];
m_lines[j] = lst[i];
m_lines[j].IndexOfFirstChar = prev_line.IndexOfFirstChar + prev_line.RawString.Length;
this.OnLineAdded(new TextManagerLineEventArgs(j, m_lines[j], historyRecord));
}
if (nShouldAddLines == 0) {
for (int i = nIndexStartLine + 1; i < m_nLineCount; i++) {
TextLine prve_line = m_lines[i - 1];
m_lines[i].IndexOfFirstChar = prve_line.IndexOfFirstChar + prve_line.RawString.Length;
}
} else {
m_nLineCount += nShouldAddLines;
if (!isHistory) {
this.OnLineCountChanged(EventArgs.Empty);
}
}
if (!isHistory) {
this.OnTextChanged(new TextManagerTextEventArgs(new List<TextHistoryRecord>() { historyRecord }));
}
return historyRecord;
}
private List<TextLine> GetTexLines(string strText) {
int nIndexStart = 0, nLen = 0;
TextLine line = new TextLine();
List<TextLine> lst = new List<TextLine>();
lst.Add(line);
for (int i = 0; i < strText.Length; i++) {
char c = strText[i];
nLen++;
if (c == '\n' || c == '\r') {
if (c == '\r' && i + 1 < strText.Length && strText[i + 1] == '\n') {
nLen++;
i++;
}
line.RawString = strText.Substring(nIndexStart, nLen);
line = new TextLine();
lst.Add(line);
nIndexStart = i + 1;
nLen = 0;
}
}
if (nLen != 0) {
line.RawString = strText.Substring(nIndexStart, nLen);
}
return lst;
}
private void InsertEmptyLines(int nIndex, int nCount, int nOffsetIncrement) {
if (nCount == 0) return;
this.EnsureSpace(nCount);
for (int i = m_nLineCount + nCount - 1; i > nIndex + nCount; i--) {
m_lines[i] = m_lines[i - nCount];
if (m_lines[i] == null) {
m_lines[i] = new TextLine();
}
m_lines[i].IndexOfFirstChar += nOffsetIncrement;
}
}
private void RemoveLines(int nIndex, int nCount, int nOffsetIncrement, TextHistoryRecord history) {
if (nCount == 0) return;
int nCounter = nCount;
for (int i = nIndex; i < m_nLineCount - nCount; i++) {
if (nCounter > 0) {
nCounter--;
}
m_lines[i] = m_lines[i + nCount];
m_lines[i].IndexOfFirstChar += nOffsetIncrement;
}
}
private void EnsureSpace(int nCount) {
if (m_nLineCount + nCount <= m_lines.Length) {
return;
}
int nLen = m_nLineCount + nCount;
TextLine[] new_arr = new TextLine[Math.Max(m_nLineCount + nCount, m_lines.Length << 1)];
Array.Copy(m_lines, new_arr, m_lines.Length);
m_lines = new_arr;
}
#endregion
#region Interface
public IEnumerator<TextLine> GetEnumerator() {
for (int i = 0; i < m_nLineCount; i++) {
yield return m_lines[i];
}
}
IEnumerator IEnumerable.GetEnumerator() {
return this.GetEnumerator();
}
#endregion
}
}