Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Allow using DocumentHighlighter with ReadOnlyDocument.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Sep 19, 2011
1 parent 0aff556 commit 2bbeefd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
Expand Up @@ -12,6 +12,8 @@
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor;

namespace ICSharpCode.AvalonEdit.AddIn
{
Expand Down Expand Up @@ -119,11 +121,11 @@ public CustomizingHighlighter(IEnumerable<CustomizedHighlightingColor> customiza
this.baseHighlighter = baseHighlighter;
}

public TextDocument Document {
public IDocument Document {
get { return baseHighlighter.Document; }
}

public ICSharpCode.AvalonEdit.Utils.ImmutableStack<HighlightingSpan> GetSpanStack(int lineNumber)
public ImmutableStack<HighlightingSpan> GetSpanStack(int lineNumber)
{
return baseHighlighter.GetSpanStack(lineNumber);
}
Expand Down
Expand Up @@ -6,9 +6,9 @@
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;

using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor;
using SpanStack = ICSharpCode.AvalonEdit.Utils.ImmutableStack<ICSharpCode.AvalonEdit.Highlighting.HighlightingSpan>;

namespace ICSharpCode.AvalonEdit.Highlighting
Expand All @@ -26,14 +26,14 @@ public class DocumentHighlighter : ILineTracker, IHighlighter
/// </summary>
readonly CompressingTreeList<SpanStack> storedSpanStacks = new CompressingTreeList<SpanStack>(object.ReferenceEquals);
readonly CompressingTreeList<bool> isValid = new CompressingTreeList<bool>((a, b) => a == b);
readonly TextDocument document;
readonly IDocument document;
readonly HighlightingRuleSet baseRuleSet;
bool isHighlighting;

/// <summary>
/// Gets the document that this DocumentHighlighter is highlighting.
/// </summary>
public TextDocument Document {
public IDocument Document {
get { return document; }
}

Expand All @@ -52,6 +52,20 @@ public DocumentHighlighter(TextDocument document, HighlightingRuleSet baseRuleSe
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)
{
CheckIsHighlighting();
Expand Down Expand Up @@ -125,19 +139,6 @@ public void InvalidateHighlighting()

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/>
public HighlightedLine HighlightLine(int lineNumber)
{
Expand All @@ -146,7 +147,7 @@ public HighlightedLine HighlightLine(int lineNumber)
isHighlighting = true;
try {
HighlightUpTo(lineNumber);
DocumentLine line = document.GetLineByNumber(lineNumber);
IDocumentLine line = document.GetLineByNumber(lineNumber);
highlightedLine = new HighlightedLine(document, line);
HighlightLineAndUpdateTreeList(line, lineNumber);
return highlightedLine;
Expand Down Expand Up @@ -187,7 +188,7 @@ void HighlightUpTo(int targetLineNumber)
}
}

void HighlightLineAndUpdateTreeList(DocumentLine line, int lineNumber)
void HighlightLineAndUpdateTreeList(IDocumentLine line, int lineNumber)
{
//Debug.WriteLine("Highlight line " + lineNumber + (highlightedLine != null ? "" : " (span stack only)"));
spanStack = storedSpanStacks[lineNumber - 1];
Expand Down Expand Up @@ -236,7 +237,7 @@ static bool EqualSpanStacks(SpanStack a, SpanStack b)
/// <remarks>This callback must not call HighlightLine or InvalidateHighlighting.
/// It may call GetSpanStack, but only for the changed line and lines above.
/// This method must not modify the document.</remarks>
protected virtual void OnHighlightStateChanged(DocumentLine line, int lineNumber)
protected virtual void OnHighlightStateChanged(IDocumentLine line, int lineNumber)
{
}

Expand All @@ -254,10 +255,10 @@ protected virtual void OnHighlightStateChanged(DocumentLine line, int lineNumber
/// </summary>
HighlightedLine highlightedLine;

void HighlightLineInternal(DocumentLine line)
void HighlightLineInternal(IDocumentLine line)
{
lineStartOffset = line.Offset;
lineText = document.GetText(line.Offset, line.Length);
lineText = document.GetText(lineStartOffset, line.Length);
position = 0;
ResetColorStack();
HighlightingRuleSet currentRuleSet = this.CurrentRuleSet;
Expand Down
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;

using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor;
Expand All @@ -19,12 +20,12 @@ public class HighlightedLine
/// <summary>
/// Creates a new HighlightedLine instance.
/// </summary>
public HighlightedLine(TextDocument document, DocumentLine documentLine)
public HighlightedLine(IDocument document, IDocumentLine documentLine)
{
if (document == null)
throw new ArgumentNullException("document");
if (!document.Lines.Contains(documentLine))
throw new ArgumentException("Line is null or not part of document");
//if (!document.Lines.Contains(documentLine))
// throw new ArgumentException("Line is null or not part of document");
this.Document = document;
this.DocumentLine = documentLine;
this.Sections = new NullSafeCollection<HighlightedSection>();
Expand All @@ -33,12 +34,12 @@ public HighlightedLine(TextDocument document, DocumentLine documentLine)
/// <summary>
/// Gets the document associated with this HighlightedLine.
/// </summary>
public TextDocument Document { get; private set; }
public IDocument Document { get; private set; }

/// <summary>
/// Gets the document line associated with this HighlightedLine.
/// </summary>
public DocumentLine DocumentLine { get; private set; }
public IDocumentLine DocumentLine { get; private set; }

/// <summary>
/// Gets the highlighted sections.
Expand Down Expand Up @@ -117,7 +118,7 @@ public string ToHtml(int startOffset, int endOffset, HtmlOptions options)
}
elements.Sort();

TextDocument document = this.Document;
IDocument document = this.Document;
StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
int textOffset = startOffset;
foreach (HtmlElement e in elements) {
Expand Down
Expand Up @@ -6,9 +6,9 @@
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.NRefactory.Editor;

namespace ICSharpCode.AvalonEdit.Highlighting
{
Expand Down Expand Up @@ -206,7 +206,7 @@ public TextViewDocumentHighlighter(HighlightingColorizer colorizer, TextView tex
this.textView = textView;
}

protected override void OnHighlightStateChanged(DocumentLine line, int lineNumber)
protected override void OnHighlightStateChanged(IDocumentLine line, int lineNumber)
{
base.OnHighlightStateChanged(line, lineNumber);
if (colorizer.lineNumberBeingColorized != lineNumber) {
Expand Down
Expand Up @@ -66,7 +66,7 @@ public static void SetHtml(DataObject dataObject, string htmlFragment)
/// <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>
/// <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)
throw new ArgumentNullException("document");
Expand All @@ -79,7 +79,7 @@ public static string CreateHtmlFragment(TextDocument document, IHighlighter high

StringBuilder html = new StringBuilder();
int segmentEndOffset = segment.EndOffset;
DocumentLine line = document.GetLineByOffset(segment.Offset);
IDocumentLine line = document.GetLineByOffset(segment.Offset);
while (line != null && line.Offset < segmentEndOffset) {
HighlightedLine highlightedLine;
if (highlighter != null)
Expand Down
Expand Up @@ -2,8 +2,8 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory.Editor;

namespace ICSharpCode.AvalonEdit.Highlighting
{
Expand All @@ -16,7 +16,7 @@ public interface IHighlighter
/// <summary>
/// Gets the underlying text document.
/// </summary>
TextDocument Document { get; }
IDocument Document { get; }

/// <summary>
/// Gets the span stack at the end of the specified line.
Expand Down

0 comments on commit 2bbeefd

Please sign in to comment.