private void HighlightLine(HighlightLineEventArgs e, ImmutableArray <SpanInfo> spans) { var line = e.Line; foreach (var span in spans) { var start = line.Offset; var end = line.Offset + line.Length; if (start > span.Span.EndPos || end < span.Span.StartPos) { continue; } var spanClassId = span.SpanClassId; var color = _highlightingStyles[spanClassId]; var startOffset = Math.Max(line.Offset, span.Span.StartPos); var endOffset = Math.Min(line.EndOffset, span.Span.EndPos); var section = new HighlightedSection { Offset = startOffset, Length = endOffset - startOffset, Color = color }; e.Sections.Add(section); } }
internal void Colorize(TextLocation start, TextLocation end, HighlightingColor color) { if (color == null) { return; } if (start.Line <= lineNumber && end.Line >= lineNumber) { int lineStartOffset = line.DocumentLine.Offset; int lineEndOffset = lineStartOffset + line.DocumentLine.Length; int startOffset = lineStartOffset + (start.Line == lineNumber ? start.Column - 1 : 0); int endOffset = lineStartOffset + (end.Line == lineNumber ? end.Column - 1 : line.DocumentLine.Length); // For some parser errors, the mcs parser produces grossly wrong locations (e.g. miscounting the number of newlines), // so we need to coerce the offsets to valid values within the line startOffset = startOffset.CoerceValue(lineStartOffset, lineEndOffset); endOffset = endOffset.CoerceValue(lineStartOffset, lineEndOffset); if (line.Sections.Count > 0) { HighlightedSection prevSection = line.Sections.Last(); if (startOffset < prevSection.Offset + prevSection.Length) { // The mcs parser sometimes creates strange ASTs with duplicate nodes // when there are syntax errors (e.g. "int A() public static void Main() {}"), // so we'll silently ignore duplicate colorization. return; //throw new InvalidOperationException("Cannot create unordered highlighting section"); } } line.Sections.Add(new HighlightedSection { Offset = startOffset, Length = endOffset - startOffset, Color = color }); } }
public void Update(ITextSourceVersion newVersion) { // Apply document changes to all highlighting sections: foreach (TextChangeEventArgs change in OldVersion.GetChangesTo(newVersion)) { foreach (HighlightedSection section in HighlightedLine.Sections) { int endOffset = section.Offset + section.Length; section.Offset = change.GetNewOffset(section.Offset); endOffset = change.GetNewOffset(endOffset); section.Length = endOffset - section.Offset; } } // The resulting sections might have become invalid: // - zero-length if section was deleted, // - a section might have moved outside the range of this document line (newline inserted in document = line split up) // So we will remove all highlighting sections which have become invalid. int lineStart = HighlightedLine.DocumentLine.Offset; int lineEnd = lineStart + HighlightedLine.DocumentLine.Length; for (int i = 0; i < HighlightedLine.Sections.Count; i++) { HighlightedSection section = HighlightedLine.Sections[i]; if (section.Offset < lineStart || section.Offset + section.Length > lineEnd || section.Length <= 0) { HighlightedLine.Sections.RemoveAt(i--); } } this.OldVersion = newVersion; this.IsValid = false; }
private void textBox1_HighlightLine(object sender, HighlightLineEventArgs e) { if (_parseResult == null) { return; } try { var line = e.Line; var spans = new HashSet <SpanInfo>(); var timer = Stopwatch.StartNew(); _parseResult.GetSpans(line.Offset, line.EndOffset, spans); _highlightingTimeSpan = timer.Elapsed; _highlightingTime.Text = _highlightingTimeSpan.ToString(); foreach (var span in spans) { HighlightingColor color; if (_highlightingStyles.TryGetValue(span.SpanClass.Name, out color)) { var startOffset = Math.Max(line.Offset, span.Span.StartPos); var endOffset = Math.Min(line.EndOffset, span.Span.EndPos); var section = new HighlightedSection { Offset = startOffset, Length = endOffset - startOffset, Color = color }; e.Sections.Add(section); } } } catch (Exception ex) { Debug.WriteLine(ex.GetType().Name + ":" + ex.Message); } }