void PushColor(HighlightingColor color) { if (highlightedLine == null) { return; } if (color == null) { highlightedSectionStack.Push(null); } else if (lastPoppedSection != null && lastPoppedSection.Color == color && lastPoppedSection.Offset + lastPoppedSection.Length == position + lineStartOffset) { highlightedSectionStack.Push(lastPoppedSection); lastPoppedSection = null; } else { HighlightedSection hs = new HighlightedSection { Offset = position + lineStartOffset, Color = color }; highlightedLine.Sections.Add(hs); highlightedSectionStack.Push(hs); lastPoppedSection = null; } }
/// <summary> /// Produces HTML code for a section of the line, with <span class="colorName"> tags. /// </summary> public string ToHtml(int startOffset, int endOffset, HtmlOptions options) { if (options == null) { throw new ArgumentNullException("options"); } int documentLineStartOffset = this.DocumentLine.Offset; int documentLineEndOffset = documentLineStartOffset + this.DocumentLine.Length; if (startOffset < documentLineStartOffset || startOffset > documentLineEndOffset) { throw new ArgumentOutOfRangeException("startOffset", startOffset, "Value must be between " + documentLineStartOffset + " and " + documentLineEndOffset); } if (endOffset < startOffset || endOffset > documentLineEndOffset) { throw new ArgumentOutOfRangeException("endOffset", endOffset, "Value must be between startOffset and " + documentLineEndOffset); } ISegment requestedSegment = new SimpleSegment(startOffset, endOffset - startOffset); List <HtmlElement> elements = new List <HtmlElement>(); for (int i = 0; i < this.Sections.Count; i++) { HighlightedSection s = this.Sections[i]; if (s.GetOverlap(requestedSegment).Length > 0) { elements.Add(new HtmlElement(s.Offset, i, false, s.Color)); elements.Add(new HtmlElement(s.Offset + s.Length, i, true, s.Color)); } } elements.Sort(); TextDocument document = this.Document; StringWriter w = new StringWriter(CultureInfo.InvariantCulture); int textOffset = startOffset; foreach (HtmlElement e in elements) { int newOffset = Math.Min(e.Offset, endOffset); if (newOffset > startOffset) { HtmlClipboard.EscapeHtml(w, document.GetText(textOffset, newOffset - textOffset), options); } textOffset = Math.Max(textOffset, newOffset); if (e.IsEnd) { w.Write("</span>"); } else { w.Write("<span"); options.WriteStyleAttributeForColor(w, e.Color); w.Write('>'); } } HtmlClipboard.EscapeHtml(w, document.GetText(textOffset, endOffset - textOffset), options); return(w.ToString()); }
void ResetColorStack() { Debug.Assert(position == 0); lastPoppedSection = null; if (highlightedLine == null) { highlightedSectionStack = null; } else { highlightedSectionStack = new Stack <HighlightedSection>(); foreach (HighlightingSpan span in spanStack.Reverse()) { PushColor(span.SpanColor); } } }
void PopColor() { if (highlightedLine == null) { return; } HighlightedSection s = highlightedSectionStack.Pop(); if (s != null) { s.Length = (position + lineStartOffset) - s.Offset; if (s.Length == 0) { highlightedLine.Sections.Remove(s); } else { lastPoppedSection = s; } } }