2

AvalonEdit で選択された (強調表示された) テキストのすべてのインスタンスを強調表示したいと考えています。VS2010 はこれを行い、便利な機能です。以下のコードに従って DocumentColorizingTransformer を実装する必要があることは理解していますが、ドキュメントから選択したテキストを取得する方法がわかりません。選択情報は「CurrentContext」では利用できません。

以下のコードは、「AvalonEdit」のすべてのインスタンスを検索します。選択した (強調表示された) テキストのすべてのインスタンスを見つけるにはどうすればよいですか。

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
}   }   }
4

1 に答える 1