產生 EditorConfig 檔案Generate an EditorConfig file

EditorConfig 檔案有助於藉由定義程式碼樣式和格式,保持您的程式碼一致。EditorConfig files help to keep your code consistent by defining code styles and formats. 這些慣例允許 Visual Studio 提供自動化的樣式和格式修正來清除您的文件。These conventions allow Visual Studio to offer automatic style and format fixes to clean up your document. 對於 C# 開發人員,IntelliCode 可以推斷您的程式碼樣式和格式設定慣例來動態建立 EditorConfig 檔案。For C# developers, IntelliCode can infer your code style and formatting conventions to dynamically create an EditorConfig file.

您可以在 Visual Studio 中,以專案或解決方案層級新增 IntelliCode 產生的 EditorConfig 檔案 (或新增到解決方案資料夾)。You can add an IntelliCode-generated EditorConfig file at the project or solution level in Visual Studio (or to a solution folder). 若要新增預先填入的 EditorConfig 檔案,在方案總管中以滑鼠右鍵按一下想要的位置,並選擇 [新增]**** > [新增 EditorConfig (IntelliCode)]****To add a prepopulated EditorConfig file, right-click on the desired location in Solution Explorer and choose Add > New EditorConfig (IntelliCode)

在 Visual Studio 中新增 IntelliCode 產生的 EditorConfig 檔案

以這種方式新增檔案之後,IntelliCode 會自動填入它從您程式碼基底推斷的程式碼樣式慣例。After you add the file in this way, IntelliCode automatically populates it with code style conventions it infers from your codebase.

注意

程式碼樣式推斷是預覽功能,根據預設會停用。Code style inference is a preview feature and is disabled by default. 可以透過工具 > 選項 > IntelliCode 開啟It can be enabled through Tools > Options > IntelliCode. 目前僅適用於 C# 程式碼。It is currently available only for C# code. 要造訪此功能,請確保更新到最新的 Visual Studio 2019 預覽版。To access this feature be sure to update to the latest Visual Studio 2019 Preview release.

如需如何根據您程式碼基底中程式碼樣式產生 EditorConfig 檔案的詳細資訊,請參閱此部落格文章For more information about generating an EditorConfig file based on code styles in your codebase, see this blog article.

套用 EditorConfig 慣例Apply EditorConfig conventions

EditorConfig 檔案中的程式碼樣式慣例違規不會出現在 [錯誤清單]**** 或在程式碼中以「波浪線」顯示。Violations of code style conventions in an EditorConfig file don't appear in the Error List or as "squiggles" in your code. 不過,它們可以使用 [程式碼清除]**** 命令 (Visual Studio 2019) 或 [格式化文件]**** 命令 (Visual Studio 2017) 來套用。They can, however, be applied by using the Code Cleanup command (Visual Studio 2019) or the Format Document command (Visual Studio 2017).

如果您使用 Visual Studio 2017,而 [格式化文件]**** 未修正您的樣式慣例,您可能在格式化文件期間未選擇加入程式碼樣式的修正。If you're using Visual Studio 2017 and Format Document is not fixing your style conventions, you may not be opted in to code style fixes during document formatting. 例如 [套用隱含/明確類型喜好設定]**** 會執行有關使用 var 的樣式規則。For example, Apply implicit/explicit type preferences runs style rules about the use of var. 使用 [格式化選項] 對話方塊設定已經修正的問題。Configure which issues are fixed by using the formatting options dialog box. 選擇工具 > 選項 > 文字編輯器 > C# > 程式碼樣式 > 格式 > 一般 > 格式文件設定 (實驗).Choose Tools > Options > Text Editor > C# > Code Style > Formatting > General > Format Document Settings (Experiment).

推斷的慣例參考Inferred convention reference

IntelliCode 尚未支援程式碼樣式設定參考中所述慣例的完整集合。IntelliCode doesn't yet support the full set of conventions documented in code style settings reference. IntelliCode 目前可以推斷下列格式設定樣式慣例:IntelliCode can currently infer the following formatting and style conventions:

格式設定慣例Formatting conventions

  • csharp_space_between_method_declaration_parameter_list_parenthesescsharp_space_between_method_declaration_parameter_list_parentheses
  • csharp_space_between_method_declaration_empty_parameter_list_parenthesescsharp_space_between_method_declaration_empty_parameter_list_parentheses
  • csharp_space_between_method_call_name_and_opening_parenthesiscsharp_space_between_method_call_name_and_opening_parenthesis
  • csharp_space_between_method_call_parameter_list_parenthesescsharp_space_between_method_call_parameter_list_parentheses
  • csharp_space_between_method_call_empty_parameter_list_parenthesescsharp_space_between_method_call_empty_parameter_list_parentheses
  • csharp_space_after_keywords_in_control_flow_statementscsharp_space_after_keywords_in_control_flow_statements
  • csharp_space_between_parenthesescsharp_space_between_parentheses
  • csharp_space_after_castcsharp_space_after_cast
  • csharp_space_after_colon_in_inheritance_clausecsharp_space_after_colon_in_inheritance_clause
  • csharp_space_before_colon_in_inheritance_clausecsharp_space_before_colon_in_inheritance_clause
  • csharp_space_around_binary_operatorscsharp_space_around_binary_operators
  • csharp_indent_switch_labelscsharp_indent_switch_labels
  • csharp_indent_case_contentscsharp_indent_case_contents
  • csharp_indent_case_contents_when_blockcsharp_indent_case_contents_when_block
  • csharp_indent_labelscsharp_indent_labels
  • csharp_preserve_single_line_blockscsharp_preserve_single_line_blocks
  • csharp_preserve_single_line_statementscsharp_preserve_single_line_statements
  • csharp_new_line_before_open_bracecsharp_new_line_before_open_brace
  • csharp_new_line_before_elsecsharp_new_line_before_else
  • csharp_new_line_before_catchcsharp_new_line_before_catch
  • csharp_new_line_before_finallycsharp_new_line_before_finally
  • csharp_new_line_before_members_in_object_initializerscsharp_new_line_before_members_in_object_initializers
  • csharp_new_line_before_members_in_anonymous_typescsharp_new_line_before_members_in_anonymous_types
  • csharp_new_line_between_query_expression_clausescsharp_new_line_between_query_expression_clauses

樣式慣例Style conventions

  • csharp_new_line_before_catchcsharp_new_line_before_catch
  • csharp_new_line_before_elsecsharp_new_line_before_else
  • csharp_new_line_before_members_in_anonymous_typescsharp_new_line_before_members_in_anonymous_types
  • csharp_new_line_before_members_in_object_initializerscsharp_new_line_before_members_in_object_initializers
  • csharp_new_line_before_finally_stylecsharp_new_line_before_finally_style
  • csharp_new_line_between_query_expression_clausescsharp_new_line_between_query_expression_clauses
  • csharp_prefer_bracescsharp_prefer_braces
  • csharp_preferred_modifier_order_stylecsharp_preferred_modifier_order_style
  • csharp_prefer_simple_default_expressioncsharp_prefer_simple_default_expression
  • csharp_preserve_single_line_blockscsharp_preserve_single_line_blocks
  • csharp_space_after_castcsharp_space_after_cast
  • csharp_space_after_keywords_in_control_flow_statementscsharp_space_after_keywords_in_control_flow_statements
  • csharp_space_between_method_call_parameter_list_parenthesescsharp_space_between_method_call_parameter_list_parentheses
  • csharp_space_between_method_declaration_parameter_list_parenthesescsharp_space_between_method_declaration_parameter_list_parentheses
  • csharp_space_between_parenthesescsharp_space_between_parentheses
  • csharp_style_expression_bodied_accessorscsharp_style_expression_bodied_accessors
  • csharp_style_expression_bodied_constructorscsharp_style_expression_bodied_constructors
  • csharp_style_expression_bodied_indexerscsharp_style_expression_bodied_indexers
  • csharp_style_expression_bodied_methodscsharp_style_expression_bodied_methods
  • csharp_style_expression_bodied_operatorscsharp_style_expression_bodied_operators
  • csharp_style_expression_bodied_propertiescsharp_style_expression_bodied_properties
  • csharp_style_inlined_variable_declarationcsharp_style_inlined_variable_declaration
  • csharp_style_pattern_local_over_anonymous_functioncsharp_style_pattern_local_over_anonymous_function
  • csharp_style_pattern_matching_over_as_with_null_checkcsharp_style_pattern_matching_over_as_with_null_check
  • csharp_style_var_for_built_in_typescsharp_style_var_for_built_in_types
  • csharp_style_var_when_type_is_apparentcsharp_style_var_when_type_is_apparent
  • dotnet_sort_system_directives_firstdotnet_sort_system_directives_first
  • dotnet_style_explicit_tuple_namesdotnet_style_explicit_tuple_names
  • dotnet_style_object_initializerdotnet_style_object_initializer
  • dotnet_style_predefined_type_for_locals_parameters_membersdotnet_style_predefined_type_for_locals_parameters_members
  • dotnet_style_predefined_type_for_member_accessdotnet_style_predefined_type_for_member_access
  • dotnet_style_prefer_inferred_anonymous_type_member_namesdotnet_style_prefer_inferred_anonymous_type_member_names
  • dotnet_style_qualification_for_eventdotnet_style_qualification_for_event
  • dotnet_style_qualification_for_fielddotnet_style_qualification_for_field
  • dotnet_style_qualification_for_methoddotnet_style_qualification_for_method
  • dotnet_style_qualification_for_propertydotnet_style_qualification_for_property
  • dotnet_style_require_accessibility_modifiersdotnet_style_require_accessibility_modifiers

另請參閱See also