2011-02-17 3 views
4

믿을 수 없을 정도로 멋진 AvalonEdit WPF TextEditor 컨트롤은 중요한 기능이 부족하거나 적어도 파악할 수 없습니다. 오프셋과 길이를 지정하면 TextDocument의 해당 부분을 HighlightColor으로 강조 표시합니다. 간단 하죠?AvalonEdit WPF TextEditor (SharpDevelop) : 특정 텍스트 범위를 강조 표시하는 방법?

어쨌든. 나는 RTFM을 사용했고, "Syntax Highlighting"에 대한 문서는 나를 혼란스럽게 만들었다. Someone else asked the same question in the SharpDevelop forums, 나는 Herr Grunwald의 대답을 이해할 수 없다는 것을 두려워합니다.

여기 내 시도, (물론 작동하지 않음) DocumentHighlighter 클래스를 사용하여입니다 :

textEditor1.Text = "1234567890"; 

    HighlightingColor c = new HighlightingColor() { FontWeight = FontWeights.ExtraBold }; 

    DocumentHighlighter dh = new DocumentHighlighter(textEditor1.Document, new HighlightingRuleSet()); 
    HighlightedLine hl = dh.HighlightLine(1); 

    hl.Sections.Add(new HighlightedSection() { Color = c, Offset = 1, Length = 3 }); 

이 도움을 주셔서 감사가!

답변

5

당신은 this article이 봤어 - 당신이 요구 정확히 것 같다 :

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

이 굵게 단어 AvalonEdit을 강조한다.

+0

'endOffset' ='lineStartOffset + index + "AvalonEdit".Length'가 아니겠습니까?전체 문자열의 길이를 더하면 10의 임의 오프셋 만이 아닙니다. –

7

일부 배경 정보 : AvalonEdit는 서식있는 텍스트 편집기가 아닌 코드 편집기입니다. "문서의 일부 강조 표시"와 같은 것은 없습니다. 문서는 일반 텍스트 만 저장합니다.

강조 표시는 현재 볼 수있는 행에 대해서만이라고 계산 된 입니다. 사용자 지정 강조 표시를 원하면 하이라이트 계산에 단계를 추가해야합니다. mzabsky가 게시 한 예제의 ColorizeAvalonEdit 클래스가 수행하는 것입니다.

+0

Daniel에게 감사드립니다. 지금 AvalonEdit을 조금 더 잘 이해하고 있으며 작동하는 솔루션을 만들었습니다. –

4

이렇게하려면 맞춤형 ColorizingTransformer를 만들어야합니다. 위의 예는 실제로 특정 단어를 강조 표시 한 것입니다. 그래도 부분을 색칠하거나 강조 표시하려면 조금만 변경할 수 있습니다.

나는 (그 순간에 아주 원시적 인 단계에) 내 Console+ 프로젝트

public class OffsetColorizer : DocumentColorizingTransformer 
{ 
    public int StartOffset { get; set; } 
    public int EndOffset { get; set; } 

    protected override void ColorizeLine(DocumentLine line) 
    { 
     if (line.Length == 0) 
      return; 

     if (line.Offset < StartOffset || line.Offset > EndOffset) 
      return; 

     int start = line.Offset > StartOffset ? line.Offset : StartOffset; 
     int end = EndOffset > line.EndOffset ? line.EndOffset : EndOffset; 

     ChangeLinePart(start, end, element => element.TextRunProperties.SetForegroundBrush(Brushes.Red)); 
    } 
} 

에 대한 아발론 문서 편집기를 사용 그리고 당신은 LineTransformers 컬렉션에 추가하여 편집기에 colorizer를 추가 할 수 있습니다.

tbxConsole.TextArea.TextView.LineTransformers.Add(_offsetColorizer); 
1

나는 이것이 꽤 오래된 질문이라는 것을 알고 있지만, 나는 나의 해결책을 공유 할 것이라고 생각했다. 나는이 솔루션이 AvalonEdit에 구현되었는지 확실치 않습니다. 원래이 질문에 답을 얻었으므로 OffsetColorizer 클래스가 실제로 선을 선택하지 않습니다. 단지 선의 배경색 만 변경합니다.

textEditor.SelectionStart = offset; 

textEditor.SelectionLength = length; 

을 그러나이 더과 같이 확장 할 수 있습니다 : 다음과 같이

내 솔루션입니다

public void SelectText(int offset, int length) 
{ 
    //Get the line number based off the offset. 
    var line = textEditor.Document.GetLineByOffset(offset); 
    var lineNumber = line.LineNumber; 

    //Select the text. 
    textEditor.SelectionStart = offset; 
    textEditor.SelectionLength = length; 

    //Scroll the textEditor to the selected line. 
    var visualTop = textEditor.TextArea.TextView.GetVisualTopByDocumentLine(lineNumber); 
    textEditor.ScrollToVerticalOffset(visualTop); 
} 

나는이 솔루션은 잘 작동 그건 그냥 라인을 착색이 아닌 것을 발견, 실제로 그것을 선택합니다 : Ctrl + C를 사용하여 복사 할 수 있음을 의미합니다.

희망이 사람을 미래에 도움이되기를 바랍니다.