gpt4 book ai didi

syntax-highlighting - 以编程方式向 AvalonEdit 添加语法高亮规则

转载 作者:行者123 更新时间:2023-12-04 18:12:02 31 4
gpt4 key购买 nike

我在运行我自己的定制语言的应用程序中使用 AvalonEdit。我想在 Avalon Edit 中加入适当的语法高亮。通常这是通过在 xml 文件中手动定义突出显示规则来完成的。

但是,我不希望在我扩展语言时突出显示规则总是与语言语法不同步。所以我希望使用我的 coco/R 解析器中已经包含的语法信息来自动生成这些规则。
那么有没有办法以编程方式向 Avalon Edit 添加语法高亮规则?

谢谢

最佳答案

下面的代码至少对我有用。

Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream s = assembly.GetManifestResourceStream("Your.xshd"))
{
using (XmlTextReader reader = new XmlTextReader(s))
{
//Load default Syntax Highlighting
InternalEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);

// Dynamic syntax highlighting for your own purpose
var rules = InternalEditor.SyntaxHighlighting.MainRuleSet.Rules;

_HighlightingRule = new HighlightingRule();
_HighlightingRule.Color = new HighlightingColor()
{
Foreground = new CustomizedBrush(SomeColor)
};

String[] wordList = PseudoGetKeywords(); // Your own logic
String regex = String.Format(@"\b({0})\w*\b", String.Join("|", wordList));
_HighlightingRule.Regex = new Regex(regex);

rules.Add(_HighlightingRule);
}
}


internal sealed class CustomizedBrush : HighlightingBrush
{
private readonly SolidColorBrush brush;
public CustomizedBrush(Color color)
{
brush = CreateFrozenBrush(color);
}

public CustomizedBrush(System.Drawing.Color c)
{
var c2 = System.Windows.Media.Color.FromArgb(c.A, c.R, c.G, c.B);
brush = CreateFrozenBrush(c2);
}

public override Brush GetBrush(ITextRunConstructionContext context)
{
return brush;
}

public override string ToString()
{
return brush.ToString();
}

private static SolidColorBrush CreateFrozenBrush(Color color)
{
SolidColorBrush brush = new SolidColorBrush(color);
brush.Freeze();
return brush;
}
}

关于syntax-highlighting - 以编程方式向 AvalonEdit 添加语法高亮规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11806764/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com