VSCODE 语法高亮插件开发笔记
vscode 主题里定义了常见的语法作用域(syntax scope)的配色。常见的语法作用域的一些例子:
"keyword.operator.new",
"keyword.operator.expression",
"keyword.operator.cast",
"keyword.operator.sizeof",
"keyword.operator.alignof",
"keyword.operator.typeid",
"keyword.operator.alignas",
"keyword.operator.instanceof",
"keyword.operator.logical.python",
"keyword.operator.wordlike",
"punctuation.definition.quote.begin.markdown",
"punctuation.definition.list.begin.markdown",
"entity.other.attribute-name.class.css",
"entity.other.attribute-name.class.mixin.css",
"entity.other.attribute-name.id.css",
"entity.other.attribute-name.parent-selector.css",
"entity.other.attribute-name.pseudo-class.css",
"entity.other.attribute-name.pseudo-element.css",
"source.css.less entity.other.attribute-name.id",
"entity.other.attribute-name.scss",
"constant.numeric",
"variable.other.enummember",
"keyword.operator.plus.exponent",
"keyword.operator.minus.exponent"
通过Developer: Generate Color Theme From Current Settings 命令可以将当前主题的设置导出为json文件。在导出的json文件里列出了主题支持的语法作用域名字。

在为VSCODE开发支持新语言或脚本的插件时,在tmLanguage.json 里用正则表达式来表示语法作用域的匹配规则。
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"name": "toolset transaction script",
"patterns": [
{
"include": "#keywords"
},
{
"include": "#strings"
},
{
"include": "#sql"
},
{
"include": "#scr_sentences"
},
{
"include": "#variableTypes"
}
],
"repository": {
"keywords": {
"patterns": [{
"name": "keyword.control.toolset.tra",
"match": "\\b(EXEQRY|GOBLK|STEP|SHOWREC|SETQRY|BLOCKNUMBER|BLOCKTABLE|DUALX|POS_ABS_ROW|POS_ABS_COL|FONT|TYPE|LEN|FIELD|VAL_LOW|VAL_HGIHT|POS_FTX_ROW|POST_FTX|COL|ERRMSGTEXT|DBFILE|DBFIELD|STRUCTURE|DUMMY|YES|NO)\\b"
}]
},
"variableTypes": {
"settings": {"foreground": "#B98EFF"},
"patterns":[{
"name": "toolset.tra.variableTypes",
"match": "\\b(DATE|ROWID|IND|L\\d+|((I|C|R|L)\\*\\d+))\\b"
}]
},
"scr_sentences":{
"name": "toolset.scr.in.tra",
"begin": ">SCR",
"end":">END",
"patterns": [
{
"include": "#keywords"
},
{"math": "\\\\."}
]
},
"strings": {
"name": "string.quoted.double.toolset",
"begin": "\"",
"end": "\"",
"patterns": [
{
"name": "constant.character.escape.toolset",
"match": "\\\\."
}
]
},
"sql": {
"begin": ">SQL",
"end": ">END",
"beginCaptures": {
"0": { "name": "punctuation.code.open" }
},
"endCaptures": {
"0": { "name": "punctuation.code.close" }
},
"name": "meta.embedded.block.sql",
"patterns": [ { "include": "source.oraclesql" } ]
}
},
"scopeName": "source.tra"
}
如果需要为新语言的语法作用域取名,我建议尽量使用已有的语法作用域名字。理由是这样可以复用已有的配色主题。如果确实需要新建一个语法作用域,我们也不可能去让别人的配色主题适配我们新开发的语言插件,这时我们可以在用户设置里告诉VSCODE新语法作用域的颜色。
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "toolset.tra.variableTypes",
"settings": {
"foreground": "#ff0000"
}
}
]
}