2013-07-14 58 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中可用。 –

相关问题