英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论