Question

I'm registering syntax highlighting with AvalonEdit with:

PythonPrompt.SyntaxHighlighting = pythonHighlighting; 

Text can then be input by the user throughout the course of the program. Is there a way to take the formatted text and move it to a TextBlock without loosing the formatting?

As this formatted text will not be edited again I presume it is more efficient to create a TextBlock rather than creating a TextEditor on the fly.

Was it helpful?

Solution

I managed to get something that works. Its based off the latest code for AvalonEdit (HighlightedLine and RichTextModel)

        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"));
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top