AvalonEdit WPF TextEditor (SharpDevelop): how to highlight a specific range of text?

The incredibly amazing AvalonEdit WPF TextEditor control does not seem to have an important function, or at least I cannot figure it out. Given offset and length, select this part in a TextDocument with HighlightColor . Just right?

Unfortunately no. I have RTFM, and the documentation on the Syntax Highlight confused me even more. Someone else asked the same question on the SharpDevelop forums , and I'm afraid that I cannot understand how Mr. Grunwald answers.

Here is my attempt using the DocumentHighlighter class (of course, it does not work):

textEditor1.Text = "1234567890"; HighlightingColor c = new HighlightingColor() { FontWeight = FontWeights.ExtraBold }; DocumentHighlighter dh = new DocumentHighlighter(textEditor1.Document, new HighlightingRuleSet()); HighlightedLine hl = dh.HighlightLine(1); hl.Sections.Add(new HighlightedSection() { Color = c, Offset = 1, Length = 3 }); 

Thanks for the help!

+4
source share
4 answers

You saw this in this article - it seems exactly what you are asking for:

 public class ColorizeAvalonEdit : DocumentColorizingTransformer { protected override void ColorizeLine(DocumentLine line) { int lineStartOffset = line.Offset; string text = CurrentContext.Document.GetText(line); int start = 0; int index; while ((index = text.IndexOf("AvalonEdit", start)) >= 0) { base.ChangeLinePart( lineStartOffset + index, // startOffset lineStartOffset + index + 10, // endOffset (VisualLineElement element) => { // This lambda gets called once for every VisualLineElement // between the specified offsets. Typeface tf = element.TextRunProperties.Typeface; // Replace the typeface with a modified version of // the same typeface element.TextRunProperties.SetTypeface(new Typeface( tf.FontFamily, FontStyles.Italic, FontWeights.Bold, tf.Stretch )); }); start = index + 1; // search for next occurrence } } } 

He highlights the word AvalonEdit in bold.

+5
source

Some background information: AvalonEdit is a code editor, not a text editor. There is no such thing as “highlight a part of a document” - a document stores only plain text.

Highlight is calculated on demand, only for rows currently displayed. If you need custom highlighting, you need to add a step to the calculation of the selection - this is what the ColorizeAvalonEdit class does in the example sent by mzabsky.

+7
source

To do this, you need to create your own ColorizingTransformer. The above example actually highlights a particular word. However, you can modify it a bit to colorize or highlight a part.

I used Avalon TextEditor for the Console + project (which is now at a very primitive stage)

 public class OffsetColorizer : DocumentColorizingTransformer { public int StartOffset { get; set; } public int EndOffset { get; set; } protected override void ColorizeLine(DocumentLine line) { if (line.Length == 0) return; if (line.Offset < StartOffset || line.Offset > EndOffset) return; int start = line.Offset > StartOffset ? line.Offset : StartOffset; int end = EndOffset > line.EndOffset ? line.EndOffset : EndOffset; ChangeLinePart(start, end, element => element.TextRunProperties.SetForegroundBrush(Brushes.Red)); } } 

And you can add a colorizer to the editor by adding it to the LineTransformers collection.

 tbxConsole.TextArea.TextView.LineTransformers.Add(_offsetColorizer); 
+4
source

I know this is a pretty old question, but I decided to share my solution. I'm not sure if this solution was implemented in AvalonEdit, because this question was answered, but I believe that the OffsetColorizer class does not actually select a line: it just changes the background color of the line.

My solution is this:

 textEditor.SelectionStart = offset; textEditor.SelectionLength = length; 

However, this can be continued as follows:

 public void SelectText(int offset, int length) { //Get the line number based off the offset. var line = textEditor.Document.GetLineByOffset(offset); var lineNumber = line.LineNumber; //Select the text. textEditor.SelectionStart = offset; textEditor.SelectionLength = length; //Scroll the textEditor to the selected line. var visualTop = textEditor.TextArea.TextView.GetVisualTopByDocumentLine(lineNumber); textEditor.ScrollToVerticalOffset(visualTop); } 

I find that this solution works better, so that instead of just coloring the line, he actually selects it: this means that it can be copied with Ctrl + C.

Hope this helps people in the future.

+1
source

Source: https://habr.com/ru/post/1340178/


All Articles