2013-07-14 1 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를 생성자에 전달하는 것이 싫다면 대신 IServiceProvider 인터페이스를 구현하므로 TextView에 TextEditor를 추가 할 수 있습니다. TextView는 CurrentContext에서 사용할 수 있습니다. –