VSCode配置语法突出显示以匹配样式指南 [英] VSCode configure syntax highlighting to match a style guide
问题描述
如何更改VSCode中的语法突出显示,使其遵循特定的样式指南?例如,我要遵守 Google C ++样式指南,其中编写了成员变量为some_member_variable_
.当我使用此约定时,VSCode不会使用与标准文本不同的颜色来命名该名称.但是我有一些使用mSomeMemberVariable
约定的代码,并且确实的颜色与其他文本的颜色不同.有没有更好的配置方法?
How do I change the syntax highlighting in VSCode so that it adheres to a particular style guide? For example, I want to adhere to the Google C++ style guide where member variables are written as some_member_variable_
. When I use this convention, VSCode does not color that name differently from standard text. But I have some code that uses the mSomeMemberVariable
convention, and that does get colored differently than other text. Is there a way to configure this better?
推荐答案
TL; DR >除非找到适用的cpp Textmate语法文件,否则没有简单的方法来应用Google样式语法突出显示(我找不到一个).但是,以下是您自己实现它的方法.
TL;DR >There is no easy way to apply Google style syntax highlighting unless you find an existing cpp Textmate grammar file for it (I could not find one). However the following is how you would implement it yourself.
查看CPP语法文件(cpp.tmLanguage.json
),我们发现没有捕获Google样式成员变量的作用域模式.您可以添加一个:
Looking at the CPP syntax file (cpp.tmLanguage.json
), we see that there is no scope pattern capturing the Google-style member variables. You can add one:
{ // this is the existing scope that matches mSomeMemberVariable
"match": "\\b(f|m)[A-Z]\\w*\\b",
"name": "variable.other.readwrite.member.cpp"
},
{ // you can add this scope to match some_member_variable_
"match": "\\b([a-z][a-z\\d]*_)+\\b",
"name": "variable.other.readwrite.member.google.cpp"
}
现在,您可以通过确保其范围(或任何外部范围,例如variable.other.readwrite.member
)在主题的.json
文件中具有主题规则来确定其样式.
Now you can make sure it is styled by making sure its scope (or any of the outer scopes like variable.other.readwrite.member
), has a theme rule in your theme's .json
file.
以下是更详细的说明.在此处中,我们看到:
The following is a more detailed explanation. From here we see:
语法高亮有两个组成部分:
There are two components to syntax highlighting:
- 使用语法将文本分为标记和作用域列表
- 然后使用主题将这些范围映射到特定的颜色和样式
首先,我们需要弄清楚哪个作用域"在设计成员变量的样式:
First we need to figure out which "scope" is styling the member variable:
- 命令面板>
ctrl+shift+p
>Developer: Inspect TM Scopes
- 单击成员变量名称(
mSomeMemberVariable
) - 最具体的范围是最上面的条目.截至本文发布之时,它被称为
variable.other.readwrite.member.cpp
- Command palette >
ctrl+shift+p
>Developer: Inspect TM Scopes
- Click on the member variable name (
mSomeMemberVariable
) - The most specific scope is the top-most entry. As of this posting it is called
variable.other.readwrite.member.cpp
名称的.cpp
部分告诉我们范围是在C ++语法(语法)中定义的.到目前为止,可以在[applications_folder]/code/resources/app/extensions/cpp/syntaxes/cpp.tmLanguage.json
下找到用于cpp语法的文件(请参阅
The .cpp
part of the name tells us that the scope is defined in the C++ grammar (syntax). As of now the file used for the cpp syntax can be found under [applications_folder]/code/resources/app/extensions/cpp/syntaxes/cpp.tmLanguage.json
(see the file in the github repo).
在语法定义文件中搜索作用域名称,我们发现以下模式:
Searching for the scope name in the syntax definition file, we find the following pattern:
{
"match": "\\b(f|m)[A-Z]\\w*\\b",
"name": "variable.other.readwrite.member.cpp"
}
要查看将哪种样式应用于上述范围,请查看活动主题的*.json
文件.例如,如果您使用 Dark +(默认为深色)主题,则可以在extensions/theme-defaults/themes/dark_plus.json
找到主题json文件.在此文件中,我们找到以下文本配合主题规则:
And to see what style is applied to the above scope, we look at the active theme's *.json
file. For example, if you are using the Dark+ (default dark) theme, you can find the theme json file at extensions/theme-defaults/themes/dark_plus.json
. In this file we find the following text mate theme rule:
{
"name": "Variable and parameter name",
"scope": [
"variable",
"meta.definition.variable.name",
"support.variable",
"entity.name.variable"
],
"settings": {
"foreground": "#9CDCFE"
}
}
从此规则中,我们看到突出显示是由variable
范围应用的. (请注意,除非您专门指定内部范围样式来覆盖它,否则所有外部范围样式都将应用于内部样式).
现在,您的一种选择是将自己的范围添加到现有文件中.另一方法是编辑现有范围,使其也与Google样式成员变量regex模式匹配.另一个选择是基于CPP扩展样式文件定义自己的语法,并创建自己的Google CPP VSCode扩展.例如,采用第一种方法,您可以按以下方式编辑cpp.tmLanguage.json
:
From this rule we see that the highlighting is applied by the variable
scope. (note that all outer scope styles are applied to inner ones unless you specifically specify the inner scope style to override it)
Now one option for you would be to add your own scope to the existing file. Another would be to edit the existing scope to also match the Google style member variable regex pattern. Yet another option would be to define your own grammar based on the CPP extension style file and create your very own Google CPP VSCode Extension. For example, taking the first approach you can edit cpp.tmLanguage.json
as follows:
{
"match": "\\b([a-z][a-z\\d]*_)+\\b",
"name": "variable.other.readwrite.member.google.cpp"
}
P.S.编辑json文件后,请重新启动VSCode,以使更改生效.
P.S. After editing the json files restart VSCode for the changes to take effect.
这篇关于VSCode配置语法突出显示以匹配样式指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!