如何重新着色AvalonEdit中的特定单词?

huangapple go评论66阅读模式
英文:

How to recolor a specific word in AvalonEdit?

问题

我正在开发一个基于.NET的编程语言以及一个使用WPF的集成开发环境(IDE)。我想要根据类型是引用类型、值类型还是接口来为类型名称设置不同的颜色(就像在Visual Studio中一样)。为此,我已经让我的编译器生成包含类型名称在源文件中的行、列和长度信息的符号。我该如何实现以下方法?

public static void Colorize(System.Windows.Media.Brush color, TextArea textArea, int line, int column, int length)
{

}
英文:

I'm developing a (.NET-based) programming language and an IDE for it in WPF. I want type names to have different colors based on wheter they are reference types, value types or interfaces (just like in Visual Studio). For this I have made my compiler emit symbols containing the row, column and length of the type name in the source file. How do I implement the following method?

public static void Colorize(System.Windows.Media.Brush color, TextArea textArea, int line, int column, int length)
{

}

答案1

得分: 1

public class HighlightedWord
{
    public int StartIndex { get; set; }
    public int Length { get; set; }
    public int Line { get; set; }
}

public partial class MainWindow : Window
{
    public static List<HighlightedWord> Words;

    public MainWindow()
    {
        InitializeComponent();
        Words = new List<HighlightedWord> 
        {
            new HighlightedWord
            {
                StartIndex = 2,
                Length = 5,
                Line = 1,
            }
        };
        yourTextEditor.TextArea.TextView.LineTransformers.Add(new ColorizeAvalonEdit());
    }

}

public class ColorizeAvalonEdit : DocumentColorizingTransformer
{
    protected override void ColorizeLine(DocumentLine line)
    {
        int lineStartOffset = line.Offset;
        foreach (HighlightedWord word in MainWindow.Words)
        {
            if (line.LineNumber != word.Line)
                continue;

            if (word.StartIndex + word.Length > line.TotalLength)
                continue;

            ChangeLinePart(
                lineStartOffset + word.StartIndex, // startOffset
                lineStartOffset + word.StartIndex + word.Length, // endOffset
                (VisualLineElement element) =>
                {
                    Typeface tf = element.TextRunProperties.Typeface;
                    element.TextRunProperties.SetTypeface(new Typeface(
                        tf.FontFamily,
                        FontStyles.Italic,
                        FontWeights.Bold,
                        tf.Stretch
                    ));
                    element.TextRunProperties.SetForegroundBrush(new SolidColorBrush(Colors.Blue));
                }
            );
        }

    }
}

Source: http://danielgrunwald.de/coding/AvalonEdit/rendering.php

英文:

I quickly wrote this, let me know if it works

    public class HighlightedWord
    {
        public int StartIndex { get; set; }
        public int Length { get; set; }
        public int Line { get; set; }
    }

    public partial class MainWindow : Window
    {
        public static List<HighlightedWord> Words;

        public MainWindow()
        {
            InitializeComponent();
            Words = new List<HighlightedWord> 
            {
                new HighlightedWord
                {
                    StartIndex = 2,
                    Length = 5,
                    Line = 1,
                }
            };
            yourTextEditor.TextArea.TextView.LineTransformers.Add(new ColorizeAvalonEdit());
        }

    }

    public class ColorizeAvalonEdit : DocumentColorizingTransformer
    {
        protected override void ColorizeLine(DocumentLine line)
        {
            int lineStartOffset = line.Offset;
            foreach (HighlightedWord word in MainWindow.Words)
            {
                if (line.LineNumber != word.Line)
                    continue;

                if (word.StartIndex + word.Length > line.TotalLength)
                    continue;

                ChangeLinePart(
                    lineStartOffset + word.StartIndex, // startOffset
                    lineStartOffset + word.StartIndex + word.Length, // endOffset
                    (VisualLineElement element) =>
                    {
                        Typeface tf = element.TextRunProperties.Typeface;
                        element.TextRunProperties.SetTypeface(new Typeface(
                            tf.FontFamily,
                            FontStyles.Italic,
                            FontWeights.Bold,
                            tf.Stretch
                        ));
                        element.TextRunProperties.SetForegroundBrush(new SolidColorBrush(Colors.Blue));
                    }
                );
            }

        }
    }

Source: http://danielgrunwald.de/coding/AvalonEdit/rendering.php

huangapple
  • 本文由 发表于 2023年5月29日 03:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定