lnt-naming-convention
确保符号的命名约定与编码样式保持一致,如项目的 .editorconfig
文件中所指定。
若要启用此功能,请在项目文件所在的同一目录中添加 .editorconfig
文件。 .editorconfig
指定项目中符号的命名约定。 例如,Unreal Engine 项目的命名约定在 gitHub 上的 .editorconfig
中指定。
将 .editorconfig
文件放在项目中后,打开 lnt-naming-convention
在 C/C++ 代码样式选项中检查 命名约定 设置。 有关如何更改此设置的信息,请参阅配置 Linter。
示例
假设你有一个 .editorconfig
文件,其中包含:
cpp_naming_style.boolean_style.capitalization = pascal_case
cpp_naming_style.boolean_style.required_prefix = b
Linter 会标记以下代码,因为它的前缀不为“b”且它符合 Pascal 大小写,如 .editorconfig
文件中所指定:
void example()
{
bool myFlag = true; // flagged because it doesn't follow the naming convention specified in the .editorconfig
}
如何解决问题
更改命名以匹配 .editorconfig
中指定的样式:
void example()
{
bool bMyFlag = true; // fixed to follow the code style specified in the .editorconfig
}
编辑器可以为你进行更改。 将光标置于标记的符号上。 选择 显示潜在修复,然后 应用命名约定:
代码编辑器显示 bool myFlag = true。 在该代码行上的游标中,将显示并选中**显示潜在修补程序**。 现在“应用命名约定”可见,它以红色显示 bool my Flag = true,以绿色显示建议的更改 bool b My Flag。 现在可以选择“应用命名约定”,将已标记的代码更改为 bool b My Flag = true。
备注
在 Visual Studio 2022 17.7 中引入,lnt-naming-convention
linter 检查可确保命名约定与 .editorconfig
文件中指定的约定保持一致。 可以将此检查应用于具有 .editorconfig
文件的任何项目。 还可以自定义 .editorconfig
文件,以适应项目的编码样式。
另请参阅
使用 EditorConfig 创建可移植的自定义编辑器设置
适用于 C++ 的 IntelliSense 代码 Linter 概述