~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/SourceEditorView.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
using MonoDevelop.Ide.TypeSystem;
51
51
using ICSharpCode.NRefactory.Semantics;
52
52
using MonoDevelop.SourceEditor.QuickTasks;
 
53
using MonoDevelop.Ide.TextEditing;
53
54
using System.Text;
 
55
using Mono.Addins;
54
56
 
55
57
namespace MonoDevelop.SourceEditor
56
58
{       
64
66
                DateTime lastSaveTimeUtc;
65
67
                string loadedMimeType;
66
68
                internal object MemoryProbe = Counters.SourceViewsInMemory.CreateMemoryProbe ();
67
 
                TextMarker currentDebugLineMarker;
68
 
                TextMarker debugStackLineMarker;
 
69
                TextLineMarker currentDebugLineMarker;
 
70
                TextLineMarker debugStackLineMarker;
69
71
                int lastDebugLine = -1;
70
72
                EventHandler currentFrameChanged;
71
73
                EventHandler<BreakpointEventArgs> breakpointAdded;
152
154
                }
153
155
                
154
156
                bool wasEdited = false;
 
157
                uint removeMarkerTimeout;
 
158
                Queue<MessageBubbleTextMarker> markersToRemove = new Queue<MessageBubbleTextMarker> ();
 
159
 
 
160
 
 
161
                void RemoveMarkerQueue ()
 
162
                {
 
163
                        if (removeMarkerTimeout != 0)
 
164
                                GLib.Source.Remove (removeMarkerTimeout);
 
165
                }
 
166
 
 
167
                void ResetRemoveMarker ()
 
168
                {
 
169
                        RemoveMarkerQueue ();
 
170
                        removeMarkerTimeout = GLib.Timeout.Add (2000, delegate {
 
171
                                while (markersToRemove.Count > 0) {
 
172
                                        var _m = markersToRemove.Dequeue ();
 
173
                                        currentErrorMarkers.Remove (_m);
 
174
                                        widget.TextEditor.Document.RemoveMarker (_m);
 
175
                                }
 
176
                                removeMarkerTimeout = 0;
 
177
                                return false;
 
178
                        });
 
179
                }
155
180
 
156
181
                public SourceEditorView ()
157
182
                {
160
185
                        breakpointAdded = (EventHandler<BreakpointEventArgs>)DispatchService.GuiDispatch (new EventHandler<BreakpointEventArgs> (OnBreakpointAdded));
161
186
                        breakpointRemoved = (EventHandler<BreakpointEventArgs>)DispatchService.GuiDispatch (new EventHandler<BreakpointEventArgs> (OnBreakpointRemoved));
162
187
                        breakpointStatusChanged = (EventHandler<BreakpointEventArgs>)DispatchService.GuiDispatch (new EventHandler<BreakpointEventArgs> (OnBreakpointStatusChanged));
163
 
                        
 
188
 
164
189
                        widget = new SourceEditorWidget (this);
165
190
                        widget.TextEditor.Document.SyntaxModeChanged += delegate(object sender, SyntaxModeChangeEventArgs e) {
166
191
                                var oldProvider = e.OldMode as IQuickTaskProvider;
180
205
                                }
181
206
                                int startIndex = args.Offset;
182
207
                                int endIndex = startIndex + Math.Max (args.RemovalLength, args.InsertionLength);
183
 
                                if (TextChanged != null)
184
 
                                        TextChanged (this, new TextChangedEventArgs (startIndex, endIndex));
 
208
                                foreach (var marker in currentErrorMarkers) {
 
209
                                        if (marker.LineSegment.Contains (args.Offset) || marker.LineSegment.Contains (args.Offset + args.InsertionLength) || args.Offset < marker.LineSegment.Offset && marker.LineSegment.Offset < args.Offset + args.InsertionLength) {
 
210
                                                markersToRemove.Enqueue (marker);
 
211
                                        }
 
212
                                }
 
213
                                ResetRemoveMarker ();
185
214
                        };
186
215
                        
187
216
                        widget.TextEditor.Document.LineChanged += delegate(object sender, LineEventArgs e) {
188
217
                                UpdateBreakpoints ();
 
218
                                UpdateWidgetPositions ();
189
219
                                if (messageBubbleCache != null && messageBubbleCache.RemoveLine (e.Line)) {
190
220
                                        MessageBubbleTextMarker marker = currentErrorMarkers.FirstOrDefault (m => m.LineSegment == e.Line);
191
221
                                        if (marker != null) {
192
 
                                                double oldHeight = marker.lastHeight;
 
222
 
193
223
                                                widget.TextEditor.TextViewMargin.RemoveCachedLine (e.Line);Ā 
194
224
                                                //Ā ensure that the line cache is renewed
195
 
                                                double newHeight = marker.GetLineHeight (widget.TextEditor);
 
225
                                                marker.GetLineHeight (widget.TextEditor);
196
226
                                        }
197
227
                                }
198
228
                        };
225
255
                        debugStackLineMarker = new DebugStackLineTextMarker (widget.TextEditor);
226
256
                        currentDebugLineMarker = new CurrentDebugLineTextMarker (widget.TextEditor);
227
257
                        
228
 
 
229
 
                        this.WorkbenchWindowChanged += delegate {
230
 
                                if (WorkbenchWindow != null) {
231
 
                                        widget.TextEditor.ExtensionContext = WorkbenchWindow.ExtensionContext;
232
 
                                        WorkbenchWindow.ActiveViewContentChanged += delegate {
233
 
                                                widget.UpdateLineCol ();
234
 
                                        };
235
 
                                }
236
 
                        };
 
258
                        this.WorkbenchWindowChanged += HandleWorkbenchWindowChanged;
237
259
                        this.ContentNameChanged += delegate {
238
260
                                this.Document.FileName = this.ContentName;
239
261
                                if (String.IsNullOrEmpty (ContentName) || !File.Exists (ContentName))
243
265
                        };
244
266
                        ClipbardRingUpdated += UpdateClipboardRing;
245
267
                        
 
268
                        TextEditorService.FileExtensionAdded += HandleFileExtensionAdded;
 
269
                        TextEditorService.FileExtensionRemoved += HandleFileExtensionRemoved;
 
270
 
246
271
                        DebuggingService.DebugSessionStarted += OnDebugSessionStarted;
247
272
                        DebuggingService.CurrentFrameChanged += currentFrameChanged;
248
273
                        DebuggingService.StoppedEvent += currentFrameChanged;
265
290
                        Document.AddAnnotation (this);
266
291
                        FileRegistry.Add (this);
267
292
                }
 
293
 
 
294
                void HandleFileExtensionRemoved (object sender, FileExtensionEventArgs args)
 
295
                {
 
296
                        if (ContentName == null || args.Extension.File.FullPath != Path.GetFullPath (ContentName))
 
297
                                return;
 
298
                        RemoveFileExtension (args.Extension);
 
299
                }
 
300
 
 
301
                void HandleFileExtensionAdded (object sender, FileExtensionEventArgs args)
 
302
                {
 
303
                        if (ContentName == null || args.Extension.File.FullPath != Path.GetFullPath (ContentName))
 
304
                                return;
 
305
                        AddFileExtension (args.Extension);
 
306
                }
 
307
 
 
308
                Dictionary<TopLevelWidgetExtension,Gtk.Widget> widgetExtensions = new Dictionary<TopLevelWidgetExtension, Widget> ();
 
309
                Dictionary<FileExtension,Tuple<TextLineMarker,DocumentLine>> markerExtensions = new Dictionary<FileExtension, Tuple<TextLineMarker,DocumentLine>> ();
 
310
 
 
311
                void LoadExtensions ()
 
312
                {
 
313
                        if (ContentName == null)
 
314
                                return;
 
315
 
 
316
                        foreach (var ext in TextEditorService.GetFileExtensions (ContentName))
 
317
                                AddFileExtension (ext);
 
318
                }
 
319
 
 
320
                void AddFileExtension (FileExtension extension)
 
321
                {
 
322
                        if (extension is TopLevelWidgetExtension) {
 
323
                                var widgetExtension = (TopLevelWidgetExtension)extension;
 
324
                                var w = widgetExtension.CreateWidget ();
 
325
                                int x, y;
 
326
                                if (!CalcWidgetPosition (widgetExtension, w, out x, out y)) {
 
327
                                        w.Destroy ();
 
328
                                        return;
 
329
                                }
 
330
 
 
331
                                widgetExtensions [widgetExtension] = w;
 
332
                                widget.TextEditor.TextArea.AddTopLevelWidget (w, x, y);
 
333
                                widgetExtension.ScrollToViewRequested += HandleScrollToViewRequested;
 
334
                        }
 
335
                        else if (extension is TextLineMarkerExtension) {
 
336
                                var lineExt = (TextLineMarkerExtension) extension;
 
337
 
 
338
                                DocumentLine line = widget.TextEditor.Document.GetLine (lineExt.Line);
 
339
                                if (line == null)
 
340
                                        return;
 
341
 
 
342
                                var marker = lineExt.CreateMarker ();
 
343
                                widget.TextEditor.Document.AddMarker (line, marker);
 
344
                                widget.TextEditor.QueueDraw ();
 
345
                                markerExtensions [extension] = new Tuple<TextLineMarker,DocumentLine> (marker, line);
 
346
                        }
 
347
                }
 
348
 
 
349
                void HandleScrollToViewRequested (object sender, EventArgs e)
 
350
                {
 
351
                        var widgetExtension = (TopLevelWidgetExtension)sender;
 
352
                        Gtk.Widget w;
 
353
                        if (widgetExtensions.TryGetValue (widgetExtension, out w)) {
 
354
                                int x, y;
 
355
                                widget.TextEditor.TextArea.GetTopLevelWidgetPosition (w, out x, out y);
 
356
                                var size = w.SizeRequest ();
 
357
                                Gtk.Application.Invoke (delegate {
 
358
                                        widget.TextEditor.ScrollTo (new Gdk.Rectangle (x, y, size.Width, size.Height));
 
359
                                });
 
360
                        }
 
361
                }
 
362
 
 
363
                void RemoveFileExtension (FileExtension extension)
 
364
                {
 
365
                        if (extension is TopLevelWidgetExtension) {
 
366
                                var widgetExtension = (TopLevelWidgetExtension)extension;
 
367
                                Gtk.Widget w;
 
368
                                if (!widgetExtensions.TryGetValue (widgetExtension, out w))
 
369
                                        return;
 
370
                                widgetExtensions.Remove (widgetExtension);
 
371
                                widget.TextEditor.TextArea.Remove (w);
 
372
                                w.Destroy ();
 
373
                                widgetExtension.ScrollToViewRequested -= HandleScrollToViewRequested;
 
374
                        }
 
375
                        else if (extension is TextLineMarkerExtension) {
 
376
                                Tuple<TextLineMarker,DocumentLine> data;
 
377
                                if (markerExtensions.TryGetValue (extension, out data))
 
378
                                        widget.TextEditor.Document.RemoveMarker (data.Item1);
 
379
                        }
 
380
                }
 
381
 
 
382
                void ClearExtensions ()
 
383
                {
 
384
                        foreach (var ex in widgetExtensions.Keys)
 
385
                                ex.ScrollToViewRequested -= HandleScrollToViewRequested;
 
386
                }
 
387
 
 
388
                void UpdateWidgetPositions ()
 
389
                {
 
390
                        foreach (var e in widgetExtensions) {
 
391
                                int x,y;
 
392
                                if (CalcWidgetPosition ((TopLevelWidgetExtension)e.Key, e.Value, out x, out y))
 
393
                                        widget.TextEditor.TextArea.MoveTopLevelWidget (e.Value, x, y);
 
394
                                else
 
395
                                        e.Value.Hide ();
 
396
                        }
 
397
                }
 
398
 
 
399
                bool CalcWidgetPosition (TopLevelWidgetExtension widgetExtension, Gtk.Widget w, out int x, out int y)
 
400
                {
 
401
                        DocumentLine line = widget.TextEditor.Document.GetLine (widgetExtension.Line);
 
402
                        if (line == null) {
 
403
                                x = y = 0;
 
404
                                return false;
 
405
                        }
 
406
 
 
407
                        int lw, lh;
 
408
                        widget.TextEditor.TextViewMargin.GetLayout (line).Layout.GetPixelSize (out lw, out lh);
 
409
                        lh = (int) TextEditor.TextViewMargin.GetLineHeight (widgetExtension.Line);
 
410
                        x = (int)widget.TextEditor.TextViewMargin.XOffset + lw + 4;
 
411
                        y = (int)widget.TextEditor.LineToY (widgetExtension.Line);
 
412
                        int lineStart = (int)widget.TextEditor.TextViewMargin.XOffset;
 
413
                        var size = w.SizeRequest ();
 
414
 
 
415
                        switch (widgetExtension.HorizontalAlignment) {
 
416
                        case HorizontalAlignment.LineLeft:
 
417
                                x = (int)widget.TextEditor.TextViewMargin.XOffset;
 
418
                                break;
 
419
                        case HorizontalAlignment.LineRight:
 
420
                                x = lineStart + lw + 4;
 
421
                                break;
 
422
                        case HorizontalAlignment.LineCenter:
 
423
                                x = lineStart + (lw - size.Width) / 2;
 
424
                                if (x < lineStart)
 
425
                                        x = lineStart;
 
426
                                break;
 
427
                        case HorizontalAlignment.Left:
 
428
                                x = 0;
 
429
                                break;
 
430
                        case HorizontalAlignment.Right:
 
431
                                break;
 
432
                        case HorizontalAlignment.Center:
 
433
                                break;
 
434
                        case HorizontalAlignment.ViewLeft:
 
435
                                break;
 
436
                        case HorizontalAlignment.ViewRight:
 
437
                                break;
 
438
                        case HorizontalAlignment.ViewCenter:
 
439
                                break;
 
440
                        }
 
441
 
 
442
                        switch (widgetExtension.VerticalAlignment) {
 
443
                        case VerticalAlignment.LineTop:
 
444
                                break; // the default
 
445
                        case VerticalAlignment.LineBottom:
 
446
                                y += lh - size.Height;
 
447
                                break;
 
448
                        case VerticalAlignment.LineCenter:
 
449
                                y = y + (lh - size.Height) / 2;
 
450
                                break;
 
451
                        case VerticalAlignment.AboveLine:
 
452
                                y -= size.Height;
 
453
                                break;
 
454
                        case VerticalAlignment.BelowLine:
 
455
                                y += lh;
 
456
                                break;
 
457
                        }
 
458
                        x += widgetExtension.OffsetX;
 
459
                        y += widgetExtension.OffsetY;
 
460
                        return true;
 
461
                }
 
462
 
 
463
                void HandleWorkbenchWindowChanged (object sender, EventArgs e)
 
464
                {
 
465
                        if (WorkbenchWindow != null) {
 
466
                                widget.TextEditor.ExtensionContext = WorkbenchWindow.ExtensionContext;
 
467
                                WorkbenchWindow.ActiveViewContentChanged += HandleActiveViewContentChanged;
 
468
                                this.WorkbenchWindowChanged -= HandleWorkbenchWindowChanged;
 
469
                        }
 
470
                }
 
471
 
 
472
                void HandleActiveViewContentChanged (object o, ActiveViewContentEventArgs e)
 
473
                {
 
474
                        widget.UpdateLineCol ();
 
475
                }
268
476
                
269
477
                MessageBubbleHighlightPopupWindow messageBubbleHighlightPopupWindow = null;
270
478
 
291
499
                        if (TextEditor != null && TextEditor.IsComposited) {
292
500
                                if (messageBubbleHighlightPopupWindow != null)
293
501
                                        messageBubbleHighlightPopupWindow.Destroy ();
294
 
                                messageBubbleHighlightPopupWindow = new MessageBubbleHighlightPopupWindow (this, marker);
 
502
                        /*      messageBubbleHighlightPopupWindow = new MessageBubbleHighlightPopupWindow (this, marker);
295
503
                                messageBubbleHighlightPopupWindow.Destroyed += delegate {
296
504
                                        messageBubbleHighlightPopupWindow = null;
297
505
                                };
298
 
                                messageBubbleHighlightPopupWindow.Popup ();
 
506
                                messageBubbleHighlightPopupWindow.Popup ();*/
299
507
                        }
300
508
                }
301
509
 
397
605
 
398
606
                        if (warnOverwrite) {
399
607
                                if (fileName == ContentName) {
400
 
                                        if (MessageService.AskQuestion (GettextCatalog.GetString ("This file {0} has been changed outside of MonoDevelop. Are you sure you want to overwrite the file?", fileName), AlertButton.Cancel, AlertButton.OverwriteFile) != AlertButton.OverwriteFile)
 
608
                                        string question = GettextCatalog.GetString (
 
609
                                                "This file {0} has been changed outside of {1}. Are you sure you want to overwrite the file?",
 
610
                                                fileName, BrandingService.ApplicationName
 
611
                                        );
 
612
                                        if (MessageService.AskQuestion (question, AlertButton.Cancel, AlertButton.OverwriteFile) != AlertButton.OverwriteFile)
401
613
                                                return;
402
614
                                }
403
615
                                warnOverwrite = false;
421
633
                                if (File.Exists (fileName)) {
422
634
                                        try {
423
635
                                                attributes = DesktopService.GetFileAttributes (fileName);
 
636
                                                var fileAttributes = File.GetAttributes (fileName);
 
637
                                                if (fileAttributes.HasFlag (FileAttributes.ReadOnly)) {
 
638
                                                        var result = MessageService.AskQuestion (
 
639
                                                                GettextCatalog.GetString ("Can't save file"),
 
640
                                                                GettextCatalog.GetString ("The file was marked as read only. Should the file be overwritten?"),
 
641
                                                                AlertButton.Yes,
 
642
                                                                AlertButton.No);
 
643
                                                        if (result == AlertButton.Yes) {
 
644
                                                                try {
 
645
                                                                        File.SetAttributes (fileName, fileAttributes & ~FileAttributes.ReadOnly);
 
646
                                                                } catch (Exception) {
 
647
                                                                        MessageService.ShowError (GettextCatalog.GetString ("Error"),
 
648
                                                                                                  GettextCatalog.GetString ("Operation failed."));
 
649
                                                                        return;
 
650
                                                                }
 
651
                                                        } else {
 
652
                                                                return;
 
653
                                                        }
 
654
                                                }
424
655
                                        } catch (Exception e) {
425
656
                                                LoggingService.LogWarning ("Can't get file attributes", e);
426
657
                                        }
483
714
                        Document.MimeType = mimeType;
484
715
                        string text = null;
485
716
                        if (content != null) {
486
 
                                text = Mono.TextEditor.Utils.TextFileUtility.GetText (content, out hadBom, out encoding);
 
717
                                text = Mono.TextEditor.Utils.TextFileUtility.GetText (content, out encoding, out hadBom);
487
718
                                Document.Text = text;
488
719
                        }
489
720
                        this.CreateDocumentParsedHandler ();
521
752
                        this.WorkbenchWindowChanged += delegate {
522
753
                                if (WorkbenchWindow == null)
523
754
                                        return;
524
 
                                WorkbenchWindow.DocumentChanged += delegate {
 
755
                                WorkbenchWindow.DocumentChanged +=  delegate {
525
756
                                        if (WorkbenchWindow.Document == null)
526
757
                                                return;
527
758
                                        foreach (var provider in WorkbenchWindow.Document.GetContents<IQuickTaskProvider> ()) {
530
761
                                        foreach (var provider in WorkbenchWindow.Document.GetContents<IUsageProvider> ()) {
531
762
                                                widget.AddUsageTaskProvider (provider);
532
763
                                        }
533
 
                                        WorkbenchWindow.Document.DocumentParsed += delegate (object sender, EventArgs e) {
534
 
                                                widget.UpdateParsedDocument (WorkbenchWindow.Document.ParsedDocument);
535
 
                                        };
 
764
                                        ownerDocument = WorkbenchWindow.Document;
 
765
                                        ownerDocument.DocumentParsed += HandleDocumentParsed;
536
766
                                };
537
767
                        };
 
768
                }
 
769
 
 
770
                MonoDevelop.Ide.Gui.Document ownerDocument;
 
771
 
 
772
                void HandleDocumentParsed (object sender, EventArgs e)
 
773
                {
 
774
                        widget.UpdateParsedDocument (ownerDocument.ParsedDocument);
538
775
                }               
 
776
 
539
777
                public void Load (string fileName, Encoding loadEncoding)
540
778
                {
541
779
                        // Handle the "reload" case.
580
818
                        UpdateExecutionLocation ();
581
819
                        UpdateBreakpoints ();
582
820
                        UpdatePinnedWatches ();
 
821
                        LoadExtensions ();
583
822
                        this.IsDirty = !didLoadCleanly;
584
823
                        UpdateTasks (null, null);
585
824
                        widget.TextEditor.VAdjustment.Changed += HandleTextEditorVAdjustmentChanged;
669
908
                        UpdateExecutionLocation ();
670
909
                        UpdateBreakpoints ();
671
910
                        UpdatePinnedWatches ();
 
911
                        LoadExtensions ();
672
912
                        IsDirty = false;
673
913
                        Document.InformLoadComplete ();
674
914
                }
695
935
                public Encoding SourceEncoding {
696
936
                        get { return encoding; }
697
937
                }
698
 
                
 
938
 
699
939
                public override void Dispose ()
700
940
                {
 
941
                        ClearExtensions ();
701
942
                        FileRegistry.Remove (this);
702
943
                        RemoveAutoSaveTimer ();
703
944
                        
708
949
                        
709
950
                        if (messageBubbleHighlightPopupWindow != null)
710
951
                                messageBubbleHighlightPopupWindow.Destroy ();
711
 
                        
 
952
 
712
953
                        IdeApp.Preferences.DefaultHideMessageBubblesChanged -= HandleIdeAppPreferencesDefaultHideMessageBubblesChanged;
713
954
                        IdeApp.Preferences.ShowMessageBubblesChanged -= HandleIdeAppPreferencesShowMessageBubblesChanged;
714
 
                        MonoDevelop.Ide.Gui.Pads.ErrorListPad errorListPad = IdeApp.Workbench.GetPad<MonoDevelop.Ide.Gui.Pads.ErrorListPad> ().Content as MonoDevelop.Ide.Gui.Pads.ErrorListPad;
715
955
                        TaskService.TaskToggled -= HandleErrorListPadTaskToggled;
716
956
                        
717
957
                        DisposeErrorMarkers ();
719
959
                        ClipbardRingUpdated -= UpdateClipboardRing;
720
960
 
721
961
                        widget.TextEditor.Document.TextReplacing -= OnTextReplacing;
722
 
                        widget.TextEditor.Document.TextReplacing -= OnTextReplaced;
 
962
                        widget.TextEditor.Document.TextReplaced -= OnTextReplaced;
723
963
                        widget.TextEditor.Document.ReadOnlyCheckDelegate = null;
724
964
                        widget.TextEditor.Options.Changed -= HandleWidgetTextEditorOptionsChanged;
725
965
 
 
966
                        TextEditorService.FileExtensionAdded -= HandleFileExtensionAdded;
 
967
                        TextEditorService.FileExtensionRemoved -= HandleFileExtensionRemoved;
 
968
 
726
969
                        DebuggingService.DebugSessionStarted -= OnDebugSessionStarted;
727
970
                        DebuggingService.CurrentFrameChanged -= currentFrameChanged;
728
971
                        DebuggingService.StoppedEvent -= currentFrameChanged;
750
993
                        breakpointRemoved = null;
751
994
                        breakpointStatusChanged = null;
752
995
 
 
996
                        if (ownerDocument != null) {
 
997
                                ownerDocument.DocumentParsed -= HandleDocumentParsed;
 
998
                                ownerDocument = null;
 
999
                        }
 
1000
 
 
1001
                        RemoveMarkerQueue ();
753
1002
                }
754
1003
                
755
1004
                public Ambience GetAmbience ()
772
1021
                
773
1022
                void OnTextReplacing (object s, DocumentChangeEventArgs a)
774
1023
                {
775
 
                        oldReplaceText = a.RemovedText;
 
1024
                        oldReplaceText = a.RemovedText.Text;
776
1025
                }
777
1026
                
778
1027
                void OnTextReplaced (object s, DocumentChangeEventArgs a)
792
1041
 
793
1042
                        if (a.InsertedText != null) {
794
1043
                                i = 0;
795
 
                                string sb = a.InsertedText;
 
1044
                                string sb = a.InsertedText.Text;
796
1045
                                while (i < sb.Length) {
797
1046
                                        if (sb [i] == '\n')
798
1047
                                                lines++;
800
1049
                                }
801
1050
                        }
802
1051
                        if (lines != 0)
803
 
                                TextFileService.FireLineCountChanged (this, location.Line, lines, location.Column);
 
1052
                                TextEditorService.NotifyLineCountChanged (this, location.Line, lines, location.Column);
804
1053
                }
805
1054
 
806
1055
                void OnCurrentFrameChanged (object s, EventArgs args)
873
1122
                void UpdatePinnedWatches ()
874
1123
                {
875
1124
                        foreach (PinnedWatchInfo wi in pinnedWatches) {
876
 
                                widget.TextEditorContainer.Remove (wi.Widget);
 
1125
                                widget.TextEditor.Remove (wi.Widget);
877
1126
                                wi.Widget.Destroy ();
878
1127
                        }
879
1128
                        pinnedWatches.Clear ();
898
1147
                                widget.TextEditor.TextViewMargin.GetLayout (line).Layout.GetPixelSize (out lw, out lh);
899
1148
                                w.OffsetX = (int)widget.TextEditor.TextViewMargin.XOffset + lw + 4;
900
1149
                        }
901
 
                        wi.Widget = new PinnedWatchWidget (widget.TextEditorContainer, w);
 
1150
                        wi.Widget = new PinnedWatchWidget (widget.TextEditor, w);
902
1151
                        
903
1152
//                      wi.Marker = new DebugValueMarker (widget.TextEditor, line, w);
904
1153
                        wi.Watch = w;
906
1155
//                      if (w.Value != null)
907
1156
//                              wi.Marker.AddValue (w.Value);
908
1157
 
909
 
                        widget.TextEditorContainer.AddTopLevelWidget (wi.Widget, w.OffsetX, w.OffsetY);
 
1158
                        widget.TextEditor.AddTopLevelWidget (wi.Widget, w.OffsetX, w.OffsetY);
910
1159
                        
911
1160
//                      widget.TextEditor.QueueDraw ();
912
1161
                }
914
1163
                void OnDebugSessionStarted (object sender, EventArgs e)
915
1164
                {
916
1165
                        UpdatePinnedWatches ();
 
1166
                        foreach (var marker in currentErrorMarkers) {
 
1167
                                marker.IsVisible = false;
 
1168
                        }
 
1169
                        DebuggingService.DebuggerSession.TargetExited += HandleTargetExited;
 
1170
                }
 
1171
 
 
1172
                void HandleTargetExited (object sender, EventArgs e)
 
1173
                {
 
1174
                        foreach (var marker in currentErrorMarkers) {
 
1175
                                marker.IsVisible = true;
 
1176
                        }
917
1177
                }
918
1178
                
919
1179
                void OnWatchAdded (object s, PinnedWatchEventArgs args)
927
1187
                        foreach (PinnedWatchInfo wi in pinnedWatches) {
928
1188
                                if (wi.Watch == args.Watch) {
929
1189
                                        pinnedWatches.Remove (wi);
930
 
                                        widget.TextEditorContainer.Remove (wi.Widget);
 
1190
                                        widget.TextEditor.Remove (wi.Widget);
931
1191
                                        wi.Widget.Destroy ();
932
1192
                                        break;
933
1193
                                }
939
1199
                        foreach (PinnedWatchInfo wi in pinnedWatches) {
940
1200
                                if (wi.Watch == args.Watch) {
941
1201
                                        wi.Widget.ObjectValue = wi.Watch.Value;
942
 
                                        widget.TextEditorContainer.MoveTopLevelWidget (wi.Widget, args.Watch.OffsetX, args.Watch.OffsetY);
 
1202
                                        widget.TextEditor.MoveTopLevelWidget (wi.Widget, args.Watch.OffsetX, args.Watch.OffsetY);
943
1203
                                        break;
944
1204
                                }
945
1205
                        }
1065
1325
                
1066
1326
                void OnIconButtonPress (object s, MarginMouseEventArgs args)
1067
1327
                {
 
1328
                        if (args.LineNumber < DocumentLocation.MinLine)
 
1329
                                return;
 
1330
 
1068
1331
                        if (args.TriggersContextMenu ()) {
1069
 
                                TextEditor.Caret.Line = args.LineNumber;
1070
 
                                TextEditor.Caret.Column = 1;
1071
 
                                IdeApp.CommandService.ShowContextMenu (WorkbenchWindow.ExtensionContext, "/MonoDevelop/SourceEditor2/IconContextMenu/Editor");
 
1332
                                if (TextEditor.Caret.Line != args.LineNumber) {
 
1333
                                        TextEditor.Caret.Line = args.LineNumber;
 
1334
                                        TextEditor.Caret.Column = 1;
 
1335
                                }
 
1336
 
 
1337
                                IdeApp.CommandService.ShowContextMenu (
 
1338
                                        TextEditor,
 
1339
                                        args.RawEvent as Gdk.EventButton,
 
1340
                                        WorkbenchWindow.ExtensionContext ?? AddinManager.AddinEngine,
 
1341
                                        "/MonoDevelop/SourceEditor2/IconContextMenu/Editor");
1072
1342
                        } else if (args.Button == 1) {
1073
1343
                                if (!string.IsNullOrEmpty (this.Document.FileName)) {
1074
 
                                        if (args.LineSegment != null)
1075
 
                                                DebuggingService.Breakpoints.Toggle (this.Document.FileName, args.LineNumber);
 
1344
                                        if (args.LineSegment != null) {
 
1345
                                                int column = TextEditor.Caret.Line == args.LineNumber ? TextEditor.Caret.Column : 1;
 
1346
 
 
1347
                                                DebuggingService.Breakpoints.Toggle (this.Document.FileName, args.LineNumber, column);
 
1348
                                        }
1076
1349
                                }
1077
1350
                        }
1078
1351
                }
1128
1401
                        }*/
1129
1402
                        if (MiscActions.CancelPreEditMode (TextEditor.GetTextEditorData ()))
1130
1403
                                return;
1131
 
                        this.Document.Undo ();
 
1404
                        MiscActions.Undo (this.TextEditor.GetTextEditorData ());
1132
1405
                }
1133
1406
                
1134
1407
                public bool EnableRedo {
1158
1431
                {
1159
1432
                        if (MiscActions.CancelPreEditMode (TextEditor.GetTextEditorData ()))
1160
1433
                                return;
1161
 
                        this.Document.Redo ();
 
1434
                        MiscActions.Redo (this.TextEditor.GetTextEditorData ());
1162
1435
                }
1163
1436
                
1164
1437
                public IDisposable OpenUndoGroup ()
1245
1518
                        set {
1246
1519
                                this.IsDirty = true;
1247
1520
                                this.widget.TextEditor.Document.Text = value;
1248
 
                                if (TextChanged != null)
1249
 
                                        TextChanged (this, new TextChangedEventArgs (0, Length));
1250
1521
                        }
1251
1522
                }
1252
1523
                
1347
1618
                #region IClipboardHandler
1348
1619
                public bool EnableCut {
1349
1620
                        get {
1350
 
                                return widget.EditorHasFocus;
 
1621
                                return !widget.SearchWidgetHasFocus;
1351
1622
                        }
1352
1623
                }
1353
1624
 
1359
1630
 
1360
1631
                public bool EnablePaste {
1361
1632
                        get {
1362
 
                                return widget.EditorHasFocus;
 
1633
                                return EnableCut;
1363
1634
                        }
1364
1635
                }
1365
1636
 
1366
1637
                public bool EnableDelete {
1367
1638
                        get {
1368
 
                                return widget.EditorHasFocus;
 
1639
                                return EnableCut;
1369
1640
                        }
1370
1641
                }
1371
1642
 
1372
1643
                public bool EnableSelectAll {
1373
1644
                        get {
1374
 
                                return widget.EditorHasFocus;
 
1645
                                return EnableCut;
1375
1646
                        }
1376
1647
                }
1377
1648
                
1459
1730
                {
1460
1731
                        CodeCompletionContext result = new CodeCompletionContext ();
1461
1732
                        result.TriggerOffset = triggerOffset;
1462
 
                        DocumentLocation loc = Document.OffsetToLocation (triggerOffset);
 
1733
                        DocumentLocation loc = widget.TextEditor.Caret.Location;
1463
1734
                        result.TriggerLine = loc.Line;
1464
1735
                        result.TriggerLineOffset = loc.Column - 1;
1465
 
                        var p = DocumentToScreenLocation (loc);
1466
 
                        result.TriggerXCoord = p.X;
1467
 
                        result.TriggerYCoord = p.Y;
1468
 
                        result.TriggerTextHeight = (int)TextEditor.LineHeight;
 
1736
/*                      var p = new Cairo.Point ((int)this.widget.TextEditor.TextViewMargin.CaretVisualLocation.X,
 
1737
                                                 (int)this.widget.TextEditor.TextViewMargin.CaretVisualLocation.Y);
 
1738
                        if (widget.TextEditor.Caret.Location.Column == loc.Column)*/
 
1739
                        var p = this.widget.TextEditor.LocationToPoint (loc);
 
1740
                        int tx, ty;
 
1741
                        widget.TextEditor.ParentWindow.GetOrigin (out tx, out ty);
 
1742
                        tx += widget.TextEditor.Allocation.X + (int)p.X;
 
1743
                        ty += widget.TextEditor.Allocation.Y + (int)p.Y + (int)TextEditor.LineHeight;
 
1744
 
 
1745
                        result.TriggerXCoord = tx;
 
1746
                        result.TriggerYCoord = ty;
 
1747
                        result.TriggerTextHeight = (int)TextEditor.GetLineHeight (loc.Line);
1469
1748
                        return result;
1470
1749
                }
1471
1750
                
1474
1753
                        var p = widget.TextEditor.LocationToPoint (location);
1475
1754
                        int tx, ty;
1476
1755
                        widget.Vbox.ParentWindow.GetOrigin (out tx, out ty);
1477
 
                        tx += widget.TextEditorContainer.Allocation.X + p.X;
1478
 
                        ty += widget.TextEditorContainer.Allocation.Y + p.Y + (int)TextEditor.LineHeight;
 
1756
                        tx += widget.TextEditor.Allocation.X + p.X;
 
1757
                        ty += widget.TextEditor.Allocation.Y + p.Y + (int)TextEditor.LineHeight;
1479
1758
                        return new Gdk.Point (tx, ty);
1480
1759
                }
1481
1760
                
1539
1818
                                                data.Replace (offset, length, complete_word);
1540
1819
                                        }
1541
1820
                                        int minColumn = System.Math.Min (data.MainSelection.Anchor.Column, data.MainSelection.Lead.Column);
1542
 
                                        data.MainSelection.Anchor = new DocumentLocation (data.Caret.Line == minLine ? maxLine : minLine, minColumn);
1543
 
                                        data.MainSelection.Lead = data.Caret.Location;
1544
 
                                        
 
1821
                                        data.MainSelection = data.MainSelection.WithRange (
 
1822
                                                new DocumentLocation (data.Caret.Line == minLine ? maxLine : minLine, minColumn),
 
1823
                                                data.Caret.Location
 
1824
                                        );
 
1825
 
1545
1826
                                        data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine);
1546
1827
                                        data.Caret.PreserveSelection = false;
1547
1828
                                }
1558
1839
                public void SetCompletionText (CodeCompletionContext ctx, string partial_word, string complete_word, int wordOffset)
1559
1840
                {
1560
1841
                        var data = GetTextEditorData ();
 
1842
                        if (data == null)
 
1843
                                return;
1561
1844
                        using (var undo = data.OpenUndoGroup ()) {
1562
1845
                                SetCompletionText (data, ctx, partial_word, complete_word, wordOffset);
1563
1846
                                var formatter = CodeFormatterService.GetFormatter (data.MimeType);
1564
 
                                if (formatter.SupportsOnTheFlyFormatting) {
 
1847
                                if (formatter != null && complete_word.IndexOfAny (new [] {' ', '\t', '{', '}'}) > 0 && formatter.SupportsOnTheFlyFormatting) {
1565
1848
                                        formatter.OnTheFlyFormat (WorkbenchWindow.Document, ctx.TriggerOffset, ctx.TriggerOffset + complete_word.Length);
1566
1849
                                }
1567
1850
                        }
1655
1938
                
1656
1939
                public void FoldDefinitions ()
1657
1940
                {
1658
 
                        foreach (FoldSegment segment in Document.FoldSegments) {
1659
 
                                if (segment.FoldingType == FoldingType.TypeDefinition)
 
1941
                        bool toggle = true;
 
1942
 
 
1943
                        foreach (FoldSegment segment in Document.FoldSegments) {
 
1944
                                if (segment.FoldingType == FoldingType.TypeMember || segment.FoldingType == FoldingType.Comment)
 
1945
                                        if (segment.IsFolded)
 
1946
                                                toggle = false;
 
1947
                        }
 
1948
 
 
1949
 
 
1950
                        foreach (FoldSegment segment in Document.FoldSegments) {
 
1951
                                if (segment.FoldingType == FoldingType.TypeDefinition) {
1660
1952
                                        segment.IsFolded = false;
 
1953
                                }
1661
1954
                                if (segment.FoldingType == FoldingType.TypeMember || segment.FoldingType == FoldingType.Comment)
1662
 
                                        segment.IsFolded = true;
 
1955
                                        segment.IsFolded = toggle;
1663
1956
                        }
 
1957
 
1664
1958
                        widget.TextEditor.Caret.MoveCaretBeforeFoldings ();
1665
1959
                        Document.RequestUpdate (new UpdateAll ());
1666
1960
                        Document.CommitDocumentUpdate ();
1861
2155
                
1862
2156
                public Gtk.TargetEntry[] DragTargets { 
1863
2157
                        get {
1864
 
                                return (Gtk.TargetEntry[])ClipboardActions.CopyOperation.targetList;
 
2158
                                return ClipboardActions.CopyOperation.TargetEntries;
1865
2159
                        }
1866
2160
                }
1867
2161
                                
1983
2277
                                formatter.CorrectIndenting (policies, editorData, TextEditor.Caret.Line);
1984
2278
                        }
1985
2279
                }
1986
 
                
 
2280
 
 
2281
                [CommandUpdateHandler (TextEditorCommands.MoveBlockUp)]
 
2282
                [CommandUpdateHandler (TextEditorCommands.MoveBlockDown)]
 
2283
                void MoveBlockUpdateHandler (CommandInfo cinfo)
 
2284
                {
 
2285
                        cinfo.Enabled = widget.EditorHasFocus;
 
2286
                }
 
2287
 
1987
2288
                [CommandHandler (TextEditorCommands.MoveBlockUp)]
1988
2289
                protected void OnMoveBlockUp ()
1989
2290
                {