广告位招租

ScintillaNET 自动语法突出显示

最流行的用途是显示和编辑源代码。开箱即用,Sprilla提供了对100多种不同语言的语法高亮支持。你想要编辑的语言很可能已经被支持了。

注意:At工具是由用户@uf6429创建的,用于帮助创建下面介绍的一些语法突出显示代码。你可以在https://github.com/uuf6429/ScintillaNET-Kitchen .

选择Lexer

语言处理器在闪烁体中被称为“lexer”。在不深入分析解析器理论的情况下,了解lexer的性能是很重要的词汇分析一种语言,不是句法分析. 简言之,这意味着闪烁体提供的语言支持足以将文本分解为标记并提供语法高亮显示,但不能解释这些标记的含义或它们是否构成实际的程序。这一区别很重要,因为使用闪烁体的开发人员经常想知道如何突出显示不正确的代码。斯宾西拉不会这么做的。如果您想要的不仅仅是基本的语法高亮显示,那么就需要将闪烁与解析器甚至后台编译器结合起来。

你必须告诉闪烁的语言是什么Lexer属性设置为适当的枚举值。在某些情况下,多种语言共享同一种语言Lexer枚举值,因为这些语言共享相同的词法语法。例如Cpplexer不仅为C提供语言支持,而且还为C、C#、Java、JavaScript等提供语言支持,因为它们在词汇上是相似的。在我们的示例中,我们希望突出显示C语法,因此我们将使用Cpp公司lexer.

scintilla.Lexer = Lexer.Cpp;

定义样式

在闪烁体中进行语法高亮显示的过程称为样式化。设置文本样式时,将在Styles收藏。例如,可以为关键字指定样式定义one,而运算符可以被分配定义2. 这完全取决于雷克萨斯如何做到这一点。然而,一旦完成,你就可以自由决定什么样的风格 one2看起来像。lexer指定样式,但您定义样式外观。为了更容易知道lexer将使用哪些样式定义,可以使用风格对象包含与每个Lexer枚举值。例如,如果我们使用Cpp公司lexer并希望为单行注释(//…)设置样式,我们将使用Style.Cpp.CommentLine常量在中设置适当的样式风格收藏:

scintilla.Styles[Style.Cpp.CommentLine].Font = "Consolas";
scintilla.Styles[Style.Cpp.CommentLine].Size = 10;
scintilla.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green

要设置字符串样式,我们将:

scintilla.Styles[Style.Cpp.String].Font = "Consolas";
scintilla.Styles[Style.Cpp.String].Size = 10;
scintilla.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red

要设置数字标记的样式,我们将使用Style.Cpp.Number不变的。对于操作员,我们将使用Style.Cpp.Operator,以此类推

如果你运用你的想象力,你会发现对每一个可能的lexer令牌进行这样的操作是多么的乏味。有很多重复。为了减少代码量,您必须编写闪烁体提供了一种设置单个样式的方法,然后将其外观应用于集合中的每个样式。一般流程是:

  • 重置Default使用样式StyleResetDefault .
  • 配置Default使用所有公共属性设置样式。
  • 使用StyleClearAll方法来应用违约所有样式的样式
  • 设置任何单独的样式属性

使用这种节省时间的方法,我们可以设置C#lexer样式的外观,如下所示:

// Configuring the default style with properties
// we have common to every lexer style saves time.
scintilla.StyleResetDefault();
scintilla.Styles[Style.Default].Font = "Consolas";
scintilla.Styles[Style.Default].Size = 10;
scintilla.StyleClearAll();

// Configure the CPP (C#) lexer styles
scintilla.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
scintilla.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0); // Green
scintilla.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green
scintilla.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128); // Gray
scintilla.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
scintilla.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
scintilla.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
scintilla.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red
scintilla.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21); // Red
scintilla.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21); // Red
scintilla.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
scintilla.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
scintilla.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;

设置关键字

要提供语法高亮显示,我们需要做的最后一件事是通知lexer语言关键字和标识符是什么。由于语言通常可以年复一年地添加关键字,或者因为lexer有时可能用于多个语言,所以使关键字列表可配置是有意义的。

因为每一个闪烁词法都像一个程序,直到它本身为止,关键字集的数量和每一个的定义都因lexer而异。要确定lexer支持的关键字集,可以调用DescribeKeywordSets方法。这将打印出一个人类可读的解释,说明当前有多少组Lexer支持及其含义:

scintilla.Lexer = Lexer.Cpp;
Console.WriteLine(scintilla.DescribeKeywordSets());

// Outputs:
// Primary keywords and identifiers
// Secondary keywords and identifiers
// Documentation comment keywords
// Global classes and typedefs
// Preprocessor definitions
// Task marker and error marker keywords

基于DescribeKeywordSets我可以确定前两个集合是我对支持通用C语法高亮显示感兴趣的。要设置一组关键字,请调用SetKeywords方法。关键字集描述中的“primary”和“secondary”的含义有待解释,但我将对其进行分解,以便主关键字是C语言关键字,辅助关键字是已知的.NET类型。设置这些我会打电话给:

scintilla.SetKeywords(0, "abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe using virtual while");
scintilla.SetKeywords(1, "bool byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");

注意:关键字集中的关键字可以用空格(空格、制表符、“\r”、“\n”)字符的任意组合分隔。

我想知道上面的关键字列表在哪里?我通常从SciTE项目.properties文件——闪烁体的伴侣。同样的方法也可以通过观察便条簿langs.model.xml如果你不想查阅其他语言的文档,就不要去查阅其他语言的文档。

完整配方

下面的完整方法将使用与visualstudio默认值大致相等的颜色来突出显示C语法。

// Configuring the default style with properties
// we have common to every lexer style saves time.
scintilla.StyleResetDefault();
scintilla.Styles[Style.Default].Font = "Consolas";
scintilla.Styles[Style.Default].Size = 10;
scintilla.StyleClearAll();

// Configure the CPP (C#) lexer styles
scintilla.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
scintilla.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0); // Green
scintilla.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green
scintilla.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128); // Gray
scintilla.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
scintilla.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
scintilla.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
scintilla.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red
scintilla.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21); // Red
scintilla.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21); // Red
scintilla.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
scintilla.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
scintilla.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;
scintilla.Lexer = Lexer.Cpp;

// Set the keywords
scintilla.SetKeywords(0, "abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe using virtual while");
scintilla.SetKeywords(1, "bool byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");

注意:我们的Wiki包括很多用户提交的配方这说明了语法高亮与其他语言功能的结合。

原文链接:https://www.exueyuan.top/517.html,转载请注明出处。

0
广告位招租

评论0

请先

广告位招租
易学手机界面分析工具上线!可用于安卓手机界面,模拟器界面分析,加速自动化开发!查看详情
没有账号? 注册  忘记密码?

社交账号快速登录

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码

Warning: error_log(/www/wwwroot/exueyuan.top/wp-content/plugins/spider-analyser/#log/log-2923.txt): failed to open stream: No such file or directory in /www/wwwroot/exueyuan.top/wp-content/plugins/spider-analyser/spider.class.php on line 2900