-
Notifications
You must be signed in to change notification settings - Fork 673
/
TextView.cs
4540 lines (3908 loc) · 125 KB
/
TextView.cs
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TextView.cs: multi-line text editing
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using NStack;
using Terminal.Gui.Resources;
using static Terminal.Gui.Graphs.PathAnnotation;
using Rune = System.Rune;
namespace Terminal.Gui {
class TextModel {
List<List<Rune>> lines = new List<List<Rune>> ();
public event Action LinesLoaded;
public bool LoadFile (string file)
{
FilePath = file ?? throw new ArgumentNullException (nameof (file));
var stream = File.OpenRead (file);
LoadStream (stream);
return true;
}
public bool CloseFile ()
{
if (FilePath == null)
throw new ArgumentNullException (nameof (FilePath));
FilePath = null;
lines = new List<List<Rune>> ();
return true;
}
// Turns the ustring into runes, this does not split the
// contents on a newline if it is present.
internal static List<Rune> ToRunes (ustring str)
{
List<Rune> runes = new List<Rune> ();
foreach (var x in str.ToRunes ()) {
runes.Add (x);
}
return runes;
}
// Splits a string into a List that contains a List<Rune> for each line
public static List<List<Rune>> StringToRunes (ustring content)
{
var lines = new List<List<Rune>> ();
int start = 0, i = 0;
var hasCR = false;
// ASCII code 13 = Carriage Return.
// ASCII code 10 = Line Feed.
for (; i < content.Length; i++) {
if (content [i] == 13) {
hasCR = true;
continue;
}
if (content [i] == 10) {
if (i - start > 0)
lines.Add (ToRunes (content [start, hasCR ? i - 1 : i]));
else
lines.Add (ToRunes (ustring.Empty));
start = i + 1;
hasCR = false;
}
}
if (i - start >= 0)
lines.Add (ToRunes (content [start, null]));
return lines;
}
void Append (List<byte> line)
{
var str = ustring.Make (line.ToArray ());
lines.Add (ToRunes (str));
}
public void LoadStream (Stream input)
{
if (input == null)
throw new ArgumentNullException (nameof (input));
lines = new List<List<Rune>> ();
var buff = new BufferedStream (input);
int v;
var line = new List<byte> ();
var wasNewLine = false;
while ((v = buff.ReadByte ()) != -1) {
if (v == 13) {
continue;
}
if (v == 10) {
Append (line);
line.Clear ();
wasNewLine = true;
continue;
}
line.Add ((byte)v);
wasNewLine = false;
}
if (line.Count > 0 || wasNewLine)
Append (line);
buff.Dispose ();
OnLinesLoaded ();
}
public void LoadString (ustring content)
{
lines = StringToRunes (content);
OnLinesLoaded ();
}
void OnLinesLoaded ()
{
LinesLoaded?.Invoke ();
}
public override string ToString ()
{
var sb = new StringBuilder ();
for (int i = 0; i < lines.Count; i++) {
sb.Append (ustring.Make (lines [i]));
if ((i + 1) < lines.Count) {
sb.AppendLine ();
}
}
return sb.ToString ();
}
public string FilePath { get; set; }
/// <summary>
/// The number of text lines in the model
/// </summary>
public int Count => lines.Count;
/// <summary>
/// Returns the specified line as a List of Rune
/// </summary>
/// <returns>The line.</returns>
/// <param name="line">Line number to retrieve.</param>
public List<Rune> GetLine (int line)
{
if (lines.Count > 0) {
if (line < Count) {
return lines [line];
} else {
return lines [Count - 1];
}
} else {
lines.Add (new List<Rune> ());
return lines [0];
}
}
/// <summary>
/// Adds a line to the model at the specified position.
/// </summary>
/// <param name="pos">Line number where the line will be inserted.</param>
/// <param name="runes">The line of text, as a List of Rune.</param>
public void AddLine (int pos, List<Rune> runes)
{
lines.Insert (pos, runes);
}
/// <summary>
/// Removes the line at the specified position
/// </summary>
/// <param name="pos">Position.</param>
public void RemoveLine (int pos)
{
if (lines.Count > 0) {
if (lines.Count == 1 && lines [0].Count == 0) {
return;
}
lines.RemoveAt (pos);
}
}
public void ReplaceLine (int pos, List<Rune> runes)
{
if (lines.Count > 0 && pos < lines.Count) {
lines [pos] = new List<Rune> (runes);
} else if (lines.Count == 0 || (lines.Count > 0 && pos >= lines.Count)) {
lines.Add (runes);
}
}
/// <summary>
/// Returns the maximum line length of the visible lines.
/// </summary>
/// <param name="first">The first line.</param>
/// <param name="last">The last line.</param>
/// <param name="tabWidth">The tab width.</param>
public int GetMaxVisibleLine (int first, int last, int tabWidth)
{
int maxLength = 0;
last = last < lines.Count ? last : lines.Count;
for (int i = first; i < last; i++) {
var line = GetLine (i);
var tabSum = line.Sum (r => r == '\t' ? Math.Max (tabWidth - 1, 0) : 0);
var l = line.Count + tabSum;
if (l > maxLength) {
maxLength = l;
}
}
return maxLength;
}
internal static bool SetCol (ref int col, int width, int cols)
{
if (col + cols <= width) {
col += cols;
return true;
}
return false;
}
internal static int GetColFromX (List<Rune> t, int start, int x, int tabWidth = 0)
{
if (x < 0) {
return x;
}
int size = start;
var pX = x + start;
for (int i = start; i < t.Count; i++) {
var r = t [i];
size += Rune.ColumnWidth (r);
if (r == '\t') {
size += tabWidth + 1;
}
if (i == pX || (size > pX)) {
return i - start;
}
}
return t.Count - start;
}
// Returns the size and length in a range of the string.
internal static (int size, int length) DisplaySize (List<Rune> t, int start = -1, int end = -1,
bool checkNextRune = true, int tabWidth = 0)
{
if (t == null || t.Count == 0) {
return (0, 0);
}
int size = 0;
int len = 0;
int tcount = end == -1 ? t.Count : end > t.Count ? t.Count : end;
int i = start == -1 ? 0 : start;
for (; i < tcount; i++) {
var rune = t [i];
size += Rune.ColumnWidth (rune);
len += Rune.RuneLen (rune);
if (rune == '\t') {
size += tabWidth + 1;
len += tabWidth - 1;
}
if (checkNextRune && i == tcount - 1 && t.Count > tcount
&& IsWideRune (t [i + 1], tabWidth, out int s, out int l)) {
size += s;
len += l;
}
}
bool IsWideRune (Rune r, int tWidth, out int s, out int l)
{
s = Rune.ColumnWidth (r);
l = Rune.RuneLen (r);
if (r == '\t') {
s += tWidth + 1;
l += tWidth - 1;
}
return s > 1;
}
return (size, len);
}
// Returns the left column in a range of the string.
internal static int CalculateLeftColumn (List<Rune> t, int start, int end, int width, int tabWidth = 0)
{
if (t == null || t.Count == 0) {
return 0;
}
int size = 0;
int tcount = end > t.Count - 1 ? t.Count - 1 : end;
int col = 0;
for (int i = tcount; i >= 0; i--) {
var rune = t [i];
size += Rune.ColumnWidth (rune);
if (rune == '\t') {
size += tabWidth + 1;
}
if (size > width) {
if (col + width == end) {
col++;
}
break;
} else if ((end < t.Count && col > 0 && start < end && col == start) || (end - col == width - 1)) {
break;
}
col = i;
}
return col;
}
(Point startPointToFind, Point currentPointToFind, bool found) toFind;
internal (Point current, bool found) FindNextText (ustring text, out bool gaveFullTurn, bool matchCase = false, bool matchWholeWord = false)
{
if (text == null || lines.Count == 0) {
gaveFullTurn = false;
return (Point.Empty, false);
}
if (toFind.found) {
toFind.currentPointToFind.X++;
}
var foundPos = GetFoundNextTextPoint (text, lines.Count, matchCase, matchWholeWord, toFind.currentPointToFind);
if (!foundPos.found && toFind.currentPointToFind != toFind.startPointToFind) {
foundPos = GetFoundNextTextPoint (text, toFind.startPointToFind.Y + 1, matchCase, matchWholeWord, Point.Empty);
}
gaveFullTurn = ApplyToFind (foundPos);
return foundPos;
}
internal (Point current, bool found) FindPreviousText (ustring text, out bool gaveFullTurn, bool matchCase = false, bool matchWholeWord = false)
{
if (text == null || lines.Count == 0) {
gaveFullTurn = false;
return (Point.Empty, false);
}
if (toFind.found) {
toFind.currentPointToFind.X++;
}
var linesCount = toFind.currentPointToFind.IsEmpty ? lines.Count - 1 : toFind.currentPointToFind.Y;
var foundPos = GetFoundPreviousTextPoint (text, linesCount, matchCase, matchWholeWord, toFind.currentPointToFind);
if (!foundPos.found && toFind.currentPointToFind != toFind.startPointToFind) {
foundPos = GetFoundPreviousTextPoint (text, lines.Count - 1, matchCase, matchWholeWord,
new Point (lines [lines.Count - 1].Count, lines.Count));
}
gaveFullTurn = ApplyToFind (foundPos);
return foundPos;
}
internal (Point current, bool found) ReplaceAllText (ustring text, bool matchCase = false, bool matchWholeWord = false, ustring textToReplace = null)
{
bool found = false;
Point pos = Point.Empty;
for (int i = 0; i < lines.Count; i++) {
var x = lines [i];
var txt = GetText (x);
var matchText = !matchCase ? text.ToUpper ().ToString () : text.ToString ();
var col = txt.IndexOf (matchText);
while (col > -1) {
if (matchWholeWord && !MatchWholeWord (txt, matchText, col)) {
if (col + 1 > txt.Length) {
break;
}
col = txt.IndexOf (matchText, col + 1);
continue;
}
if (col > -1) {
if (!found) {
found = true;
}
lines [i] = ReplaceText (x, textToReplace, matchText, col).ToRuneList ();
x = lines [i];
txt = GetText (x);
pos = new Point (col, i);
col += (textToReplace.Length - matchText.Length);
}
if (col < 0 || col + 1 > txt.Length) {
break;
}
col = txt.IndexOf (matchText, col + 1);
}
}
string GetText (List<Rune> x)
{
var txt = ustring.Make (x).ToString ();
if (!matchCase) {
txt = txt.ToUpper ();
}
return txt;
}
return (pos, found);
}
ustring ReplaceText (List<Rune> source, ustring textToReplace, string matchText, int col)
{
var origTxt = ustring.Make (source);
(int _, int len) = TextModel.DisplaySize (source, 0, col, false);
(var _, var len2) = TextModel.DisplaySize (source, col, col + matchText.Length, false);
(var _, var len3) = TextModel.DisplaySize (source, col + matchText.Length, origTxt.RuneCount, false);
return origTxt [0, len] +
textToReplace.ToString () +
origTxt [len + len2, len + len2 + len3];
}
bool ApplyToFind ((Point current, bool found) foundPos)
{
bool gaveFullTurn = false;
if (foundPos.found) {
toFind.currentPointToFind = foundPos.current;
if (toFind.found && toFind.currentPointToFind == toFind.startPointToFind) {
gaveFullTurn = true;
}
if (!toFind.found) {
toFind.startPointToFind = toFind.currentPointToFind = foundPos.current;
toFind.found = foundPos.found;
}
}
return gaveFullTurn;
}
(Point current, bool found) GetFoundNextTextPoint (ustring text, int linesCount, bool matchCase, bool matchWholeWord, Point start)
{
for (int i = start.Y; i < linesCount; i++) {
var x = lines [i];
var txt = ustring.Make (x).ToString ();
if (!matchCase) {
txt = txt.ToUpper ();
}
var matchText = !matchCase ? text.ToUpper ().ToString () : text.ToString ();
var col = txt.IndexOf (matchText, Math.Min (start.X, txt.Length));
if (col > -1 && matchWholeWord && !MatchWholeWord (txt, matchText, col)) {
continue;
}
if (col > -1 && ((i == start.Y && col >= start.X)
|| i > start.Y)
&& txt.Contains (matchText)) {
return (new Point (col, i), true);
} else if (col == -1 && start.X > 0) {
start.X = 0;
}
}
return (Point.Empty, false);
}
(Point current, bool found) GetFoundPreviousTextPoint (ustring text, int linesCount, bool matchCase, bool matchWholeWord, Point start)
{
for (int i = linesCount; i >= 0; i--) {
var x = lines [i];
var txt = ustring.Make (x).ToString ();
if (!matchCase) {
txt = txt.ToUpper ();
}
if (start.Y != i) {
start.X = Math.Max (x.Count - 1, 0);
}
var matchText = !matchCase ? text.ToUpper ().ToString () : text.ToString ();
var col = txt.LastIndexOf (matchText, toFind.found ? start.X - 1 : start.X);
if (col > -1 && matchWholeWord && !MatchWholeWord (txt, matchText, col)) {
continue;
}
if (col > -1 && ((i <= linesCount && col <= start.X)
|| i < start.Y)
&& txt.Contains (matchText)) {
return (new Point (col, i), true);
}
}
return (Point.Empty, false);
}
bool MatchWholeWord (string source, string matchText, int index = 0)
{
if (string.IsNullOrEmpty (source) || string.IsNullOrEmpty (matchText)) {
return false;
}
var txt = matchText.Trim ();
var start = index > 0 ? index - 1 : 0;
var end = index + txt.Length;
if ((start == 0 || Rune.IsWhiteSpace (source [start]))
&& (end == source.Length || Rune.IsWhiteSpace (source [end]))) {
return true;
}
return false;
}
/// <summary>
/// Redefine column and line tracking.
/// </summary>
/// <param name="point">Contains the column and line.</param>
internal void ResetContinuousFind (Point point)
{
toFind.startPointToFind = toFind.currentPointToFind = point;
toFind.found = false;
}
}
class HistoryText {
public enum LineStatus {
Original,
Replaced,
Removed,
Added
}
public class HistoryTextItem {
public List<List<Rune>> Lines;
public Point CursorPosition;
public LineStatus LineStatus;
public bool IsUndoing;
public Point FinalCursorPosition;
public HistoryTextItem RemovedOnAdded;
public HistoryTextItem (List<List<Rune>> lines, Point curPos, LineStatus linesStatus)
{
Lines = lines;
CursorPosition = curPos;
LineStatus = linesStatus;
}
public HistoryTextItem (HistoryTextItem historyTextItem)
{
Lines = new List<List<Rune>> (historyTextItem.Lines);
CursorPosition = new Point (historyTextItem.CursorPosition.X, historyTextItem.CursorPosition.Y);
LineStatus = historyTextItem.LineStatus;
}
public override string ToString ()
{
return $"(Count: {Lines.Count}, Cursor: {CursorPosition}, Status: {LineStatus})";
}
}
List<HistoryTextItem> historyTextItems = new List<HistoryTextItem> ();
int idxHistoryText = -1;
ustring originalText;
public bool IsFromHistory { get; private set; }
public bool HasHistoryChanges => idxHistoryText > -1;
public event Action<HistoryTextItem> ChangeText;
public void Add (List<List<Rune>> lines, Point curPos, LineStatus lineStatus = LineStatus.Original)
{
if (lineStatus == LineStatus.Original && historyTextItems.Count > 0
&& historyTextItems.Last ().LineStatus == LineStatus.Original) {
return;
}
if (lineStatus == LineStatus.Replaced && historyTextItems.Count > 0
&& historyTextItems.Last ().LineStatus == LineStatus.Replaced) {
return;
}
if (historyTextItems.Count == 0 && lineStatus != LineStatus.Original)
throw new ArgumentException ("The first item must be the original.");
if (idxHistoryText >= 0 && idxHistoryText + 1 < historyTextItems.Count)
historyTextItems.RemoveRange (idxHistoryText + 1, historyTextItems.Count - idxHistoryText - 1);
historyTextItems.Add (new HistoryTextItem (lines, curPos, lineStatus));
idxHistoryText++;
}
public void ReplaceLast (List<List<Rune>> lines, Point curPos, LineStatus lineStatus)
{
var found = historyTextItems.FindLast (x => x.LineStatus == lineStatus);
if (found != null) {
found.Lines = lines;
found.CursorPosition = curPos;
}
}
public void Undo ()
{
if (historyTextItems?.Count > 0 && idxHistoryText > 0) {
IsFromHistory = true;
idxHistoryText--;
var historyTextItem = new HistoryTextItem (historyTextItems [idxHistoryText]) {
IsUndoing = true
};
ProcessChanges (ref historyTextItem);
IsFromHistory = false;
}
}
public void Redo ()
{
if (historyTextItems?.Count > 0 && idxHistoryText < historyTextItems.Count - 1) {
IsFromHistory = true;
idxHistoryText++;
var historyTextItem = new HistoryTextItem (historyTextItems [idxHistoryText]) {
IsUndoing = false
};
ProcessChanges (ref historyTextItem);
IsFromHistory = false;
}
}
void ProcessChanges (ref HistoryTextItem historyTextItem)
{
if (historyTextItem.IsUndoing) {
if (idxHistoryText - 1 > -1 && ((historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Added)
|| historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Removed
|| (historyTextItem.LineStatus == LineStatus.Replaced &&
historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Original))) {
idxHistoryText--;
while (historyTextItems [idxHistoryText].LineStatus == LineStatus.Added
&& historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Removed) {
idxHistoryText--;
}
historyTextItem = new HistoryTextItem (historyTextItems [idxHistoryText]);
historyTextItem.IsUndoing = true;
historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
}
if (historyTextItem.LineStatus == LineStatus.Removed && historyTextItems [idxHistoryText + 1].LineStatus == LineStatus.Added) {
historyTextItem.RemovedOnAdded = new HistoryTextItem (historyTextItems [idxHistoryText + 1]);
}
if ((historyTextItem.LineStatus == LineStatus.Added && historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Original)
|| (historyTextItem.LineStatus == LineStatus.Removed && historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Original)
|| (historyTextItem.LineStatus == LineStatus.Added && historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Removed)) {
if (!historyTextItem.Lines [0].SequenceEqual (historyTextItems [idxHistoryText - 1].Lines [0])
&& historyTextItem.CursorPosition == historyTextItems [idxHistoryText - 1].CursorPosition) {
historyTextItem.Lines [0] = new List<Rune> (historyTextItems [idxHistoryText - 1].Lines [0]);
}
if (historyTextItem.LineStatus == LineStatus.Added && historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Removed) {
historyTextItem.FinalCursorPosition = historyTextItems [idxHistoryText - 2].CursorPosition;
} else {
historyTextItem.FinalCursorPosition = historyTextItems [idxHistoryText - 1].CursorPosition;
}
} else {
historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
}
OnChangeText (historyTextItem);
while (historyTextItems [idxHistoryText].LineStatus == LineStatus.Removed
|| historyTextItems [idxHistoryText].LineStatus == LineStatus.Added) {
idxHistoryText--;
}
} else if (!historyTextItem.IsUndoing) {
if (idxHistoryText + 1 < historyTextItems.Count && (historyTextItem.LineStatus == LineStatus.Original
|| historyTextItems [idxHistoryText + 1].LineStatus == LineStatus.Added
|| historyTextItems [idxHistoryText + 1].LineStatus == LineStatus.Removed)) {
idxHistoryText++;
historyTextItem = new HistoryTextItem (historyTextItems [idxHistoryText]);
historyTextItem.IsUndoing = false;
historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
}
if (historyTextItem.LineStatus == LineStatus.Added && historyTextItems [idxHistoryText - 1].LineStatus == LineStatus.Removed) {
historyTextItem.RemovedOnAdded = new HistoryTextItem (historyTextItems [idxHistoryText - 1]);
}
if ((historyTextItem.LineStatus == LineStatus.Removed && historyTextItems [idxHistoryText + 1].LineStatus == LineStatus.Replaced)
|| (historyTextItem.LineStatus == LineStatus.Removed && historyTextItems [idxHistoryText + 1].LineStatus == LineStatus.Original)
|| (historyTextItem.LineStatus == LineStatus.Added && historyTextItems [idxHistoryText + 1].LineStatus == LineStatus.Replaced)) {
if (historyTextItem.LineStatus == LineStatus.Removed
&& !historyTextItem.Lines [0].SequenceEqual (historyTextItems [idxHistoryText + 1].Lines [0])) {
historyTextItem.Lines [0] = new List<Rune> (historyTextItems [idxHistoryText + 1].Lines [0]);
}
historyTextItem.FinalCursorPosition = historyTextItems [idxHistoryText + 1].CursorPosition;
} else {
historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
}
OnChangeText (historyTextItem);
while (historyTextItems [idxHistoryText].LineStatus == LineStatus.Removed
|| historyTextItems [idxHistoryText].LineStatus == LineStatus.Added) {
idxHistoryText++;
}
}
}
void OnChangeText (HistoryTextItem lines)
{
ChangeText?.Invoke (lines);
}
public void Clear (ustring text)
{
historyTextItems.Clear ();
idxHistoryText = -1;
originalText = text;
OnChangeText (null);
}
public bool IsDirty (ustring text)
{
return originalText != text;
}
}
class WordWrapManager {
class WrappedLine {
public int ModelLine;
public int Row;
public int RowIndex;
public int ColWidth;
}
List<WrappedLine> wrappedModelLines = new List<WrappedLine> ();
int frameWidth;
bool isWrapModelRefreshing;
public TextModel Model { get; private set; }
public WordWrapManager (TextModel model)
{
Model = model;
}
public TextModel WrapModel (int width, out int nRow, out int nCol, out int nStartRow, out int nStartCol,
int row = 0, int col = 0, int startRow = 0, int startCol = 0, int tabWidth = 0, bool preserveTrailingSpaces = true)
{
frameWidth = width;
var modelRow = isWrapModelRefreshing ? row : GetModelLineFromWrappedLines (row);
var modelCol = isWrapModelRefreshing ? col : GetModelColFromWrappedLines (row, col);
var modelStartRow = isWrapModelRefreshing ? startRow : GetModelLineFromWrappedLines (startRow);
var modelStartCol = isWrapModelRefreshing ? startCol : GetModelColFromWrappedLines (startRow, startCol);
var wrappedModel = new TextModel ();
int lines = 0;
nRow = 0;
nCol = 0;
nStartRow = 0;
nStartCol = 0;
bool isRowAndColSetted = row == 0 && col == 0;
bool isStartRowAndColSetted = startRow == 0 && startCol == 0;
List<WrappedLine> wModelLines = new List<WrappedLine> ();
for (int i = 0; i < Model.Count; i++) {
var line = Model.GetLine (i);
var wrappedLines = ToListRune (
TextFormatter.Format (ustring.Make (line), width, TextAlignment.Left, true, preserveTrailingSpaces, tabWidth));
int sumColWidth = 0;
for (int j = 0; j < wrappedLines.Count; j++) {
var wrapLine = wrappedLines [j];
if (!isRowAndColSetted && modelRow == i) {
if (nCol + wrapLine.Count <= modelCol) {
nCol += wrapLine.Count;
nRow = lines;
if (nCol == modelCol) {
nCol = wrapLine.Count;
isRowAndColSetted = true;
} else if (j == wrappedLines.Count - 1) {
nCol = wrapLine.Count - j + modelCol - nCol;
isRowAndColSetted = true;
}
} else {
var offset = nCol + wrapLine.Count - modelCol;
nCol = wrapLine.Count - offset;
nRow = lines;
isRowAndColSetted = true;
}
}
if (!isStartRowAndColSetted && modelStartRow == i) {
if (nStartCol + wrapLine.Count <= modelStartCol) {
nStartCol += wrapLine.Count;
nStartRow = lines;
if (nStartCol == modelStartCol) {
nStartCol = wrapLine.Count;
isStartRowAndColSetted = true;
} else if (j == wrappedLines.Count - 1) {
nStartCol = wrapLine.Count - j + modelStartCol - nStartCol;
isStartRowAndColSetted = true;
}
} else {
var offset = nStartCol + wrapLine.Count - modelStartCol;
nStartCol = wrapLine.Count - offset;
nStartRow = lines;
isStartRowAndColSetted = true;
}
}
wrappedModel.AddLine (lines, wrapLine);
sumColWidth += wrapLine.Count;
var wrappedLine = new WrappedLine () {
ModelLine = i,
Row = lines,
RowIndex = j,
ColWidth = wrapLine.Count,
};
wModelLines.Add (wrappedLine);
lines++;
}
}
wrappedModelLines = wModelLines;
return wrappedModel;
}
public List<List<Rune>> ToListRune (List<ustring> textList)
{
var runesList = new List<List<Rune>> ();
foreach (var text in textList) {
runesList.Add (text.ToRuneList ());
}
return runesList;
}
public int GetModelLineFromWrappedLines (int line) => wrappedModelLines.Count > 0
? wrappedModelLines [Math.Min (line, wrappedModelLines.Count - 1)].ModelLine
: 0;
public int GetModelColFromWrappedLines (int line, int col)
{
if (wrappedModelLines?.Count == 0) {
return 0;
}
var modelLine = GetModelLineFromWrappedLines (line);
var firstLine = wrappedModelLines.IndexOf (r => r.ModelLine == modelLine);
int modelCol = 0;
for (int i = firstLine; i <= Math.Min (line, wrappedModelLines.Count - 1); i++) {
var wLine = wrappedModelLines [i];
if (i < line) {
modelCol += wLine.ColWidth;
} else {
modelCol += col;
}
}
return modelCol;
}
List<Rune> GetCurrentLine (int row) => Model.GetLine (row);
public void AddLine (int row, int col)
{
var modelRow = GetModelLineFromWrappedLines (row);
var modelCol = GetModelColFromWrappedLines (row, col);
var line = GetCurrentLine (modelRow);
var restCount = line.Count - modelCol;
var rest = line.GetRange (modelCol, restCount);
line.RemoveRange (modelCol, restCount);
Model.AddLine (modelRow + 1, rest);
isWrapModelRefreshing = true;
WrapModel (frameWidth, out _, out _, out _, out _, modelRow + 1, 0);
isWrapModelRefreshing = false;
}
public bool Insert (int row, int col, Rune rune)
{
var line = GetCurrentLine (GetModelLineFromWrappedLines (row));
line.Insert (GetModelColFromWrappedLines (row, col), rune);
if (line.Count > frameWidth) {
return true;
} else {
return false;
}
}
public bool RemoveAt (int row, int col)
{
var modelRow = GetModelLineFromWrappedLines (row);
var line = GetCurrentLine (modelRow);
var modelCol = GetModelColFromWrappedLines (row, col);
if (modelCol > line.Count) {
Model.RemoveLine (modelRow);
RemoveAt (row, 0);
return false;
}
if (modelCol < line.Count)
line.RemoveAt (modelCol);
if (line.Count > frameWidth || (row + 1 < wrappedModelLines.Count
&& wrappedModelLines [row + 1].ModelLine == modelRow)) {
return true;
}
return false;
}
public bool RemoveLine (int row, int col, out bool lineRemoved, bool forward = true)
{
lineRemoved = false;
var modelRow = GetModelLineFromWrappedLines (row);
var line = GetCurrentLine (modelRow);
var modelCol = GetModelColFromWrappedLines (row, col);
if (modelCol == 0 && line.Count == 0) {
Model.RemoveLine (modelRow);
return false;
} else if (modelCol < line.Count) {
if (forward) {
line.RemoveAt (modelCol);
return true;
} else if (modelCol - 1 > -1) {
line.RemoveAt (modelCol - 1);
return true;
}
}
lineRemoved = true;
if (forward) {
if (modelRow + 1 == Model.Count) {
return false;
}
var nextLine = Model.GetLine (modelRow + 1);
line.AddRange (nextLine);
Model.RemoveLine (modelRow + 1);
if (line.Count > frameWidth) {
return true;
}
} else {
if (modelRow == 0) {
return false;
}
var prevLine = Model.GetLine (modelRow - 1);
prevLine.AddRange (line);
Model.RemoveLine (modelRow);
if (prevLine.Count > frameWidth) {
return true;
}
}
return false;
}
public bool RemoveRange (int row, int index, int count)
{
var modelRow = GetModelLineFromWrappedLines (row);
var line = GetCurrentLine (modelRow);
var modelCol = GetModelColFromWrappedLines (row, index);
try {
line.RemoveRange (modelCol, count);
} catch (Exception) {
return false;
}
return true;
}
public void UpdateModel (TextModel model, out int nRow, out int nCol, out int nStartRow, out int nStartCol,
int row, int col, int startRow, int startCol, bool preserveTrailingSpaces)
{
isWrapModelRefreshing = true;
Model = model;
WrapModel (frameWidth, out nRow, out nCol, out nStartRow, out nStartCol, row, col, startRow, startCol, tabWidth: 0, preserveTrailingSpaces);
isWrapModelRefreshing = false;
}
public int GetWrappedLineColWidth (int line, int col, WordWrapManager wrapManager)
{
if (wrappedModelLines?.Count == 0)
return 0;
var wModelLines = wrapManager.wrappedModelLines;
var modelLine = GetModelLineFromWrappedLines (line);
var firstLine = wrappedModelLines.IndexOf (r => r.ModelLine == modelLine);
int modelCol = 0;
int colWidthOffset = 0;
int i = firstLine;
while (modelCol < col) {
var wLine = wrappedModelLines [i];
var wLineToCompare = wModelLines [i];