forked from gitextensions/ICSharpCode.TextEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiscActions.cs
1011 lines (896 loc) · 40.8 KB
/
MiscActions.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
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="[email protected]"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Diagnostics;
using System.Text;
using ICSharpCode.TextEditor.Document;
namespace ICSharpCode.TextEditor.Actions
{
public class Tab : AbstractEditAction
{
public static string GetIndentationString(IDocument document)
{
return GetIndentationString(document, textArea: null);
}
public static string GetIndentationString(IDocument document, TextArea textArea)
{
var indent = new StringBuilder();
if (document.TextEditorProperties.ConvertTabsToSpaces)
{
var tabIndent = document.TextEditorProperties.IndentationSize;
if (textArea != null)
{
var column = textArea.TextView.GetVisualColumn(textArea.Caret.Line, textArea.Caret.Column);
indent.Append(new string(c: ' ', tabIndent - column%tabIndent));
}
else
{
indent.Append(new string(c: ' ', tabIndent));
}
}
else
{
indent.Append(value: '\t');
}
return indent.ToString();
}
private static void InsertTabs(IDocument document, ISelection selection, int y1, int y2)
{
var indentationString = GetIndentationString(document);
for (var i = y2; i >= y1; --i)
{
var line = document.GetLineSegment(i);
if (i == y2 && i == selection.EndPosition.Y && selection.EndPosition.X == 0)
continue;
// this bit is optional - but useful if you are using block tabbing to sort out
// a source file with a mixture of tabs and spaces
// string newLine = document.GetText(line.Offset,line.Length);
// document.Replace(line.Offset,line.Length,newLine);
document.Insert(line.Offset, indentationString);
}
}
private static void InsertTabAtCaretPosition(TextArea textArea)
{
switch (textArea.Caret.CaretMode)
{
case CaretMode.InsertMode:
textArea.InsertString(GetIndentationString(textArea.Document, textArea));
break;
case CaretMode.OverwriteMode:
var indentStr = GetIndentationString(textArea.Document, textArea);
textArea.ReplaceChar(indentStr[index: 0]);
if (indentStr.Length > 1)
textArea.InsertString(indentStr.Substring(startIndex: 1));
break;
}
textArea.SetDesiredColumn();
}
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.SelectionManager.SelectionIsReadonly)
return;
textArea.Document.UndoStack.StartUndoGroup();
if (textArea.SelectionManager.HasSomethingSelected)
{
foreach (var selection in textArea.SelectionManager.SelectionCollection)
{
var startLine = selection.StartPosition.Y;
var endLine = selection.EndPosition.Y;
if (startLine != endLine)
{
textArea.BeginUpdate();
InsertTabs(textArea.Document, selection, startLine, endLine);
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, startLine, endLine));
textArea.EndUpdate();
}
else
{
InsertTabAtCaretPosition(textArea);
break;
}
}
textArea.Document.CommitUpdate();
textArea.AutoClearSelection = false;
}
else
{
InsertTabAtCaretPosition(textArea);
}
textArea.Document.UndoStack.EndUndoGroup();
}
}
public class ShiftTab : AbstractEditAction
{
private static void RemoveTabs(IDocument document, ISelection selection, int y1, int y2)
{
document.UndoStack.StartUndoGroup();
for (var i = y2; i >= y1; --i)
{
var line = document.GetLineSegment(i);
if (i == y2 && line.Offset == selection.EndOffset)
continue;
if (line.Length > 0)
{
var charactersToRemove = 0;
if (document.GetCharAt(line.Offset) == '\t')
{
// first character is a tab - just remove it
charactersToRemove = 1;
}
else if (document.GetCharAt(line.Offset) == ' ')
{
int leadingSpaces;
var tabIndent = document.TextEditorProperties.IndentationSize;
for (leadingSpaces = 1;
leadingSpaces < line.Length && document.GetCharAt(line.Offset + leadingSpaces) == ' ';
leadingSpaces++)
{
// deliberately empty
}
if (leadingSpaces >= tabIndent)
charactersToRemove = tabIndent;
else if (line.Length > leadingSpaces && document.GetCharAt(line.Offset + leadingSpaces) == '\t')
charactersToRemove = leadingSpaces + 1;
else
charactersToRemove = leadingSpaces;
}
if (charactersToRemove > 0)
document.Remove(line.Offset, charactersToRemove);
}
}
document.UndoStack.EndUndoGroup();
}
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.SelectionManager.HasSomethingSelected)
{
foreach (var selection in textArea.SelectionManager.SelectionCollection)
{
var startLine = selection.StartPosition.Y;
var endLine = selection.EndPosition.Y;
textArea.BeginUpdate();
RemoveTabs(textArea.Document, selection, startLine, endLine);
textArea.Document.UpdateQueue.Clear();
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, startLine, endLine));
textArea.EndUpdate();
}
textArea.AutoClearSelection = false;
}
else
{
// Pressing Shift-Tab with nothing selected the cursor will move back to the
// previous tab stop. It will stop at the beginning of the line. Also, the desired
// column is updated to that column.
var line = textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
//var startOfLine = textArea.Document.GetText(line.Offset, textArea.Caret.Offset - line.Offset);
var tabIndent = textArea.Document.TextEditorProperties.IndentationSize;
var currentColumn = textArea.Caret.Column;
var remainder = currentColumn%tabIndent;
if (remainder == 0)
textArea.Caret.DesiredColumn = Math.Max(val1: 0, currentColumn - tabIndent);
else
textArea.Caret.DesiredColumn = Math.Max(val1: 0, currentColumn - remainder);
textArea.SetCaretToDesiredColumn();
}
}
}
public class ToggleComment : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.Document.ReadOnly)
return;
if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("LineComment"))
new ToggleLineComment().Execute(textArea);
else if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("BlockCommentBegin"))
new ToggleBlockComment().Execute(textArea);
}
}
public class ToggleLineComment : AbstractEditAction
{
private int firstLine;
private int lastLine;
private void RemoveCommentAt(IDocument document, string comment, ISelection selection, int y1, int y2)
{
firstLine = y1;
lastLine = y2;
for (var i = y2; i >= y1; --i)
{
var line = document.GetLineSegment(i);
if (selection != null && i == y2 && line.Offset == selection.Offset + selection.Length)
{
--lastLine;
continue;
}
var lineText = document.GetText(line.Offset, line.Length);
if (lineText.Trim().StartsWith(comment))
document.Remove(line.Offset + lineText.IndexOf(comment), comment.Length);
}
}
private void SetCommentAt(IDocument document, string comment, ISelection selection, int y1, int y2)
{
firstLine = y1;
lastLine = y2;
for (var i = y2; i >= y1; --i)
{
var line = document.GetLineSegment(i);
if (selection != null && i == y2 && line.Offset == selection.Offset + selection.Length)
{
--lastLine;
continue;
}
// var lineText = document.GetText(line.Offset, line.Length);
document.Insert(line.Offset, comment);
}
}
private bool ShouldComment(IDocument document, string comment, ISelection selection, int startLine, int endLine)
{
for (var i = endLine; i >= startLine; --i)
{
var line = document.GetLineSegment(i);
if (selection != null && i == endLine && line.Offset == selection.Offset + selection.Length)
{
--lastLine;
continue;
}
var lineText = document.GetText(line.Offset, line.Length);
if (!lineText.Trim().StartsWith(comment))
return true;
}
return false;
}
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.Document.ReadOnly)
return;
string comment = null;
if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("LineComment"))
comment = textArea.Document.HighlightingStrategy.Properties["LineComment"];
if (comment == null || comment.Length == 0)
return;
textArea.Document.UndoStack.StartUndoGroup();
if (textArea.SelectionManager.HasSomethingSelected)
{
var shouldComment = true;
foreach (var selection in textArea.SelectionManager.SelectionCollection)
if (!ShouldComment(textArea.Document, comment, selection, selection.StartPosition.Y, selection.EndPosition.Y))
{
shouldComment = false;
break;
}
foreach (var selection in textArea.SelectionManager.SelectionCollection)
{
textArea.BeginUpdate();
if (shouldComment)
SetCommentAt(textArea.Document, comment, selection, selection.StartPosition.Y, selection.EndPosition.Y);
else
RemoveCommentAt(textArea.Document, comment, selection, selection.StartPosition.Y, selection.EndPosition.Y);
textArea.Document.UpdateQueue.Clear();
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, firstLine, lastLine));
textArea.EndUpdate();
}
textArea.Document.CommitUpdate();
textArea.AutoClearSelection = false;
}
else
{
textArea.BeginUpdate();
var caretLine = textArea.Caret.Line;
if (ShouldComment(textArea.Document, comment, selection: null, caretLine, caretLine))
SetCommentAt(textArea.Document, comment, selection: null, caretLine, caretLine);
else
RemoveCommentAt(textArea.Document, comment, selection: null, caretLine, caretLine);
textArea.Document.UpdateQueue.Clear();
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.SingleLine, caretLine));
textArea.EndUpdate();
}
textArea.Document.UndoStack.EndUndoGroup();
}
}
public class ToggleBlockComment : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.Document.ReadOnly)
return;
string commentStart = null;
if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("BlockCommentBegin"))
commentStart = textArea.Document.HighlightingStrategy.Properties["BlockCommentBegin"];
string commentEnd = null;
if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("BlockCommentEnd"))
commentEnd = textArea.Document.HighlightingStrategy.Properties["BlockCommentEnd"];
if (commentStart == null || commentStart.Length == 0 || commentEnd == null || commentEnd.Length == 0)
return;
int selectionStartOffset;
int selectionEndOffset;
if (textArea.SelectionManager.HasSomethingSelected)
{
selectionStartOffset = textArea.SelectionManager.SelectionCollection[index: 0].Offset;
selectionEndOffset = textArea.SelectionManager.SelectionCollection[textArea.SelectionManager.SelectionCollection.Count - 1].EndOffset;
}
else
{
selectionStartOffset = textArea.Caret.Offset;
selectionEndOffset = selectionStartOffset;
}
var commentRegion = FindSelectedCommentRegion(textArea.Document, commentStart, commentEnd, selectionStartOffset, selectionEndOffset);
textArea.Document.UndoStack.StartUndoGroup();
if (commentRegion != null)
RemoveComment(textArea.Document, commentRegion);
else if (textArea.SelectionManager.HasSomethingSelected)
SetCommentAt(textArea.Document, selectionStartOffset, selectionEndOffset, commentStart, commentEnd);
textArea.Document.UndoStack.EndUndoGroup();
textArea.Document.CommitUpdate();
textArea.AutoClearSelection = false;
}
public static BlockCommentRegion FindSelectedCommentRegion(IDocument document, string commentStart, string commentEnd, int selectionStartOffset, int selectionEndOffset)
{
if (document.TextLength == 0)
return null;
// Find start of comment in selected text.
int commentEndOffset;
var selectedText = document.GetText(selectionStartOffset, selectionEndOffset - selectionStartOffset);
var commentStartOffset = selectedText.IndexOf(commentStart);
if (commentStartOffset >= 0)
commentStartOffset += selectionStartOffset;
// Find end of comment in selected text.
if (commentStartOffset >= 0)
commentEndOffset = selectedText.IndexOf(commentEnd, commentStartOffset + commentStart.Length - selectionStartOffset);
else
commentEndOffset = selectedText.IndexOf(commentEnd);
if (commentEndOffset >= 0)
commentEndOffset += selectionStartOffset;
// Find start of comment before or partially inside the
// selected text.
if (commentStartOffset == -1)
{
var offset = selectionEndOffset + commentStart.Length - 1;
if (offset > document.TextLength)
offset = document.TextLength;
var text = document.GetText(offset: 0, offset);
commentStartOffset = text.LastIndexOf(commentStart);
if (commentStartOffset >= 0)
{
// Find end of comment before comment start.
var commentEndBeforeStartOffset = text.IndexOf(commentEnd, commentStartOffset, selectionStartOffset - commentStartOffset);
if (commentEndBeforeStartOffset > commentStartOffset)
commentStartOffset = -1;
}
}
// Find end of comment after or partially after the
// selected text.
if (commentEndOffset == -1)
{
var offset = selectionStartOffset + 1 - commentEnd.Length;
if (offset < 0)
offset = selectionStartOffset;
var text = document.GetText(offset, document.TextLength - offset);
commentEndOffset = text.IndexOf(commentEnd);
if (commentEndOffset >= 0)
commentEndOffset += offset;
}
if (commentStartOffset != -1 && commentEndOffset != -1)
return new BlockCommentRegion(commentStart, commentEnd, commentStartOffset, commentEndOffset);
return null;
}
private static void SetCommentAt(IDocument document, int offsetStart, int offsetEnd, string commentStart, string commentEnd)
{
document.Insert(offsetEnd, commentEnd);
document.Insert(offsetStart, commentStart);
}
private static void RemoveComment(IDocument document, BlockCommentRegion commentRegion)
{
document.Remove(commentRegion.EndOffset, commentRegion.CommentEnd.Length);
document.Remove(commentRegion.StartOffset, commentRegion.CommentStart.Length);
}
}
public class BlockCommentRegion
{
/// <summary>
/// The end offset is the offset where the comment end string starts from.
/// </summary>
public BlockCommentRegion(string commentStart, string commentEnd, int startOffset, int endOffset)
{
CommentStart = commentStart;
CommentEnd = commentEnd;
StartOffset = startOffset;
EndOffset = endOffset;
}
public string CommentStart { get; } = string.Empty;
public string CommentEnd { get; } = string.Empty;
public int StartOffset { get; } = -1;
public int EndOffset { get; } = -1;
public override int GetHashCode()
{
var hashCode = 0;
unchecked
{
if (CommentStart != null) hashCode += 1000000007*CommentStart.GetHashCode();
if (CommentEnd != null) hashCode += 1000000009*CommentEnd.GetHashCode();
hashCode += 1000000021*StartOffset.GetHashCode();
hashCode += 1000000033*EndOffset.GetHashCode();
}
return hashCode;
}
public override bool Equals(object obj)
{
var other = obj as BlockCommentRegion;
if (other == null) return false;
return CommentStart == other.CommentStart && CommentEnd == other.CommentEnd && StartOffset == other.StartOffset && EndOffset == other.EndOffset;
}
}
public class Backspace : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.SelectionManager.HasSomethingSelected)
{
Delete.DeleteSelection(textArea);
}
else
{
if (textArea.Caret.Offset > 0 && !textArea.IsReadOnly(textArea.Caret.Offset - 1))
{
textArea.BeginUpdate();
var curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset);
var curLineOffset = textArea.Document.GetLineSegment(curLineNr).Offset;
if (curLineOffset == textArea.Caret.Offset)
{
var line = textArea.Document.GetLineSegment(curLineNr - 1);
// var lastLine = curLineNr == textArea.Document.TotalNumberOfLines;
var lineEndOffset = line.Offset + line.Length;
var lineLength = line.Length;
textArea.Document.Remove(lineEndOffset, curLineOffset - lineEndOffset);
textArea.Caret.Position = new TextLocation(lineLength, curLineNr - 1);
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(column: 0, curLineNr - 1)));
}
else
{
var caretOffset = textArea.Caret.Offset - 1;
textArea.Caret.Position = textArea.Document.OffsetToPosition(caretOffset);
textArea.Document.Remove(caretOffset, length: 1);
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToLineEnd, new TextLocation(textArea.Caret.Offset - textArea.Document.GetLineSegment(curLineNr).Offset, curLineNr)));
}
textArea.EndUpdate();
}
}
}
}
public class Delete : AbstractEditAction
{
internal static void DeleteSelection(TextArea textArea)
{
Debug.Assert(textArea.SelectionManager.HasSomethingSelected);
if (textArea.SelectionManager.SelectionIsReadonly)
return;
textArea.BeginUpdate();
textArea.Caret.Position = textArea.SelectionManager.SelectionCollection[index: 0].StartPosition;
textArea.SelectionManager.RemoveSelectedText();
textArea.ScrollToCaret();
textArea.EndUpdate();
}
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.SelectionManager.HasSomethingSelected)
{
DeleteSelection(textArea);
}
else
{
if (textArea.IsReadOnly(textArea.Caret.Offset))
return;
if (textArea.Caret.Offset < textArea.Document.TextLength)
{
textArea.BeginUpdate();
var curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset);
var curLine = textArea.Document.GetLineSegment(curLineNr);
if (curLine.Offset + curLine.Length == textArea.Caret.Offset)
{
if (curLineNr + 1 < textArea.Document.TotalNumberOfLines)
{
var nextLine = textArea.Document.GetLineSegment(curLineNr + 1);
textArea.Document.Remove(textArea.Caret.Offset, nextLine.Offset - textArea.Caret.Offset);
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(column: 0, curLineNr)));
}
}
else
{
textArea.Document.Remove(textArea.Caret.Offset, length: 1);
// textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToLineEnd, new TextLocation(textArea.Caret.Offset - textArea.Document.GetLineSegment(curLineNr).Offset, curLineNr)));
}
textArea.UpdateMatchingBracket();
textArea.EndUpdate();
}
}
}
}
public class MovePageDown : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
var curLineNr = textArea.Caret.Line;
var requestedLineNumber = Math.Min(textArea.Document.GetNextVisibleLineAbove(curLineNr, textArea.TextView.VisibleLineCount), textArea.Document.TotalNumberOfLines - 1);
if (curLineNr != requestedLineNumber)
{
textArea.Caret.Position = new TextLocation(column: 0, requestedLineNumber);
textArea.SetCaretToDesiredColumn();
}
}
}
public class MovePageUp : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
var curLineNr = textArea.Caret.Line;
var requestedLineNumber = Math.Max(textArea.Document.GetNextVisibleLineBelow(curLineNr, textArea.TextView.VisibleLineCount), val2: 0);
if (curLineNr != requestedLineNumber)
{
textArea.Caret.Position = new TextLocation(column: 0, requestedLineNumber);
textArea.SetCaretToDesiredColumn();
}
}
}
public class Space : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (!textArea.Document.ReadOnly)
return;
(new MovePageDown()).Execute(textArea);
}
}
public class ShiftSpace : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (!textArea.Document.ReadOnly)
return;
(new MovePageUp()).Execute(textArea);
}
}
public class Return : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="TextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.Document.ReadOnly)
return;
textArea.BeginUpdate();
textArea.Document.UndoStack.StartUndoGroup();
try
{
if (textArea.HandleKeyPress(ch: '\n'))
return;
textArea.InsertString(Environment.NewLine);
var curLineNr = textArea.Caret.Line;
textArea.Document.FormattingStrategy.FormatLine(textArea, curLineNr, textArea.Caret.Offset, charTyped: '\n');
textArea.SetDesiredColumn();
textArea.Document.UpdateQueue.Clear();
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(column: 0, curLineNr - 1)));
}
finally
{
textArea.Document.UndoStack.EndUndoGroup();
textArea.EndUpdate();
}
}
}
public class ToggleEditMode : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.Document.ReadOnly)
return;
switch (textArea.Caret.CaretMode)
{
case CaretMode.InsertMode:
textArea.Caret.CaretMode = CaretMode.OverwriteMode;
break;
case CaretMode.OverwriteMode:
textArea.Caret.CaretMode = CaretMode.InsertMode;
break;
}
}
}
public class Undo : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
textArea.MotherTextEditorControl.Undo();
}
}
public class Redo : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
textArea.MotherTextEditorControl.Redo();
}
}
/// <summary>
/// handles the ctrl-backspace key
/// functionality attempts to roughly mimic MS Developer studio
/// I will implement this as deleting back to the point that ctrl-leftarrow would
/// take you to
/// </summary>
public class WordBackspace : AbstractEditAction
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
// if anything is selected we will just delete it first
if (textArea.SelectionManager.HasSomethingSelected)
{
Delete.DeleteSelection(textArea);
return;
}
textArea.BeginUpdate();
// now delete from the caret to the beginning of the word
var line =
textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
// if we are not at the beginning of a line
if (textArea.Caret.Offset > line.Offset)
{
var prevWordStart = TextUtilities.FindPrevWordStart(
textArea.Document,
textArea.Caret.Offset);
if (prevWordStart < textArea.Caret.Offset)
if (!textArea.IsReadOnly(prevWordStart, textArea.Caret.Offset - prevWordStart))
{
textArea.Document.Remove(
prevWordStart,
textArea.Caret.Offset - prevWordStart);
textArea.Caret.Position = textArea.Document.OffsetToPosition(prevWordStart);
}
}
// if we are now at the beginning of a line
if (textArea.Caret.Offset == line.Offset)
{
// if we are not on the first line
var curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset);
if (curLineNr > 0)
{
// move to the end of the line above
var lineAbove = textArea.Document.GetLineSegment(curLineNr - 1);
var endOfLineAbove = lineAbove.Offset + lineAbove.Length;
var charsToDelete = textArea.Caret.Offset - endOfLineAbove;
if (!textArea.IsReadOnly(endOfLineAbove, charsToDelete))
{
textArea.Document.Remove(endOfLineAbove, charsToDelete);
textArea.Caret.Position = textArea.Document.OffsetToPosition(endOfLineAbove);
}
}
}
textArea.SetDesiredColumn();
textArea.EndUpdate();
// if there are now less lines, we need this or there are redraw problems
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(column: 0, textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset))));
textArea.Document.CommitUpdate();
}
}
/// <summary>
/// handles the ctrl-delete key
/// functionality attempts to mimic MS Developer studio
/// I will implement this as deleting forwardto the point that
/// ctrl-leftarrow would take you to
/// </summary>
public class DeleteWord : Delete
{
/// <remarks>
/// Executes this edit action
/// </remarks>
/// <param name="textArea">The <see cref="ItextArea" /> which is used for callback purposes</param>
public override void Execute(TextArea textArea)
{
if (textArea.SelectionManager.HasSomethingSelected)
{
DeleteSelection(textArea);
return;
}
// if anything is selected we will just delete it first
textArea.BeginUpdate();
// now delete from the caret to the beginning of the word
var line = textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
if (textArea.Caret.Offset == line.Offset + line.Length)
{
// if we are at the end of a line
base.Execute(textArea);
}
else
{
var nextWordStart = TextUtilities.FindNextWordStart(
textArea.Document,
textArea.Caret.Offset);
if (nextWordStart > textArea.Caret.Offset)
if (!textArea.IsReadOnly(textArea.Caret.Offset, nextWordStart - textArea.Caret.Offset))
textArea.Document.Remove(textArea.Caret.Offset, nextWordStart - textArea.Caret.Offset);
}
textArea.UpdateMatchingBracket();
textArea.EndUpdate();
// if there are now less lines, we need this or there are redraw problems
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(column: 0, textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset))));
textArea.Document.CommitUpdate();
}
}
public class DeleteLine : AbstractEditAction
{
public override void Execute(TextArea textArea)
{
var lineNr = textArea.Caret.Line;
var line = textArea.Document.GetLineSegment(lineNr);
if (textArea.IsReadOnly(line.Offset, line.Length))
return;
textArea.Document.Remove(line.Offset, line.TotalLength);
textArea.Caret.Position = textArea.Document.OffsetToPosition(line.Offset);
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(column: 0, lineNr)));
textArea.UpdateMatchingBracket();
textArea.Document.CommitUpdate();
}
}
public class MoveLineUp : AbstractEditAction
{
public override void Execute(TextArea textArea)
{
int caretInitialLine = textArea.Caret.Line;
int caretInitialColumn = textArea.Caret.Column;
if (MoveLine.TrySwitchLines(textArea, caretInitialLine - 1))
{
textArea.Caret.Position = new TextLocation(caretInitialColumn, caretInitialLine - 1);
}
}
}
public class MoveLineDown : AbstractEditAction
{
public override void Execute(TextArea textArea)
{
int caretInitialLine = textArea.Caret.Line;
int caretInitialColumn = textArea.Caret.Column;
if (MoveLine.TrySwitchLines(textArea, caretInitialLine))
{
textArea.Caret.Position = new TextLocation(caretInitialColumn, caretInitialLine + 1);
}
}
}
public static class MoveLine
{
public static bool TrySwitchLines(TextArea textArea, int firstLineIndex)
{
if (textArea.Document.ReadOnly
|| firstLineIndex >= textArea.Document.TotalNumberOfLines - 1
|| firstLineIndex < 0)
{
return false;
}
try
{
LineSegment firstLine = textArea.Document.GetLineSegment(firstLineIndex);
LineSegment secondLine = textArea.Document.GetLineSegment(firstLineIndex + 1);
string firstLineContent = textArea.Document.GetText(firstLine.Offset, firstLine.TotalLength);
string secondLineContent = textArea.Document.GetText(secondLine.Offset, secondLine.TotalLength);
// Handling of special case where last line that could have no eol char (that is taken from 1st line)
string newContent = secondLine.DelimiterLength != 0
? secondLineContent + firstLineContent
: secondLineContent
+ firstLineContent.Substring(firstLineContent.Length - firstLine.DelimiterLength)
+ firstLineContent.Substring(0, firstLineContent.Length - firstLine.DelimiterLength);
textArea.Document.Replace(firstLine.Offset, firstLine.TotalLength + secondLine.TotalLength, newContent);
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(column: 0, firstLineIndex)));
textArea.UpdateMatchingBracket();
textArea.Document.CommitUpdate();
return true;
}
catch (Exception)
{
// We don't want to crash for a non-essential feature...
return false;
}
}
}
public class DeleteToLineEnd : AbstractEditAction
{
public override void Execute(TextArea textArea)
{
var lineNr = textArea.Caret.Line;
var line = textArea.Document.GetLineSegment(lineNr);
var numRemove = line.Offset + line.Length - textArea.Caret.Offset;
if (numRemove > 0 && !textArea.IsReadOnly(textArea.Caret.Offset, numRemove))
{
textArea.Document.Remove(textArea.Caret.Offset, numRemove);
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.SingleLine, new TextLocation(column: 0, lineNr)));
textArea.Document.CommitUpdate();
}
}
}
public class GotoMatchingBrace : AbstractEditAction
{
public override void Execute(TextArea textArea)
{
var highlight = textArea.FindMatchingBracketHighlight();
if (highlight != null)
{
var p1 = new TextLocation(highlight.CloseBrace.X + 1, highlight.CloseBrace.Y);
var p2 = new TextLocation(highlight.OpenBrace.X + 1, highlight.OpenBrace.Y);
if (p1 == textArea.Caret.Position)
{
if (textArea.Document.TextEditorProperties.BracketMatchingStyle == BracketMatchingStyle.After)
textArea.Caret.Position = p2;
else
textArea.Caret.Position = new TextLocation(p2.X - 1, p2.Y);
}
else
{