Browse Source

Allow using DocumentHighlighter with ReadOnlyDocument.

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
2bbeefdaf2
  1. 6
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CustomizableHighlightingColorizer.cs
  2. 43
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/DocumentHighlighter.cs
  3. 13
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs
  4. 4
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColorizer.cs
  5. 4
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HtmlClipboard.cs
  6. 4
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/IHighlighter.cs

6
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CustomizableHighlightingColorizer.cs

@ -12,6 +12,8 @@ using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing; using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering; using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.AvalonEdit.AddIn namespace ICSharpCode.AvalonEdit.AddIn
{ {
@ -119,11 +121,11 @@ namespace ICSharpCode.AvalonEdit.AddIn
this.baseHighlighter = baseHighlighter; this.baseHighlighter = baseHighlighter;
} }
public TextDocument Document { public IDocument Document {
get { return baseHighlighter.Document; } get { return baseHighlighter.Document; }
} }
public ICSharpCode.AvalonEdit.Utils.ImmutableStack<HighlightingSpan> GetSpanStack(int lineNumber) public ImmutableStack<HighlightingSpan> GetSpanStack(int lineNumber)
{ {
return baseHighlighter.GetSpanStack(lineNumber); return baseHighlighter.GetSpanStack(lineNumber);
} }

43
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/DocumentHighlighter.cs

@ -6,9 +6,9 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Utils; using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor;
using SpanStack = ICSharpCode.AvalonEdit.Utils.ImmutableStack<ICSharpCode.AvalonEdit.Highlighting.HighlightingSpan>; using SpanStack = ICSharpCode.AvalonEdit.Utils.ImmutableStack<ICSharpCode.AvalonEdit.Highlighting.HighlightingSpan>;
namespace ICSharpCode.AvalonEdit.Highlighting namespace ICSharpCode.AvalonEdit.Highlighting
@ -26,14 +26,14 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// </summary> /// </summary>
readonly CompressingTreeList<SpanStack> storedSpanStacks = new CompressingTreeList<SpanStack>(object.ReferenceEquals); readonly CompressingTreeList<SpanStack> storedSpanStacks = new CompressingTreeList<SpanStack>(object.ReferenceEquals);
readonly CompressingTreeList<bool> isValid = new CompressingTreeList<bool>((a, b) => a == b); readonly CompressingTreeList<bool> isValid = new CompressingTreeList<bool>((a, b) => a == b);
readonly TextDocument document; readonly IDocument document;
readonly HighlightingRuleSet baseRuleSet; readonly HighlightingRuleSet baseRuleSet;
bool isHighlighting; bool isHighlighting;
/// <summary> /// <summary>
/// Gets the document that this DocumentHighlighter is highlighting. /// Gets the document that this DocumentHighlighter is highlighting.
/// </summary> /// </summary>
public TextDocument Document { public IDocument Document {
get { return document; } get { return document; }
} }
@ -52,6 +52,20 @@ namespace ICSharpCode.AvalonEdit.Highlighting
InvalidateHighlighting(); InvalidateHighlighting();
} }
/// <summary>
/// Creates a new DocumentHighlighter instance.
/// </summary>
public DocumentHighlighter(ReadOnlyDocument document, HighlightingRuleSet baseRuleSet)
{
if (document == null)
throw new ArgumentNullException("document");
if (baseRuleSet == null)
throw new ArgumentNullException("baseRuleSet");
this.document = document;
this.baseRuleSet = baseRuleSet;
InvalidateHighlighting();
}
void ILineTracker.BeforeRemoveLine(DocumentLine line) void ILineTracker.BeforeRemoveLine(DocumentLine line)
{ {
CheckIsHighlighting(); CheckIsHighlighting();
@ -125,19 +139,6 @@ namespace ICSharpCode.AvalonEdit.Highlighting
int firstInvalidLine; int firstInvalidLine;
/// <summary>
/// Highlights the specified document line.
/// </summary>
/// <param name="line">The line to highlight.</param>
/// <returns>A <see cref="HighlightedLine"/> line object that represents the highlighted sections.</returns>
[ObsoleteAttribute("Use the (int lineNumber) overload instead")]
public HighlightedLine HighlightLine(DocumentLine line)
{
if (!document.Lines.Contains(line))
throw new ArgumentException("The specified line does not belong to the document.");
return HighlightLine(line.LineNumber);
}
/// <inheritdoc/> /// <inheritdoc/>
public HighlightedLine HighlightLine(int lineNumber) public HighlightedLine HighlightLine(int lineNumber)
{ {
@ -146,7 +147,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
isHighlighting = true; isHighlighting = true;
try { try {
HighlightUpTo(lineNumber); HighlightUpTo(lineNumber);
DocumentLine line = document.GetLineByNumber(lineNumber); IDocumentLine line = document.GetLineByNumber(lineNumber);
highlightedLine = new HighlightedLine(document, line); highlightedLine = new HighlightedLine(document, line);
HighlightLineAndUpdateTreeList(line, lineNumber); HighlightLineAndUpdateTreeList(line, lineNumber);
return highlightedLine; return highlightedLine;
@ -187,7 +188,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
} }
} }
void HighlightLineAndUpdateTreeList(DocumentLine line, int lineNumber) void HighlightLineAndUpdateTreeList(IDocumentLine line, int lineNumber)
{ {
//Debug.WriteLine("Highlight line " + lineNumber + (highlightedLine != null ? "" : " (span stack only)")); //Debug.WriteLine("Highlight line " + lineNumber + (highlightedLine != null ? "" : " (span stack only)"));
spanStack = storedSpanStacks[lineNumber - 1]; spanStack = storedSpanStacks[lineNumber - 1];
@ -236,7 +237,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <remarks>This callback must not call HighlightLine or InvalidateHighlighting. /// <remarks>This callback must not call HighlightLine or InvalidateHighlighting.
/// It may call GetSpanStack, but only for the changed line and lines above. /// It may call GetSpanStack, but only for the changed line and lines above.
/// This method must not modify the document.</remarks> /// This method must not modify the document.</remarks>
protected virtual void OnHighlightStateChanged(DocumentLine line, int lineNumber) protected virtual void OnHighlightStateChanged(IDocumentLine line, int lineNumber)
{ {
} }
@ -254,10 +255,10 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// </summary> /// </summary>
HighlightedLine highlightedLine; HighlightedLine highlightedLine;
void HighlightLineInternal(DocumentLine line) void HighlightLineInternal(IDocumentLine line)
{ {
lineStartOffset = line.Offset; lineStartOffset = line.Offset;
lineText = document.GetText(line.Offset, line.Length); lineText = document.GetText(lineStartOffset, line.Length);
position = 0; position = 0;
ResetColorStack(); ResetColorStack();
HighlightingRuleSet currentRuleSet = this.CurrentRuleSet; HighlightingRuleSet currentRuleSet = this.CurrentRuleSet;

13
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Utils; using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor; using ICSharpCode.NRefactory.Editor;
@ -19,12 +20,12 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <summary> /// <summary>
/// Creates a new HighlightedLine instance. /// Creates a new HighlightedLine instance.
/// </summary> /// </summary>
public HighlightedLine(TextDocument document, DocumentLine documentLine) public HighlightedLine(IDocument document, IDocumentLine documentLine)
{ {
if (document == null) if (document == null)
throw new ArgumentNullException("document"); throw new ArgumentNullException("document");
if (!document.Lines.Contains(documentLine)) //if (!document.Lines.Contains(documentLine))
throw new ArgumentException("Line is null or not part of document"); // throw new ArgumentException("Line is null or not part of document");
this.Document = document; this.Document = document;
this.DocumentLine = documentLine; this.DocumentLine = documentLine;
this.Sections = new NullSafeCollection<HighlightedSection>(); this.Sections = new NullSafeCollection<HighlightedSection>();
@ -33,12 +34,12 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <summary> /// <summary>
/// Gets the document associated with this HighlightedLine. /// Gets the document associated with this HighlightedLine.
/// </summary> /// </summary>
public TextDocument Document { get; private set; } public IDocument Document { get; private set; }
/// <summary> /// <summary>
/// Gets the document line associated with this HighlightedLine. /// Gets the document line associated with this HighlightedLine.
/// </summary> /// </summary>
public DocumentLine DocumentLine { get; private set; } public IDocumentLine DocumentLine { get; private set; }
/// <summary> /// <summary>
/// Gets the highlighted sections. /// Gets the highlighted sections.
@ -117,7 +118,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
} }
elements.Sort(); elements.Sort();
TextDocument document = this.Document; IDocument document = this.Document;
StringWriter w = new StringWriter(CultureInfo.InvariantCulture); StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
int textOffset = startOffset; int textOffset = startOffset;
foreach (HtmlElement e in elements) { foreach (HtmlElement e in elements) {

4
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColorizer.cs

@ -6,9 +6,9 @@ using System.Diagnostics;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Threading; using System.Windows.Threading;
using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering; using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.AvalonEdit.Highlighting namespace ICSharpCode.AvalonEdit.Highlighting
{ {
@ -206,7 +206,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
this.textView = textView; this.textView = textView;
} }
protected override void OnHighlightStateChanged(DocumentLine line, int lineNumber) protected override void OnHighlightStateChanged(IDocumentLine line, int lineNumber)
{ {
base.OnHighlightStateChanged(line, lineNumber); base.OnHighlightStateChanged(line, lineNumber);
if (colorizer.lineNumberBeingColorized != lineNumber) { if (colorizer.lineNumberBeingColorized != lineNumber) {

4
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HtmlClipboard.cs

@ -66,7 +66,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <param name="segment">The part of the document to create HTML for. You can pass <c>null</c> to create HTML for the whole document.</param> /// <param name="segment">The part of the document to create HTML for. You can pass <c>null</c> to create HTML for the whole document.</param>
/// <param name="options">The options for the HTML creation.</param> /// <param name="options">The options for the HTML creation.</param>
/// <returns>HTML code for the document part.</returns> /// <returns>HTML code for the document part.</returns>
public static string CreateHtmlFragment(TextDocument document, IHighlighter highlighter, ISegment segment, HtmlOptions options) public static string CreateHtmlFragment(IDocument document, IHighlighter highlighter, ISegment segment, HtmlOptions options)
{ {
if (document == null) if (document == null)
throw new ArgumentNullException("document"); throw new ArgumentNullException("document");
@ -79,7 +79,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
StringBuilder html = new StringBuilder(); StringBuilder html = new StringBuilder();
int segmentEndOffset = segment.EndOffset; int segmentEndOffset = segment.EndOffset;
DocumentLine line = document.GetLineByOffset(segment.Offset); IDocumentLine line = document.GetLineByOffset(segment.Offset);
while (line != null && line.Offset < segmentEndOffset) { while (line != null && line.Offset < segmentEndOffset) {
HighlightedLine highlightedLine; HighlightedLine highlightedLine;
if (highlighter != null) if (highlighter != null)

4
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/IHighlighter.cs

@ -2,8 +2,8 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System; using System;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Utils; using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.AvalonEdit.Highlighting namespace ICSharpCode.AvalonEdit.Highlighting
{ {
@ -16,7 +16,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <summary> /// <summary>
/// Gets the underlying text document. /// Gets the underlying text document.
/// </summary> /// </summary>
TextDocument Document { get; } IDocument Document { get; }
/// <summary> /// <summary>
/// Gets the span stack at the end of the specified line. /// Gets the span stack at the end of the specified line.

Loading…
Cancel
Save