Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TextEditor.cs « MonoDevelop.Ide.Editor « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 444f64fd049d97a18031630c3940896eee220cf2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
//
// ITextEditor.cs
//
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using MonoDevelop.Core.Text;
using System.Collections.Generic;
using System.Text;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Ide.Editor.Extension;
using System.IO;
using MonoDevelop.Ide.Editor.Highlighting;
using Mono.Addins;
using MonoDevelop.Core;
using MonoDevelop.Ide.Extensions;
using System.Linq;
using MonoDevelop.Components;
using System.ComponentModel;
using MonoDevelop.Ide.TypeSystem;
using System.Threading;
using MonoDevelop.Ide.Editor.Projection;

namespace MonoDevelop.Ide.Editor
{
	public sealed class TextEditor : Control, ITextDocument, IDisposable
	{
		readonly ITextEditorImpl textEditorImpl;
		IReadonlyTextDocument ReadOnlyTextDocument { get { return textEditorImpl.Document; } }
		ITextDocument ReadWriteTextDocument { get { return (ITextDocument)textEditorImpl.Document; } }

		public ITextSourceVersion Version {
			get {
				return ReadOnlyTextDocument.Version;
			}
		}

		FileTypeCondition fileTypeCondition = new FileTypeCondition ();

		List<TooltipExtensionNode> allProviders = new List<TooltipExtensionNode> ();

		void OnTooltipProviderChanged (object s, ExtensionNodeEventArgs a)
		{
			TooltipProvider provider;
			try {
				var extensionNode = a.ExtensionNode as TooltipExtensionNode;
				allProviders.Add (extensionNode);
				if (extensionNode.IsValidFor (MimeType))
					return;
				provider = (TooltipProvider) extensionNode.CreateInstance ();
			} catch (Exception e) {
				LoggingService.LogError ("Can't create tooltip provider:"+ a.ExtensionNode, e);
				return;
			}
			if (a.Change == ExtensionChange.Add) {
				textEditorImpl.AddTooltipProvider (provider);
			} else {
				textEditorImpl.RemoveTooltipProvider (provider);
			}
		}

		public event EventHandler SelectionChanged {
			add { textEditorImpl.SelectionChanged += value; }
			remove { textEditorImpl.SelectionChanged -= value; }
		}

		public event EventHandler CaretPositionChanged {
			add { textEditorImpl.CaretPositionChanged += value; }
			remove { textEditorImpl.CaretPositionChanged -= value; }
		}

		public event EventHandler BeginAtomicUndoOperation {
			add { textEditorImpl.BeginAtomicUndoOperation += value; }
			remove { textEditorImpl.BeginAtomicUndoOperation -= value; }
		}

		public event EventHandler EndAtomicUndoOperation {
			add { textEditorImpl.EndAtomicUndoOperation += value; }
			remove { textEditorImpl.EndAtomicUndoOperation -= value; }
		}

		public event EventHandler<TextChangeEventArgs> TextChanging {
			add { ReadWriteTextDocument.TextChanging += value; }
			remove { ReadWriteTextDocument.TextChanging -= value; }
		}

		public event EventHandler<TextChangeEventArgs> TextChanged {
			add { ReadWriteTextDocument.TextChanged += value; }
			remove { ReadWriteTextDocument.TextChanged -= value; }
		}

		public event EventHandler BeginMouseHover {
			add { textEditorImpl.BeginMouseHover += value; }
			remove { textEditorImpl.BeginMouseHover -= value; }
		}

		public event EventHandler VAdjustmentChanged {
			add { textEditorImpl.VAdjustmentChanged += value; }
			remove { textEditorImpl.VAdjustmentChanged -= value; }
		}

		public event EventHandler HAdjustmentChanged {
			add { textEditorImpl.HAdjustmentChanged += value; }
			remove { textEditorImpl.HAdjustmentChanged -= value; }
		}
		public char this[int offset] {
			get {
				return ReadOnlyTextDocument [offset];
			}
			set {
				ReadWriteTextDocument [offset] = value;
			}
		}

//		public event EventHandler<LineEventArgs> LineChanged {
//			add { textEditorImpl.LineChanged += value; }
//			remove { textEditorImpl.LineChanged -= value; }
//		}
//
//		public event EventHandler<LineEventArgs> LineInserted {
//			add { textEditorImpl.LineInserted += value; }
//			remove { textEditorImpl.LineInserted -= value; }
//		}
//
//		public event EventHandler<LineEventArgs> LineRemoved {
//			add { textEditorImpl.LineRemoved += value; }
//			remove { textEditorImpl.LineRemoved -= value; }
//		}

		public ITextEditorOptions Options {
			get {
				return textEditorImpl.Options;
			}
			set {
				textEditorImpl.Options = value;
				OnOptionsChanged (EventArgs.Empty);
			}
		}

		public event EventHandler OptionsChanged;

		void OnOptionsChanged (EventArgs e)
		{
			var handler = OptionsChanged;
			if (handler != null)
				handler (this, e);
		}

		public EditMode EditMode {
			get {
				return textEditorImpl.EditMode;
			}
		}

		public DocumentLocation CaretLocation {
			get {
				return textEditorImpl.CaretLocation;
			}
			set {
				textEditorImpl.CaretLocation = value;
			}
		}

		public SemanticHighlighting SemanticHighlighting {
			get {
				return textEditorImpl.SemanticHighlighting;
			}
			set {
				textEditorImpl.SemanticHighlighting = value;
			}
		}

		public int CaretLine {
			get {
				return CaretLocation.Line;
			}
			set {
				CaretLocation = new DocumentLocation (value, CaretColumn);
			}
		}

		public int CaretColumn {
			get {
				return CaretLocation.Column;
			}
			set {
				CaretLocation = new DocumentLocation (CaretLine, value);
			}
		}

		public int CaretOffset {
			get {
				return textEditorImpl.CaretOffset;
			}
			set {
				textEditorImpl.CaretOffset = value;
			}
		}

		public bool IsReadOnly {
			get {
				return ReadOnlyTextDocument.IsReadOnly;
			}
			set {
				ReadWriteTextDocument.IsReadOnly = value;
			}
		}

		public bool IsSomethingSelected {
			get {
				return textEditorImpl.IsSomethingSelected;
			}
		}

		public SelectionMode SelectionMode {
			get {
				return textEditorImpl.SelectionMode;
			}
		}

		public ISegment SelectionRange {
			get {
				return textEditorImpl.SelectionRange;
			}
			set {
				textEditorImpl.SelectionRange = value;
			}
		}

		public DocumentRegion SelectionRegion {
			get {
				return textEditorImpl.SelectionRegion;
			}
			set {
				textEditorImpl.SelectionRegion = value;
			}
		}
		
		public int SelectionAnchorOffset {
			get {
				return textEditorImpl.SelectionAnchorOffset;
			}
			set {
				textEditorImpl.SelectionAnchorOffset = value;
			}
		}

		public int SelectionLeadOffset {
			get {
				return textEditorImpl.SelectionLeadOffset;
			}
			set {
				textEditorImpl.SelectionLeadOffset = value;
			}
		}

		public string SelectedText {
			get {
				return IsSomethingSelected ? ReadOnlyTextDocument.GetTextAt (SelectionRange) : null;
			}
			set {
				var selection = SelectionRange;
				ReplaceText (selection, value);
				SelectionRange = new TextSegment (selection.Offset, value.Length);
			}
		}

		public bool IsInAtomicUndo {
			get {
				return ReadWriteTextDocument.IsInAtomicUndo;
			}
		}

		public double LineHeight {
			get {
				return textEditorImpl.LineHeight;
			}
		}

		/// <summary>
		/// Gets or sets the type of the MIME.
		/// </summary>
		/// <value>The type of the MIME.</value>
		public string MimeType {
			get {
				return ReadOnlyTextDocument.MimeType;
			}
			set {
				ReadWriteTextDocument.MimeType = value;
			}
		}

		public event EventHandler MimeTypeChanged {
			add { ReadWriteTextDocument.MimeTypeChanged += value; }
			remove { ReadWriteTextDocument.MimeTypeChanged -= value; }
		}

		public string Text {
			get {
				return ReadOnlyTextDocument.Text;
			}
			set {
				ReadWriteTextDocument.Text = value;
			}
		}

		/// <summary>
		/// Gets the eol marker. On a text editor always use that and not GetEolMarker.
		/// The EOL marker of the document may get overwritten my the one from the options.
		/// </summary>
		public string EolMarker {
			get {
				if (Options.OverrideDocumentEolMarker)
					return Options.DefaultEolMarker;
				return ReadOnlyTextDocument.GetEolMarker ();
			}
		}

		public bool UseBOM {
			get {
				return ReadOnlyTextDocument.UseBOM;
			}
			set {
				ReadWriteTextDocument.UseBOM = value;
			}
		}

		public Encoding Encoding {
			get {
				return ReadOnlyTextDocument.Encoding;
			}
			set {
				ReadWriteTextDocument.Encoding = value;
			}
		}

		public int LineCount {
			get {
				return ReadOnlyTextDocument.LineCount;
			}
		}

		/// <summary>
		/// Gets the name of the file the document is stored in.
		/// Could also be a non-existent dummy file name or null if no name has been set.
		/// </summary>
		public FilePath FileName {
			get {
				return ReadOnlyTextDocument.FileName;
			}
			set {
				ReadWriteTextDocument.FileName = value;
			}
		}

		public event EventHandler FileNameChanged {
			add {
				ReadWriteTextDocument.FileNameChanged += value;
			}
			remove {
				ReadWriteTextDocument.FileNameChanged -= value;
			}
		}

		public int Length {
			get {
				return ReadOnlyTextDocument.Length;
			}
		}

		public double ZoomLevel {
			get {
				return textEditorImpl.ZoomLevel;
			}
			set {
				textEditorImpl.ZoomLevel = value;
			}
		}

		public event EventHandler ZoomLevelChanged {
			add {
				textEditorImpl.ZoomLevelChanged += value;
			}
			remove {
				textEditorImpl.ZoomLevelChanged -= value;
			}
		}

		public IDisposable OpenUndoGroup ()
		{
			return ReadWriteTextDocument.OpenUndoGroup ();
		}

		public void SetSelection (int anchorOffset, int leadOffset)
		{
			textEditorImpl.SetSelection (anchorOffset, leadOffset);
		}

		public void SetSelection (DocumentLocation anchor, DocumentLocation lead)
		{
			SetSelection (LocationToOffset (anchor), LocationToOffset (lead));
		}

		public void SetCaretLocation (DocumentLocation location, bool usePulseAnimation = false)
		{
			CaretLocation = location;
			ScrollTo (CaretLocation);
			if (usePulseAnimation)
				StartCaretPulseAnimation ();
		}

		public void SetCaretLocation (int line, int col, bool usePulseAnimation = false)
		{
			CaretLocation = new DocumentLocation (line, col);
			CenterTo (CaretLocation);
			if (usePulseAnimation)
				StartCaretPulseAnimation ();
		}

		public void ClearSelection ()
		{
			textEditorImpl.ClearSelection ();
		}

		public void CenterToCaret ()
		{
			textEditorImpl.CenterToCaret ();
		}

		public void StartCaretPulseAnimation ()
		{
			textEditorImpl.StartCaretPulseAnimation ();
		}

		public int EnsureCaretIsNotVirtual ()
		{
			return textEditorImpl.EnsureCaretIsNotVirtual ();
		}

		public void FixVirtualIndentation ()
		{
			textEditorImpl.FixVirtualIndentation ();
		}

		public void RunWhenLoaded (Action action)
		{
			if (action == null)
				throw new ArgumentNullException ("action");
			textEditorImpl.RunWhenLoaded (action);
		}

		public string FormatString (DocumentLocation insertPosition, string code)
		{
			return textEditorImpl.FormatString (LocationToOffset (insertPosition), code);
		}

		public string FormatString (int offset, string code)
		{
			return textEditorImpl.FormatString (offset, code);
		}

		public void StartInsertionMode (InsertionModeOptions insertionModeOptions)
		{
			if (insertionModeOptions == null)
				throw new ArgumentNullException ("insertionModeOptions");
			textEditorImpl.StartInsertionMode (insertionModeOptions);
		}

		public void StartTextLinkMode (TextLinkModeOptions textLinkModeOptions)
		{
			if (textLinkModeOptions == null)
				throw new ArgumentNullException ("textLinkModeOptions");
			textEditorImpl.StartTextLinkMode (textLinkModeOptions);
		}

		public void InsertAtCaret (string text)
		{
			InsertText (CaretOffset, text);
		}

		public DocumentLocation PointToLocation (double xp, double yp, bool endAtEol = false)
		{
			return textEditorImpl.PointToLocation (xp, yp, endAtEol);
		}

		public Xwt.Point LocationToPoint (DocumentLocation location)
		{
			return textEditorImpl.LocationToPoint (location.Line, location.Column);
		} 

		public Xwt.Point LocationToPoint (int line, int column)
		{
			return textEditorImpl.LocationToPoint (line, column);
		}

		public string GetLineText (int line, bool includeDelimiter = false)
		{
			var segment = GetLine (line);
			return GetTextAt (includeDelimiter ? segment.SegmentIncludingDelimiter : segment);
		}

		public int LocationToOffset (int line, int column)
		{
			return ReadOnlyTextDocument.LocationToOffset (new DocumentLocation (line, column));
		}

		public int LocationToOffset (DocumentLocation location)
		{
			return ReadOnlyTextDocument.LocationToOffset (location);
		}

		public DocumentLocation OffsetToLocation (int offset)
		{
			return ReadOnlyTextDocument.OffsetToLocation (offset);
		}

		public void InsertText (int offset, string text)
		{
			ReadWriteTextDocument.InsertText (offset, text);
		}

		public void InsertText (int offset, ITextSource text)
		{
			ReadWriteTextDocument.InsertText (offset, text);
		}

		public void RemoveText (int offset, int count)
		{
			RemoveText (new TextSegment (offset, count)); 
		}

		public void RemoveText (ISegment segment)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			ReadWriteTextDocument.RemoveText (segment);
		}

		public void ReplaceText (int offset, int count, string value)
		{
			ReadWriteTextDocument.ReplaceText (offset, count, value);
		}

		public void ReplaceText (int offset, int count, ITextSource value)
		{
			ReadWriteTextDocument.ReplaceText (offset, count, value);
		}

		public void ReplaceText (ISegment segment, string value)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			ReadWriteTextDocument.ReplaceText (segment.Offset, segment.Length, value);
		}

		public void ReplaceText (ISegment segment, ITextSource value)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			ReadWriteTextDocument.ReplaceText (segment.Offset, segment.Length, value);
		}

		public IDocumentLine GetLine (int lineNumber)
		{
			return ReadOnlyTextDocument.GetLine (lineNumber);
		}

		public IDocumentLine GetLineByOffset (int offset)
		{
			return ReadOnlyTextDocument.GetLineByOffset (offset);
		}

		public int OffsetToLineNumber (int offset)
		{
			return ReadOnlyTextDocument.OffsetToLineNumber (offset);
		}

		public void AddMarker (IDocumentLine line, ITextLineMarker lineMarker)
		{
			if (line == null)
				throw new ArgumentNullException ("line");
			if (lineMarker == null)
				throw new ArgumentNullException ("lineMarker");
			textEditorImpl.AddMarker (line, lineMarker);
		}

		public void AddMarker (int lineNumber, ITextLineMarker lineMarker)
		{
			if (lineMarker == null)
				throw new ArgumentNullException ("lineMarker");
			AddMarker (GetLine (lineNumber), lineMarker);
		}

		public void RemoveMarker (ITextLineMarker lineMarker)
		{
			if (lineMarker == null)
				throw new ArgumentNullException ("lineMarker");
			textEditorImpl.RemoveMarker (lineMarker);
		}

		public IEnumerable<ITextLineMarker> GetLineMarkers (IDocumentLine line)
		{
			if (line == null)
				throw new ArgumentNullException ("line");
			return textEditorImpl.GetLineMarkers (line);
		}

		public IEnumerable<ITextSegmentMarker> GetTextSegmentMarkersAt (ISegment segment)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			return textEditorImpl.GetTextSegmentMarkersAt (segment);
		}

		public IEnumerable<ITextSegmentMarker> GetTextSegmentMarkersAt (int offset)
		{
			return textEditorImpl.GetTextSegmentMarkersAt (offset);
		}

		public void AddMarker (ITextSegmentMarker marker)
		{
			if (marker == null)
				throw new ArgumentNullException ("marker");
			textEditorImpl.AddMarker (marker);
		}

		public bool RemoveMarker (ITextSegmentMarker marker)
		{
			if (marker == null)
				throw new ArgumentNullException ("marker");
			return textEditorImpl.RemoveMarker (marker);
		}

		public void SetFoldings (IEnumerable<IFoldSegment> foldings)
		{
			if (foldings == null)
				throw new ArgumentNullException ("foldings");
			textEditorImpl.SetFoldings (foldings);
		}


		public IEnumerable<IFoldSegment> GetFoldingsContaining (int offset)
		{
			return textEditorImpl.GetFoldingsContaining (offset);
		}

		public IEnumerable<IFoldSegment> GetFoldingsIn (ISegment segment)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			return textEditorImpl.GetFoldingsIn (segment.Offset, segment.Length);
		}

		/// <summary>
		/// Gets a character at the specified position in the document.
		/// </summary>
		/// <paramref name="offset">The index of the character to get.</paramref>
		/// <exception cref="ArgumentOutOfRangeException">Offset is outside the valid range (0 to TextLength-1).</exception>
		/// <returns>The character at the specified position.</returns>
		/// <remarks>This is the same as Text[offset], but is more efficient because
		///  it doesn't require creating a String object.</remarks>
		public char GetCharAt (int offset)
		{
			return ReadOnlyTextDocument.GetCharAt (offset); 
		}

		public string GetTextAt (int offset, int length)
		{
			return ReadOnlyTextDocument.GetTextAt (offset, length);
		}

		public string GetTextAt (ISegment segment)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			return ReadOnlyTextDocument.GetTextAt (segment);
		}

		public IReadonlyTextDocument CreateDocumentSnapshot ()
		{
			return ReadWriteTextDocument.CreateDocumentSnapshot ();
		}

		public string GetVirtualIndentationString (int lineNumber)
		{
			if (lineNumber < 1 || lineNumber > LineCount)
				throw new ArgumentOutOfRangeException ("lineNumber");
			return textEditorImpl.GetVirtualIndentationString (lineNumber);
		}

		public string GetVirtualIndentationString (IDocumentLine line)
		{
			if (line == null)
				throw new ArgumentNullException ("line");
			return textEditorImpl.GetVirtualIndentationString (line.LineNumber);
		}

		public int GetVirtualIndentationColumn (int lineNumber)
		{
			if (lineNumber < 1 || lineNumber > LineCount)
				throw new ArgumentOutOfRangeException ("lineNumber");
			return 1 + textEditorImpl.GetVirtualIndentationString (lineNumber).Length;
		}

		public int GetVirtualIndentationColumn (IDocumentLine line)
		{
			if (line == null)
				throw new ArgumentNullException ("line");
			return 1 + textEditorImpl.GetVirtualIndentationString (line.LineNumber).Length;
		}

		public TextReader CreateReader ()
		{
			return ReadOnlyTextDocument.CreateReader ();
		}

		public TextReader CreateReader (int offset, int length)
		{
			return ReadOnlyTextDocument.CreateReader (offset, length);
		}

		public ITextSource CreateSnapshot ()
		{
			return ReadOnlyTextDocument.CreateSnapshot ();
		}

		public ITextSource CreateSnapshot (int offset, int length)
		{
			return ReadOnlyTextDocument.CreateSnapshot (offset, length);
		}

		public ITextSource CreateSnapshot (ISegment segment)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			return ReadOnlyTextDocument.CreateSnapshot (segment.Offset, segment.Length);
		}

		public void WriteTextTo (TextWriter writer)
		{
			if (writer == null)
				throw new ArgumentNullException ("writer");
			ReadOnlyTextDocument.WriteTextTo (writer);
		}

		public void WriteTextTo (TextWriter writer, int offset, int length)
		{
			if (writer == null)
				throw new ArgumentNullException ("writer");
			ReadOnlyTextDocument.WriteTextTo (writer, offset, length);
		}

		public void ScrollTo (int offset)
		{
			textEditorImpl.ScrollTo (offset);
		}

		public void ScrollTo (DocumentLocation loc)
		{
			ScrollTo (LocationToOffset (loc));
		}

		public void CenterTo (int offset)
		{
			textEditorImpl.CenterTo (offset);
		}

		public void CenterTo (DocumentLocation loc)
		{
			CenterTo (LocationToOffset (loc));
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		public void SetIndentationTracker (IndentationTracker indentationTracker)
		{
			textEditorImpl.SetIndentationTracker (indentationTracker);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		public void SetSelectionSurroundingProvider (SelectionSurroundingProvider surroundingProvider)
		{
			textEditorImpl.SetSelectionSurroundingProvider (surroundingProvider);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		public void SetTextPasteHandler (TextPasteHandler textPasteHandler)
		{
			textEditorImpl.SetTextPasteHandler (textPasteHandler);
		}

		public IList<SkipChar> SkipChars {
			get {
				return textEditorImpl.SkipChars;
			}
		}

		/// <summary>
		/// Skip chars are 
		/// </summary>
		public void AddSkipChar (int offset, char ch)
		{
			textEditorImpl.AddSkipChar (offset, ch);
		}

		protected override void Dispose (bool disposing)
		{
			if (disposing) {
				DetachExtensionChain ();
				textEditorImpl.Dispose ();
			}
			base.Dispose (disposing);
		}

		protected override object CreateNativeWidget ()
		{
			return textEditorImpl.CreateNativeControl ();
		}

		#region Internal API
		ExtensionContext extensionContext;

		internal ExtensionContext ExtensionContext {
			get {
				return extensionContext;
			}
			set {
				if (extensionContext != null) {
					extensionContext.RemoveExtensionNodeHandler ("MonoDevelop/SourceEditor2/TooltipProviders", OnTooltipProviderChanged);
//					textEditorImpl.ClearTooltipProviders ();
				}
				extensionContext = value;
				if (extensionContext != null)
					extensionContext.AddExtensionNodeHandler ("MonoDevelop/SourceEditor2/TooltipProviders", OnTooltipProviderChanged);
			}
		}

		internal IEditorActionHost EditorActionHost {
			get {
				return textEditorImpl.Actions;
			}
		}

		internal ITextMarkerFactory TextMarkerFactory {
			get {
				return textEditorImpl.TextMarkerFactory;
			}
		}

		internal TextEditor (ITextEditorImpl textEditorImpl)
		{
			if (textEditorImpl == null)
				throw new ArgumentNullException ("textEditorImpl");
			this.textEditorImpl = textEditorImpl;
			commandRouter = new InternalCommandRouter (this);
			fileTypeCondition.SetFileName (FileName);
			ExtensionContext = AddinManager.CreateExtensionContext ();
			ExtensionContext.RegisterCondition ("FileType", fileTypeCondition);

			FileNameChanged += delegate {
				fileTypeCondition.SetFileName (FileName);
			};

			MimeTypeChanged += delegate {
				textEditorImpl.ClearTooltipProviders ();
				foreach (var extensionNode in allProviders) {
					if (extensionNode.IsValidFor (MimeType))
						textEditorImpl.AddTooltipProvider ((TooltipProvider) extensionNode.CreateInstance ());
				}
			};
		}

		TextEditorViewContent viewContent;
		internal IViewContent GetViewContent ()
		{
			if (viewContent == null) {
				viewContent = new TextEditorViewContent (this, textEditorImpl);
			}

			return viewContent;
		}

		internal IFoldSegment CreateFoldSegment (int offset, int length, bool isFolded = false)
		{
			return textEditorImpl.CreateFoldSegment (offset, length, isFolded);
		}
		#endregion

		#region Editor extensions
		InternalCommandRouter commandRouter;
		class InternalCommandRouter : MonoDevelop.Components.Commands.IMultiCastCommandRouter
		{
			readonly TextEditor editor;

			public InternalCommandRouter (TextEditor editor)
			{
				this.editor = editor;
			}
			
			#region IMultiCastCommandRouter implementation

			System.Collections.IEnumerable MonoDevelop.Components.Commands.IMultiCastCommandRouter.GetCommandTargets ()
			{
				yield return editor.textEditorImpl;
				yield return editor.textEditorImpl.EditorExtension;
			}
			#endregion
		}

		internal object CommandRouter {
			get {
				return commandRouter;
			}
		}

		DocumentContext documentContext;
		internal DocumentContext DocumentContext {
			get {
				return documentContext;
			}
			set {
				documentContext = value;
				OnDocumentContextChanged (EventArgs.Empty);
			}
		}

		public event EventHandler DocumentContextChanged;

		void OnDocumentContextChanged (EventArgs e)
		{
			if (DocumentContext != null) {
				textEditorImpl.SetQuickTaskProviders (DocumentContext.GetContents<IQuickTaskProvider> ());
				textEditorImpl.SetUsageTaskProviders (DocumentContext.GetContents<UsageProviderEditorExtension> ());
			} else {
				textEditorImpl.SetQuickTaskProviders (Enumerable.Empty<IQuickTaskProvider> ());
				textEditorImpl.SetUsageTaskProviders (Enumerable.Empty<UsageProviderEditorExtension> ());
			}
			var handler = DocumentContextChanged;
			if (handler != null)
				handler (this, e);
		}

		internal void InitializeExtensionChain (DocumentContext documentContext)
		{
			if (documentContext == null)
				throw new ArgumentNullException ("documentContext");
			DetachExtensionChain ();
			var extensions = ExtensionContext.GetExtensionNodes ("/MonoDevelop/Ide/TextEditorExtensions", typeof(TextEditorExtensionNode));
			TextEditorExtension last = null;
			var mimetypeChain = DesktopService.GetMimeTypeInheritanceChainForFile (FileName).ToArray ();
			foreach (TextEditorExtensionNode extNode in extensions) {
				if (!extNode.Supports (FileName, mimetypeChain))
					continue;
				TextEditorExtension ext;
				try {
					var instance = extNode.CreateInstance ();
					ext = instance as TextEditorExtension;
					if (ext == null)
						continue;
				} catch (Exception e) {
					LoggingService.LogError ("Error while creating text editor extension :" + extNode.Id + "(" + extNode.Type +")", e); 
					continue;
				}
				if (ext.IsValidInContext (documentContext)) {
					if (last != null) {
						last.Next = ext;
						last = ext;
					} else {
						textEditorImpl.EditorExtension = last = ext;
					}
					ext.Initialize (this, documentContext);
				}
			}
			this.DocumentContext = documentContext;
		}

		void DetachExtensionChain ()
		{
			var editorExtension = textEditorImpl.EditorExtension;
			while (editorExtension != null) {
				try {
					editorExtension.Dispose ();
				} catch (Exception ex) {
					LoggingService.LogError ("Exception while disposing extension:" + editorExtension, ex);
				}
				editorExtension = editorExtension.Next;
			}
			textEditorImpl.EditorExtension = null;
		}

		public T GetContent<T> () where T : class
		{
			T result = textEditorImpl as T;
			if (result != null)
				return result;
			var ext = textEditorImpl.EditorExtension;
			while (ext != null) {
				result = ext as T;
				if (result != null)
					return result;
				ext = ext.Next;
			}
			return null;
		}

		public IEnumerable<T> GetContents<T> () where T : class
		{
			T result = textEditorImpl as T;
			if (result != null)
				yield return result;
			var ext = textEditorImpl.EditorExtension;
			while (ext != null) {
				result = ext as T;
				if (result != null)
					yield return result;
				ext = ext.Next;
			}
		}
		#endregion

		public string GetPangoMarkup (int offset, int length)
		{
			return textEditorImpl.GetPangoMarkup (offset, length);
		}

		public string GetPangoMarkup (ISegment segment)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			return textEditorImpl.GetPangoMarkup (segment.Offset, segment.Length);
		}

		public static implicit operator Microsoft.CodeAnalysis.Text.SourceText (TextEditor editor)
		{
			return new MonoDevelopSourceText (editor);
		}


		#region Annotations
		// Annotations: points either null (no annotations), to the single annotation,
		// or to an AnnotationList.
		// Once it is pointed at an AnnotationList, it will never change (this allows thread-safety support by locking the list)

		object annotations;
		sealed class AnnotationList : List<object>, ICloneable
		{
			// There are two uses for this custom list type:
			// 1) it's private, and thus (unlike List<object>) cannot be confused with real annotations
			// 2) It allows us to simplify the cloning logic by making the list behave the same as a clonable annotation.
			public AnnotationList (int initialCapacity) : base(initialCapacity)
			{
			}

			public object Clone ()
			{
				lock (this) {
					AnnotationList copy = new AnnotationList (this.Count);
					for (int i = 0; i < this.Count; i++) {
						object obj = this [i];
						ICloneable c = obj as ICloneable;
						copy.Add (c != null ? c.Clone () : obj);
					}
					return copy;
				}
			}
		}

		public void AddAnnotation (object annotation)
		{
			if (annotation == null)
				throw new ArgumentNullException ("annotation");
			retry: // Retry until successful
			object oldAnnotation = Interlocked.CompareExchange (ref this.annotations, annotation, null);
			if (oldAnnotation == null) {
				return; // we successfully added a single annotation
			}
			AnnotationList list = oldAnnotation as AnnotationList;
			if (list == null) {
				// we need to transform the old annotation into a list
				list = new AnnotationList (4);
				list.Add (oldAnnotation);
				list.Add (annotation);
				if (Interlocked.CompareExchange (ref this.annotations, list, oldAnnotation) != oldAnnotation) {
					// the transformation failed (some other thread wrote to this.annotations first)
					goto retry;
				}
			} else {
				// once there's a list, use simple locking
				lock (list) {
					list.Add (annotation);
				}
			}
		}

		public void RemoveAnnotations<T> () where T : class
		{
			retry: // Retry until successful
			object oldAnnotations = this.annotations;
			var list = oldAnnotations as AnnotationList;
			if (list != null) {
				lock (list)
					list.RemoveAll (obj => obj is T);
			} else if (oldAnnotations is T) {
				if (Interlocked.CompareExchange (ref this.annotations, null, oldAnnotations) != oldAnnotations) {
					// Operation failed (some other thread wrote to this.annotations first)
					goto retry;
				}
			}
		}

		public T Annotation<T> () where T: class
		{
			object annotations = this.annotations;
			var list = annotations as AnnotationList;
			if (list != null) {
				lock (list) {
					foreach (object obj in list) {
						T t = obj as T;
						if (t != null)
							return t;
					}
					return null;
				}
			}
			return annotations as T;
		}

		/// <summary>
		/// Gets all annotations stored on this AstNode.
		/// </summary>
		public IEnumerable<object> Annotations {
			get {
				object annotations = this.annotations;
				AnnotationList list = annotations as AnnotationList;
				if (list != null) {
					lock (list) {
						return list.ToArray ();
					}
				}
				if (annotations != null)
					return new [] { annotations };
				return Enumerable.Empty<object> ();
			}
		}
		#endregion

		List<ProjectedTooltipProvider> projectedProviders = new List<ProjectedTooltipProvider> ();
		IReadOnlyList<MonoDevelop.Ide.Editor.Projection.Projection> projections = null;

		public void SetOrUpdateProjections (DocumentContext ctx, IReadOnlyList<MonoDevelop.Ide.Editor.Projection.Projection> projections, DisabledProjectionFeatures disabledFeatures = DisabledProjectionFeatures.None)
		{
			if (ctx == null)
				throw new ArgumentNullException ("ctx");
			if (this.projections != null) {
				foreach (var projection in this.projections) {
					projection.Dettach ();
				}
			}
			this.projections = projections;
			if (projections != null) {
				foreach (var projection in projections) {
					projection.Attach (this);
				}
			}

			if ((disabledFeatures & DisabledProjectionFeatures.SemanticHighlighting) != DisabledProjectionFeatures.SemanticHighlighting) {
					if (SemanticHighlighting is ProjectedSemanticHighlighting) {
					((ProjectedSemanticHighlighting)SemanticHighlighting).UpdateProjection (projections);
				} else {
					SemanticHighlighting = new ProjectedSemanticHighlighting (this, ctx, projections);
				}
			}

			if ((disabledFeatures & DisabledProjectionFeatures.Tooltips) != DisabledProjectionFeatures.Tooltips) {
					projectedProviders.ForEach (textEditorImpl.RemoveTooltipProvider);
				projectedProviders = new List<ProjectedTooltipProvider> ();
				foreach (var projection in projections) {
					foreach (var tp in projection.ProjectedEditor.textEditorImpl.TooltipProvider) {
						var newProvider = new ProjectedTooltipProvider (this, ctx, projection, tp);
						projectedProviders.Add (newProvider);
						textEditorImpl.AddTooltipProvider (newProvider);
					}
				}
			}
			InitializeProjectionExtensions (ctx, disabledFeatures);
		}

		bool projectionsAdded = false;
		void InitializeProjectionExtensions (DocumentContext ctx, DisabledProjectionFeatures disabledFeatures)
		{
			if (projectionsAdded) {
				TextEditorExtension ext = textEditorImpl.EditorExtension;
				while (ext != null && ext.Next != null) {
					var pext = ext as IProjectionExtension;
					if (pext != null) {
						pext.Projections = projections;
					}
					ext = ext.Next;
				}
				return;
			}

			if (projections.Count == 0)
				return;

			TextEditorExtension lastExtension = textEditorImpl.EditorExtension;
			while (lastExtension != null && lastExtension.Next != null) {
				var completionTextEditorExtension = lastExtension.Next as CompletionTextEditorExtension;
				if (completionTextEditorExtension != null) {
					var projectedFilterExtension = new ProjectedFilterCompletionTextEditorExtension (completionTextEditorExtension, projections) { Next = completionTextEditorExtension.Next };
					lastExtension.Next = projectedFilterExtension;
					projectedFilterExtension.Initialize (this, DocumentContext);
				}
				lastExtension = lastExtension.Next;
			}

			// no extensions -> no projections needed
			if (textEditorImpl.EditorExtension == null)
				return;

			if ((disabledFeatures & DisabledProjectionFeatures.Completion) != DisabledProjectionFeatures.Completion) {
				var projectedCompletionExtension = new ProjectedCompletionExtension (ctx, projections);
				projectedCompletionExtension.Next = textEditorImpl.EditorExtension;

				textEditorImpl.EditorExtension = projectedCompletionExtension;
				projectedCompletionExtension.Initialize (this, DocumentContext);
			}
			projectionsAdded = true;
		}

		public void AddOverlay (Control messageOverlayContent, Func<int> sizeFunc)
		{
			textEditorImpl.AddOverlay (messageOverlayContent, sizeFunc);
		}

		public void RemoveOverlay (Control messageOverlayContent)
		{
			textEditorImpl.RemoveOverlay (messageOverlayContent);
		}
	}
}