可以将 EditorConfig 文件添加到项目,以配置 C++ 格式,以便为参与项目的每个人强制实施一致的代码样式。 Visual Studio和Visual Studio Code都支持每个全局Visual Studio C++ 格式设置的内置 EditorConfig。 EditorConfig 设置存储在 .editorconfig 文件中。 当项目中存在该文件时,它优先于Visual Studio格式设置。
本文档列出了Visual Studio和Visual Studio Code支持的所有 EditorConfig C++ 格式设置。
C++ 具有不设置格式设置的其他 .editorconfig 设置。 这些设置记录了它们配置的功能:
- 在配置 C/C++ 包括清理Visual Studio中记录了包括清理设置(
cpp_include_cleanup_*和cpp_sort_includes_*)。 - 完成的
#includeIntelliSense 设置(cpp_includes_use_forward_slash和cpp_includes_style)记录在“配置语言 > C/C++ > IntelliSense”选项中。
有关以下内容的更多信息:
- 在 Visual Studio 中使用 EditorConfig 文件,请参阅使用 EditorConfig 定义一致的编码样式。
- 根据 clang 格式设置手动创建 EditorConfig 文件,请参阅 clangformat。 重命名该文件
.editorconfig并将其保存在项目中。
C++ 格式设置约定
C++ 格式 EditorConfig 设置的 cpp_前缀为 . 下面是文件可能如下所示的示例 .editorconfig :
[*.{c++,cc,cpp,cxx,h,h++,hh,hpp,hxx,inl,ipp,tlh,tli}]
cpp_indent_case_contents_when_block = true
cpp_new_line_before_open_brace_namespace = same_line
本文的其余部分列出了 Visual Studio 和 VS Code 支持的所有 EditorConfig C++ 格式设置。
缩进设置
缩进大括号
- 名称:
cpp_indent_braces - 值:
true、false
缩进每行相对
- 名称:
cpp_indent_multi_line_relative_to - 价值观:
-
outermost_parenthesis- 键入新行时,它会相对最外侧的左括号缩进。 -
innermost_parenthesis- 键入新行时,它会相对最内侧的左括号缩进。 -
statement_begin- 键入新行时,它会相对缩进到当前语句的开头。
-
在括号内,键入新行时对齐新行
- 名称:
cpp_indent_within_parentheses - 价值观:
-
align_to_parenthesis- 将内容与左括号对齐。 -
indent- 缩进新行。
-
在现有代码中,请勿使用设置对齐括号内的新行
- 名称:
cpp_indent_preserve_within_parentheses - 值:
true、false
缩进事例内容
- 名称:
cpp_indent_case_contents - 值:
true、false
缩进事例标签
- 名称:
cpp_indent_case_labels - 值:
true、false
case 语句后面的缩进大括号
- 名称:
cpp_indent_case_contents_when_block - 值:
true、false
用作参数的 lambda 的缩进大括号
- 名称:
cpp_indent_lambda_braces_when_parameter - 值:
true、false
goto 标签的位置
- 名称:
cpp_indent_goto_labels - 价值观:
-
one_left- 向左缩进一个缩进 -
leftmost_column- 移动到最左侧的列 -
none- 保留缩进
-
预处理器指令的位置
- 名称:
cpp_indent_preprocessor - 价值观:
-
one_left- 向左缩进一个缩进 -
leftmost_column- 移动到最左侧的列 -
none- 保留缩进
-
缩进访问说明符
- 名称:
cpp_indent_access_specifiers - 值:
true、false
缩进命名空间内容
- 名称:
cpp_indent_namespace_contents - 值:
true、false
保留批注缩进
- 名称:
cpp_indent_preserve_comments - 值:
true、false
换行设置
命名空间的打开大括号的位置
- 名称:
cpp_new_line_before_open_brace_namespace - 价值观:
-
new_line- 移动到新行 -
same_line- 保持相同的行,但添加空格之前 -
ignore- 不要自动重新定位
-
类型的打开大括号的位置
- 名称:
cpp_new_line_before_open_brace_type - 价值观:
-
new_line- 移动到新行 -
same_line- 保持相同的行,但添加空格之前 -
ignore- 不要自动重新定位
-
函数的打开大括号的位置
- 名称:
cpp_new_line_before_open_brace_function - 价值观:
-
new_line- 移动到新行 -
same_line- 保持相同的行,但添加空格之前 -
ignore- 不要自动重新定位
-
控制块的打开大括号的位置
- 名称:
cpp_new_line_before_open_brace_block - 价值观:
-
new_line- 移动到新行 -
same_line- 保持相同的行,但添加空格之前 -
ignore- 不要自动重新定位
-
lambda 的打开大括号的位置
- 名称:
cpp_new_line_before_open_brace_lambda - 价值观:
-
new_line- 移动到新行 -
same_line- 保持相同的行,但添加空格之前 -
ignore- 不要自动重新定位
-
将范围大括号放在单独的行上
- 名称:
cpp_new_line_scope_braces_on_separate_lines - 值:
true、false
对于空类型,将右大括号移动到与左大括号相同的行
- 名称:
cpp_new_line_close_brace_same_line_empty_type - 值:
true、false
对于空函数正文,将右大括号移动到与左大括号相同的行
- 名称:
cpp_new_line_close_brace_same_line_empty_function - 值:
true、false
在新行上放置“catch”和类似的关键字
- 名称:
cpp_new_line_before_catch - 值:
true、false
将“else”放在新行上
- 名称:
cpp_new_line_before_else - 值:
true、false
将“while”置于新行上的 do-while 循环中
- 名称:
cpp_new_line_before_while_in_do_while - 值:
true、false
间距设置
函数名称和参数列表的左括号之间的间距
- 名称:
cpp_space_before_function_open_parenthesis - 价值观:
-
insert- 插入空格 -
remove- 删除空格 -
ignore- 请勿更改空格
-
在参数列表的括号内插入空格
- 名称
cpp_space_within_parameter_list_parentheses值:truefalse
当参数列表为空时,在括号之间插入空格
- 名称:
cpp_space_between_empty_parameter_list_parentheses - 值:
true、false
在控制流语句中插入关键字和左括号之间的空格
- 名称:
cpp_space_after_keywords_in_control_flow_statements - 值:
true、false
在控件语句的括号内插入空格
- 名称:
cpp_space_within_control_flow_statement_parentheses - 值:
true、false
在打开 lambda 参数列表的括号之前插入空格
- 名称:
cpp_space_before_lambda_open_parenthesis - 值:
true、false
在 C 样式强制转换的括号内插入空格
- 名称:
cpp_space_within_cast_parentheses - 值:
true、false
在 C 样式强制转换的右括号后插入空格
- 名称:
cpp_space_after_cast_close_parenthesis - 值:
true、false
在括号表达式的括号内插入空格
- 名称:
cpp_space_within_expression_parentheses - 值:
true、false
在块的左大括号前插入空格
- 名称:
cpp_space_before_block_open_brace - 值:
true、false
在空大括号之间插入空格
- 名称:
cpp_space_between_empty_braces - 值:
true、false
在统一初始化和初始值设定项列表的左大括号前插入空格
- 名称:
cpp_space_before_initializer_list_open_brace - 值:
true、false
在统一初始化和初始值设定项列表的大括号内插入空格
- 名称:
cpp_space_within_initializer_list_braces - 值:
true、false
在统一初始化和初始值设定项列表中保留空格
- 名称:
cpp_space_preserve_in_initializer_list - 值:
true、false
在左方括号前插入空格
- 名称:
cpp_space_before_open_square_bracket - 值:
true、false
在方括号内插入空格
- 名称:
cpp_space_within_square_brackets - 值:
true、false
在空方括号之前插入空格
- 名称:
cpp_space_before_empty_square_brackets - 值:
true、false
在空方括号之间插入空格
- 名称:
cpp_space_between_empty_square_brackets - 值:
true、false
将多维数组的方括号组合在一起
- 名称:
cpp_space_group_square_brackets - 值:
true、false
在 lambda 的方括号内插入空格
- 名称:
cpp_space_within_lambda_brackets - 值:
true、false
SpaceBetweenEmptyLambdaBrackets
- 名称:
cpp_space_between_empty_lambda_brackets - 值:
true、false
在逗号之前插入空格
- 名称:
cpp_space_before_comma - 值:
true、false
在逗号后插入空格
- 名称:
cpp_space_after_comma - 值:
true、false
删除成员运算符前后的空格
- 名称:
cpp_space_remove_around_member_operators - 值:
true、false
在类型声明中的基号前插入空格
- 名称:
cpp_space_before_inheritance_colon - 值:
true、false
在构造函数冒号之前插入空格
- 名称:
cpp_space_before_constructor_colon - 值:
true、false
删除分号之前的空格
- 名称:
cpp_space_remove_before_semicolon - 值:
true、false
在分号后插入空格
- 名称:
cpp_space_after_semicolon - 值:
true、false
删除一元运算符与其操作数之间的空格
- 名称:
cpp_space_remove_around_unary_operator - 值:
true、false
二进制运算符的间距
- 名称:
cpp_space_around_binary_operator - 价值观:
-
insert- 在二进制运算符前后插入空格。 -
remove- 删除二进制运算符周围的空格。 -
ignore- 不要更改二进制运算符周围的空格。
-
赋值运算符的间距
- 名称:
cpp_space_around_assignment_operator - 价值观:
-
insert- 在赋值运算符周围插入空格。 -
remove- 删除赋值运算符周围的空格。 -
ignore- 不要更改赋值运算符周围的空格。
-
指针/引用对齐方式
- 名称:
cpp_space_pointer_reference_alignment - 价值观:
-
left- 向左对齐。 -
center- 居中对齐。 -
right- 右对齐。 -
ignore- 保持不变。
-
条件运算符的间距
- 名称:
cpp_space_around_ternary_operator - 价值观:
-
insert- 在条件运算符周围插入空格。 -
remove- 删除条件运算符周围的空格。 -
ignore- 不要更改条件运算符周围的空格。
-
包装选项
块的包装选项
- 名称:
cpp_wrap_preserve_blocks - 价值观:
-
one_liners- 不要包装单行代码块。 -
all_one_line_scopes- 不要包装代码块,其中左大括号和右大括号位于下一行。 -
never- 始终为块应用“新建行”设置。
-