我正在尝试通过Visual Studio Code's theme settings使用TextMate language grammars创建自定义语法样式。
具体来说,我想将所有JavaScript的保留关键字都斜体化。通过以下设置,我已经设法达到了98%的性能(包括剩余的注释)。
不幸的是,有一些规则我找不到:
storage
包含粗体箭头符号,我不想要添加。如下面的设置所示,我试图更加具体,但无法找到constructor
和const
的更具体的设置。此外,"storage.type.function"
是我可以找到的最明确的函数设置(需要function关键字,但其中包含粗箭头)。keyword
包含逻辑运算符等字符,我再次不希望包含这些字符。 keyword.operator
对于文本运算符(例如in
,instanceof
)是必需的,但包括字符运算符。eval
(严格禁止使用)或package
(未使用的未来关键字)的规则。有什么想法吗?
const settings = {
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": [
// TODO: missing keywords: package, eval
// all comment types
"comment",
// true, false, null
"constant.language",
// import, from, export, default, return, if, for, break, continue, try, catch, finally,
// throw, default, yield, await
"keyword.control",
// TODO: remove operator symbols
// in, void, delete, instanceof
"keyword.operator",
// debugger
"keyword.other",
// new
"keyword.operator.new",
// enum
"storage.type.enum",
// super, this, arguments
"variable.language",
// attributes in html, jsx, etc.
"entity.other.attribute-name",
// TODO: remove storage.type after finding explicit for constructor, const, let, var
"storage.type",
// static, extends, async, private, public, implements
"storage.modifier",
// TODO: exclude fat arrow
// function
"storage.type.function",
// class
"storage.type.class",
// interface
"storage.type.interface",
],
"settings": {
"fontStyle": "italic"
}
},
]
},
};
答案 0 :(得分:6)
事实证明,在VS Code中,您可以轻松找到所需的范围。
使用 ctrl / cmd + shift + p 打开命令搜索,然后搜索Developer: Inspect TM scopes
。然后,您可以单击想要查找范围的任何符号/单词的左侧。
要回答我自己的问题:
const
,let
,var
或function
关键字本身没有明确的范围(storage.type.function
包括保留字和箭头)。但是,功能箭头有一个明确的范围:storage.type.function.arrow
。这使我们可以包括整个storage
范围,然后明确排除箭头。keyword.operator.expression
是仅表示为单词的运算符所需的范围。eval
和package
的特定范围尚不可用。您可以将eval
和support.function
分别与package
和variable.other.readwrite
定位,但是这些范围很广,将包含许多其他结果。话虽如此,以下列出了在VS Code中使所有JavaScript的保留关键字变为斜体的必要规则(还包括注释和jsx
/ html
属性):
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": [
// all comment types
"comment",
// true, false, null
"constant.language",
// import, from, export, default, return, if, for, break, continue, try, catch, finally,
// throw, default, yield, await
"keyword.control",
// in, void, delete, instanceof
"keyword.operator.expression",
// debugger
"keyword.other",
// new
"keyword.operator.new",
// super, this, arguments
"variable.language",
// attributes in html, jsx, etc.
"entity.other.attribute-name",
// static, extends, async, private, public, implements
// constructor, const, let, var, enum, class, function, interface
// no explicit scopes for constructor, const, let, var
// also no explicit scope for function without the arrow
// therefore we enable all storage and explictly exclude the arrow in another scope
"storage",
// // no explicit scope for the eval keyword yet
// // can be targeted with the scope below, but scope is too broad
// "support.function",
// // no explicit scope for the package keyword yet
// // can be targeted with the scope below, but scope is too broad
// "variable.other.readwrite",
],
"settings": {
"fontStyle": "italic"
}
},
{
"scope": [
// function keyword does not have an explicit scope without the arrow
// therefore we explictly exclude the function arrow from being italicized
"storage.type.function.arrow",
],
"settings": {
"fontStyle": ""
}
}
]
}