Browse Source

Fix TextMarkerService crashing after switching to another document.

pull/469/merge
Daniel Grunwald 11 years ago
parent
commit
6de8e09a14
  1. 52
      ILSpy/AvalonEdit/TextMarkerService.cs
  2. 2
      ILSpy/TextView/DecompilerTextView.cs

52
ILSpy/AvalonEdit/TextMarkerService.cs

@ -28,20 +28,30 @@ using ICSharpCode.AvalonEdit.Rendering;
namespace ICSharpCode.ILSpy.AvalonEdit namespace ICSharpCode.ILSpy.AvalonEdit
{ {
using TextView = ICSharpCode.AvalonEdit.Rendering.TextView;
/// <summary> /// <summary>
/// Handles the text markers for a code editor. /// Handles the text markers for a code editor.
/// </summary> /// </summary>
public sealed class TextMarkerService : DocumentColorizingTransformer, IBackgroundRenderer, ITextMarkerService, ITextViewConnect sealed class TextMarkerService : DocumentColorizingTransformer, IBackgroundRenderer, ITextMarkerService
{ {
TextSegmentCollection<TextMarker> markers; TextSegmentCollection<TextMarker> markers;
TextDocument document; TextView textView;
public TextMarkerService(TextDocument document) public TextMarkerService(TextView textView)
{ {
if (document == null) if (textView == null)
throw new ArgumentNullException("document"); throw new ArgumentNullException("textView");
this.document = document; this.textView = textView;
this.markers = new TextSegmentCollection<TextMarker>(document); textView.DocumentChanged += OnDocumentChanged;
OnDocumentChanged(null, null);
}
void OnDocumentChanged(object sender, EventArgs e)
{
if (textView.Document != null)
markers = new TextSegmentCollection<TextMarker>(textView.Document);
else
markers = null;
} }
#region ITextMarkerService #region ITextMarkerService
@ -50,7 +60,7 @@ namespace ICSharpCode.ILSpy.AvalonEdit
if (markers == null) if (markers == null)
throw new InvalidOperationException("Cannot create a marker when not attached to a document"); 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) if (startOffset < 0 || startOffset > textLength)
throw new ArgumentOutOfRangeException("startOffset", startOffset, "Value must be between 0 and " + textLength); throw new ArgumentOutOfRangeException("startOffset", startOffset, "Value must be between 0 and " + textLength);
if (length < 0 || startOffset + length > textLength) if (length < 0 || startOffset + length > textLength)
@ -102,9 +112,7 @@ namespace ICSharpCode.ILSpy.AvalonEdit
/// </summary> /// </summary>
internal void Redraw(ISegment segment) internal void Redraw(ISegment segment)
{ {
foreach (var view in textViews) { textView.Redraw(segment, DispatcherPriority.Normal);
view.Redraw(segment, DispatcherPriority.Normal);
}
if (RedrawRequested != null) if (RedrawRequested != null)
RedrawRequested(this, EventArgs.Empty); 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)); yield return new Point(start.X + i * offset, start.Y - ((i + 1) % 2 == 0 ? offset : 0));
} }
#endregion #endregion
#region ITextViewConnect
readonly List<ICSharpCode.AvalonEdit.Rendering.TextView> textViews = new List<ICSharpCode.AvalonEdit.Rendering.TextView>();
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; readonly TextMarkerService service;

2
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.FontFamilyProperty, new Binding { Source = DisplaySettingsPanel.CurrentDisplaySettings, Path = new PropertyPath("SelectedFont") });
textEditor.SetBinding(Control.FontSizeProperty, new Binding { Source = DisplaySettingsPanel.CurrentDisplaySettings, Path = new PropertyPath("SelectedFontSize") }); 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.BackgroundRenderers.Add(textMarkerService);
textEditor.TextArea.TextView.LineTransformers.Add(textMarkerService); textEditor.TextArea.TextView.LineTransformers.Add(textMarkerService);
textEditor.ShowLineNumbers = true; textEditor.ShowLineNumbers = true;

Loading…
Cancel
Save