你可以在專案中加入 EditorConfig 檔案,設定 C++ 格式,強制所有參與專案的人使用一致的程式碼風格。 Visual Studio 與 Visual Studio Code 皆內建 EditorConfig 支援,適用於全球 Visual Studio C++ 格式設定。 EditorConfig 的設定會儲存在一個 .editorconfig 檔案中。 當該檔案存在於你的專案中時,它會優先於 Visual Studio 的格式設定。
本文件列出 Visual Studio 與 Visual Studio Code 支援的所有 EditorConfig C++ 格式設定。
C++ 有額外的 .editorconfig 設定,但不是格式化設定。 這些設定會附上它們所設定的功能:
-
包含清理設定(
cpp_include_cleanup_*和cpp_sort_includes_*)已在 Visual Studio 配置 C/C++ 包含清理中說明。 -
IntelliSense 的補全
cpp_includes_use_forward_slash設定#include(和cpp_includes_style)可在 Configure Languages > 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
案例陳述後的縮排支架
- 名稱:
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
在新行的 do-while 迴圈中放置「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值:true,false
當參數列表為空時,請在括號間插入空格
- 名稱:
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
在型別宣告中,在冒號前插入空格以表示 base
- 名稱:
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- 總是對區塊套用新行設定。
-