2013-07-14 59 views
2

我想突出顯示AvalonEdit中選定(突出顯示)文本的所有實例。 VS2010可以做到這一點,它是一個方便的功能。我知道我需要按照以下代碼實現DocumentColorizingTransformer,但不知道如何從文檔中獲取所選文本。選擇信息在「CurrentContext」中不可用。選擇AvalonEdit中突出顯示的單詞的所有實例

下面的代碼找到「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 
} } } 

回答

2

當前的文本選擇在TextEditor上可用,因此您可以使用ColorizeAvalonEdit類中的文本選項。

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

然而,這還不足以讓所有選定的文本爲粗體和斜體在每一行,因爲只有被修改將要更新的行。爲了讓所有選定的文本變得粗體和斜體,當選擇改變時,我不得不刷新文本編輯器。

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(); 
    } 
+0

「TextEditor上提供了當前的文本選擇,因此您可以使用ColorizeAvalonEdit類中的文本選項。」這是這個問題的重要組成部分。你如何從CurrentContext訪問textEditor。它似乎不在那裏? – paligap

+0

正確,它在CurrentContext上不可用,因此您必須從其他位置獲取它。查看SharpDevelop源代碼,似乎是如果自定義着色變壓器需要文本編輯器,那麼它將在創建時傳遞到變壓器中。 –

+1

如果您不喜歡在構造函數中傳遞TextEditor,則可以將其添加到TextView,因爲它實現了IServiceProvider接口。 TextView在CurrentContext中可用。 –

相關問題