SEWorkbench
SEWorkbench copied to clipboard
Highlight all instances of selected word
Submitted by Posthy. Highlight all instances in the document of the word at caret.
For future reference, I was able to find this: http://stackoverflow.com/questions/17644292/select-all-instances-of-highlighted-word-in-avalonedit
I'm going to archive the example here so I can find it easier later. I'll do more research to see if this is already implemented somewhere.
The current text selection is available on the TextEditor so you could use that from your ColorizeAvalonEdit class.
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
}
}
}
However this is not enough to get all the selected text to be bold and italic on every line since only the line being modified will be updated. In order to get all the selected text to be bold and italic I had to refresh the text editor when the selection changed.
textEditor.TextArea.TextView.LineTransformers.Add(new ColorizeAvalonEdit(textEditor));
textEditor.TextArea.SelectionChanged += textEditor_TextArea_SelectionChanged;
void textEditor_TextArea_SelectionChanged(object sender, EventArgs e)
{
this.textEditor.TextArea.TextView.Redraw();
}