public HighlightLine ( |
||
line | The line to highlight. | |
Результат | HighlightedLine |
/// <summary> /// Creates a HTML fragment from a part of a document. /// </summary> /// <param name="document">The document to create HTML from.</param> /// <param name="highlighter">The highlighter used to highlight the document.</param> /// <param name="segment">The part of the document to create HTML for. You can pass null 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, DocumentHighlighter highlighter, ISegment segment, HtmlOptions options) { if (document == null) throw new ArgumentNullException("document"); if (options == null) throw new ArgumentNullException("options"); if (segment == null) segment = new SimpleSegment(0, document.TextLength); StringBuilder html = new StringBuilder(); int segmentEndOffset = segment.EndOffset; DocumentLine line = document.GetLineByOffset(segment.Offset); while (line != null && line.Offset < segmentEndOffset) { HighlightedLine highlightedLine; if (highlighter != null) highlightedLine = highlighter.HighlightLine(line); else highlightedLine = new HighlightedLine(document, line); SimpleSegment s = segment.GetOverlap(line); if (html.Length > 0) html.AppendLine("<br>"); html.Append(highlightedLine.ToHtml(s.Offset, s.EndOffset, options)); line = line.NextLine; } return html.ToString(); }
/// <summary> /// Creates a HTML fragment from a part of a document. /// </summary> /// <param name="document">The document to create HTML from.</param> /// <param name="highlighter">The highlighter used to highlight the document.</param> /// <param name="segment">The part of the document to create HTML for. You can pass null 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, DocumentHighlighter highlighter, ISegment segment, HtmlOptions options) { if (document == null) { throw new ArgumentNullException("document"); } if (options == null) { throw new ArgumentNullException("options"); } if (segment == null) { segment = new SimpleSegment(0, document.TextLength); } StringBuilder html = new StringBuilder(); int segmentEndOffset = segment.EndOffset; DocumentLine line = document.GetLineByOffset(segment.Offset); while (line != null && line.Offset < segmentEndOffset) { HighlightedLine highlightedLine; if (highlighter != null) { highlightedLine = highlighter.HighlightLine(line); } else { highlightedLine = new HighlightedLine(line); } SimpleSegment s = segment.GetOverlap(line); if (html.Length > 0) { html.AppendLine("<br>"); } html.Append(highlightedLine.ToHtml(s.Offset, s.EndOffset, options)); line = line.NextLine; } return(html.ToString()); }
public void DetectState(TextEditor editor) { //Don't do anything if currently disabled if (this.State == AutoCompleteState.Disabled) return; //Then call the derived classes internal state detection (if any) this.DetectStateInternal(editor); //Work out what sort of token we are currently in and set our State appropriately DocumentHighlighter h = new DocumentHighlighter(editor.Document, Syntax.SyntaxManager.GetHighlighter("Turtle").MainRuleSet); DocumentLine line = editor.Document.GetLineByOffset(editor.CaretOffset); HighlightedLine result = h.HighlightLine(line); HighlightedSection current = result.Sections.FirstOrDefault(s => s.Offset <= editor.CaretOffset && (s.Offset + s.Length) > editor.CaretOffset); if (current != null) { if (current.Color != null) { switch (current.Color.Name) { case "Keyword": this.State = AutoCompleteState.Keyword; this.StartOffset = current.Offset; break; case "URI": this.State = AutoCompleteState.Uri; this.StartOffset = current.Offset; break; case "QName": this.State = AutoCompleteState.QName; this.StartOffset = current.Offset; break; case "String": String lit = editor.Document.GetText(current.Offset, current.Length); if (lit.StartsWith("\"\"\"")) { this.State = AutoCompleteState.LongLiteral; } else { this.State = AutoCompleteState.Literal; } break; case "Comment": this.State = AutoCompleteState.Comment; this.StartOffset = current.Offset; break; case "BNode": this.State = AutoCompleteState.BNode; this.StartOffset = current.Offset; break; case "Variables": this.State = AutoCompleteState.Variable; this.StartOffset = current.Offset; break; default: this.State = AutoCompleteState.None; break; } } } }
// See: http://stackoverflow.com/questions/21558386/avalonedit-getting-syntax-highlighted-text public void ApplyHighlighter(string code) { Inlines.Clear(); if (string.IsNullOrEmpty(code)) return; var item = this; var document = new TextDocument(code); var highlighter = new DocumentHighlighter(document, SyntaxHighlighting); var lineCount = document.LineCount; for (var lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) { var line = highlighter.HighlightLine(lineNumber); var lineText = document.GetText(line.DocumentLine); var offset = line.DocumentLine.Offset; var sectionCount = line.Sections.Count; for (var sectionNumber = 0; sectionNumber < sectionCount; sectionNumber++) { var section = line.Sections[sectionNumber]; //Deal with previous text if (section.Offset > offset) { item.Inlines.Add(new Run(document.GetText(offset, section.Offset - offset))); } var runItem = new Run(document.GetText(section)); if (runItem.Foreground != null) { runItem.Foreground = section.Color.Foreground.GetBrush(null); } if (section.Color.FontWeight != null) { runItem.FontWeight = section.Color.FontWeight.Value; } item.Inlines.Add(runItem); offset = section.Offset + section.Length; } //Deal with stuff at end of line var lineEnd = line.DocumentLine.Offset + lineText.Length; if (lineEnd > offset) { item.Inlines.Add(new Run(document.GetText(offset, lineEnd - offset))); } //If not last line add a new line if (lineNumber < lineCount) { item.Inlines.Add(new Run("\n")); } } }