簡體   English   中英

AvalonEdit:獲取語法突出顯示的文本

[英]AvalonEdit: Getting Syntax Highlighted Text

我正在使用AvalonEdit注冊語法高亮顯示:

PythonPrompt.SyntaxHighlighting = pythonHighlighting; 

然后,用戶可以在整個程序過程中輸入文本。 有沒有辦法采取格式化的文本並將其移動到TextBlock而不失去格式?

由於此格式化的文本將不再進行編輯,因此我認為創建一個TextBlock而不是動態創建TextEditor更為有效。

我設法得到了有用的東西。 它基於AvalonEdit的最新代碼( HighlightedLineRichTextModel

        TextBlock Item = new TextBlock();
        Code = Code.Replace("\t", new String(' ', Editor.Options.IndentationSize));

        TextDocument Document = new TextDocument(Code);
        IHighlightingDefinition HighlightDefinition = Editor.SyntaxHighlighting;
        IHighlighter Highlighter = new DocumentHighlighter(Document, HighlightDefinition.MainRuleSet);

        int LineCount = Document.LineCount;
        for (int LineNumber = 1; LineNumber <= Document.LineCount; LineNumber++)
        {
            HighlightedLine Line = Highlighter.HighlightLine(LineNumber);

            string LineText = Document.GetText(Line.DocumentLine);
            int Offset = Line.DocumentLine.Offset;

            int SectionCount = Line.Sections.Count;
            for (int SectionNumber = 0; SectionNumber < SectionCount; SectionNumber++)
            {
                HighlightedSection Section = Line.Sections[SectionNumber];

                //Deal with previous text
                if (Section.Offset > Offset)
                {
                    Item.Inlines.Add(
                        new Run(Document.GetText(Offset, Section.Offset - Offset))
                    );
                }

                Run RunItem = new Run(Document.GetText(Section));

                if (RunItem.Foreground != null)
                {
                    RunItem.Foreground = Section.Color.Foreground.GetBrush(null);
                }
                if (Section.Color.FontWeight != null)
                {
                    RunItem.FontWeight = Section.Color.FontWeight.Value;
                }

                Item.Inlines.Add(RunItem);

                Offset = Section.Offset + Section.Length;
            }

            //Deal with stuff at end of line
            int LineEnd = Line.DocumentLine.Offset + LineText.Length;
            if (LineEnd > Offset)
            {
                Item.Inlines.Add(
                    new Run(Document.GetText(Offset, LineEnd-Offset))
                );
            }

            //If not last line add a new line
            if (LineNumber < LineCount)
            {
                Item.Inlines.Add(new Run("\n"));
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM