From 6de8e09a140e329a6aa7f5c37b091d54d583c02e Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 7 Jun 2014 23:28:54 +0200 Subject: [PATCH] Fix TextMarkerService crashing after switching to another document. --- ILSpy/AvalonEdit/TextMarkerService.cs | 52 +++++++++++---------------- ILSpy/TextView/DecompilerTextView.cs | 2 +- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/ILSpy/AvalonEdit/TextMarkerService.cs b/ILSpy/AvalonEdit/TextMarkerService.cs index a448d02c4..c5e0d85a0 100644 --- a/ILSpy/AvalonEdit/TextMarkerService.cs +++ b/ILSpy/AvalonEdit/TextMarkerService.cs @@ -28,20 +28,30 @@ using ICSharpCode.AvalonEdit.Rendering; namespace ICSharpCode.ILSpy.AvalonEdit { + using TextView = ICSharpCode.AvalonEdit.Rendering.TextView; /// /// Handles the text markers for a code editor. /// - public sealed class TextMarkerService : DocumentColorizingTransformer, IBackgroundRenderer, ITextMarkerService, ITextViewConnect + sealed class TextMarkerService : DocumentColorizingTransformer, IBackgroundRenderer, ITextMarkerService { TextSegmentCollection markers; - TextDocument document; + TextView textView; - public TextMarkerService(TextDocument document) + public TextMarkerService(TextView textView) { - if (document == null) - throw new ArgumentNullException("document"); - this.document = document; - this.markers = new TextSegmentCollection(document); + if (textView == null) + throw new ArgumentNullException("textView"); + this.textView = textView; + textView.DocumentChanged += OnDocumentChanged; + OnDocumentChanged(null, null); + } + + void OnDocumentChanged(object sender, EventArgs e) + { + if (textView.Document != null) + markers = new TextSegmentCollection(textView.Document); + else + markers = null; } #region ITextMarkerService @@ -50,7 +60,7 @@ namespace ICSharpCode.ILSpy.AvalonEdit if (markers == null) throw new InvalidOperationException("Cannot create a marker when not attached to a document"); - int textLength = document.TextLength; + int textLength = textView.Document.TextLength; if (startOffset < 0 || startOffset > textLength) throw new ArgumentOutOfRangeException("startOffset", startOffset, "Value must be between 0 and " + textLength); if (length < 0 || startOffset + length > textLength) @@ -102,9 +112,7 @@ namespace ICSharpCode.ILSpy.AvalonEdit /// internal void Redraw(ISegment segment) { - foreach (var view in textViews) { - view.Redraw(segment, DispatcherPriority.Normal); - } + textView.Redraw(segment, DispatcherPriority.Normal); if (RedrawRequested != null) RedrawRequested(this, EventArgs.Empty); } @@ -228,29 +236,9 @@ namespace ICSharpCode.ILSpy.AvalonEdit yield return new Point(start.X + i * offset, start.Y - ((i + 1) % 2 == 0 ? offset : 0)); } #endregion - - #region ITextViewConnect - readonly List textViews = new List(); - - void ITextViewConnect.AddToTextView(ICSharpCode.AvalonEdit.Rendering.TextView textView) - { - if (textView != null && !textViews.Contains(textView)) { - Debug.Assert(textView.Document == document); - textViews.Add(textView); - } - } - - void ITextViewConnect.RemoveFromTextView(ICSharpCode.AvalonEdit.Rendering.TextView textView) - { - if (textView != null) { - Debug.Assert(textView.Document == document); - textViews.Remove(textView); - } - } - #endregion } - public sealed class TextMarker : TextSegment, ITextMarker + sealed class TextMarker : TextSegment, ITextMarker { readonly TextMarkerService service; diff --git a/ILSpy/TextView/DecompilerTextView.cs b/ILSpy/TextView/DecompilerTextView.cs index 19d175096..9af5aefb5 100644 --- a/ILSpy/TextView/DecompilerTextView.cs +++ b/ILSpy/TextView/DecompilerTextView.cs @@ -97,7 +97,7 @@ namespace ICSharpCode.ILSpy.TextView textEditor.SetBinding(Control.FontFamilyProperty, new Binding { Source = DisplaySettingsPanel.CurrentDisplaySettings, Path = new PropertyPath("SelectedFont") }); textEditor.SetBinding(Control.FontSizeProperty, new Binding { Source = DisplaySettingsPanel.CurrentDisplaySettings, Path = new PropertyPath("SelectedFontSize") }); - textMarkerService = new TextMarkerService(textEditor.Document); + textMarkerService = new TextMarkerService(textEditor.TextArea.TextView); textEditor.TextArea.TextView.BackgroundRenderers.Add(textMarkerService); textEditor.TextArea.TextView.LineTransformers.Add(textMarkerService); textEditor.ShowLineNumbers = true;