1

我尝试使用新的 VS 功能 .editorconfig 来设置我自己的代码样式。但其中一部分不起作用。我听说过 Roslyn 错误,处理命名样式,但我不知道问题出在哪里。我的.editorconfig:

root = true

#################################
#   .NET Coding Conventions     #
#################################
[*.cs]
# Code files
indent_style = tab
insert_final_newline = true
charset = utf-8-bom

# this. preferences
dotnet_style_qualification_for_field = false:warning
dotnet_style_qualification_for_property = false:warning
dotnet_style_qualification_for_method = false:warning
dotnet_style_qualification_for_event = false:warning

# Language keywords instead of framework type names for type references
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
dotnet_style_predefined_type_for_member_access = true:warning

# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:warning
csharp_preferred_modifier_order = public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async:warning
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:warning
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:warning
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning

# Null checking preferences
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion

# Implicit and explicit types
###############
csharp_style_var_when_type_is_apparent = true:warning
csharp_style_var_elsewhere = false:none

# Expression-bodied members
csharp_style_expression_bodied_methods = when_on_single_line:none
csharp_style_expression_bodied_constructors = when_on_single_line:none
csharp_style_expression_bodied_operators = when_on_single_line:none
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion

# Pattern matching C# 7.0+
csharp_style_pattern_matching_over_is_with_cast_check = true:warning
csharp_style_pattern_matching_over_as_with_null_check = true:warning

# Inlined variable declarations C# 7.0+
csharp_style_inlined_variable_declaration = true:warning

# Expression-level preferences C# 7.0+
csharp_prefer_simple_default_expression = true:warning
csharp_style_deconstructed_variable_declaration = true:warning
csharp_style_pattern_local_over_anonymous_function = true:suggestion

# Null checking preferences
csharp_style_throw_expression = true:warning
csharp_style_conditional_delegate_call = true:warning

# Code block preferences
csharp_prefer_braces =true:none

# Organize usings
dotnet_sort_system_directives_first = true

# Newline options
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

#Indentation options
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = no_change

#Spacing options
csharp_space_after_cast = true
csharp_space_after_keywords_in_control_flow_statements = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
#csharp_space_between_parentheses
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false

#Wrapping options
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true

# Naming
# Common rule
dotnet_naming_rule.members_and_types_common_name.symbols = members_and_types_identifier
dotnet_naming_rule.members_and_types_common_name.style = pascal_case_style
dotnet_naming_rule.members_and_types_common_name.severity = error

dotnet_naming_symbols.members_and_types_identifier.applicable_kinds = class,struct,interface,enum,property,method,field,event,delegate
dotnet_naming_symbols.members_and_types_identifier.applicable_accessibilities = *
dotnet_naming_symbols.members_and_types_identifier.required_modifiers =

dotnet_naming_style.pascal_case_style.required_prefix = 
dotnet_naming_style.pascal_case_style.required_suffix =
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
dotnet_naming_style.pascal_case_style.word_separator = 

# Async methods should have "Async" suffix
dotnet_naming_rule.async_methods_end_in_async.symbols = any_async_methods
dotnet_naming_rule.async_methods_end_in_async.style = end_in_async
dotnet_naming_rule.async_methods_end_in_async.severity = error

dotnet_naming_symbols.any_async_methods.applicable_kinds = method
dotnet_naming_symbols.any_async_methods.applicable_accessibilities = *
dotnet_naming_symbols.any_async_methods.required_modifiers = async

dotnet_naming_style.end_in_async.required_prefix = 
dotnet_naming_style.end_in_async.required_suffix = Async
dotnet_naming_style.end_in_async.capitalization = pascal_case
dotnet_naming_style.end_in_async.word_separator = 

csharp_style_var_for_built_in_types = true:error

# Interfaces
dotnet_naming_rule.interface_i_prefix.symbols           = interfaces_identifier
dotnet_naming_rule.interface_i_prefix.style             = i_prefix_style
dotnet_naming_rule.interface_i_prefix.severity          = error

dotnet_naming_symbols.interfaces_identifier.applicable_kinds = interface
dotnet_naming_style.i_prefix_style.capitalization            = pascal_case
dotnet_naming_style.i_prefix_style.required_prefix           = _

# Parameter
dotnet_naming_rule.parameters_in_camel.symbols  = parameters_identifier
dotnet_naming_rule.parameters_in_camel.style    = camel_case_style
dotnet_naming_rule.parameters_in_camel.severity = error

dotnet_naming_symbols.parameters_identifier.applicable_kinds  = parameter
dotnet_naming_style.camel_case_style.capitalization           = camel_case

查看“# Naming”部分和下面的屏幕截图。如您所见,“#Common rule”和“csharp_style_var_for_built_in_types”工作正常——第 17 和 21 行。但是“#Async 方法应该有“Async”后缀”、“#Interfaces”和参数不起作用(第 19、13 和30)。

在此处输入图像描述

4

1 回答 1

1

这是因为

在 .editorconfig 文件中,命名约定应从最具体到最不具体进行排序。遇到的第一个可以应用的规则是唯一应用的规则。

参考:https ://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions?view=vs-2017

因此,在 的情况下DisplayResultAyncmembers_and_types_common_name规则被匹配并应用,其余的命名规则被跳过。解决方案是简单地更改顺序。在这种情况下,看起来你应该把“通用规则”放在最后。

于 2018-12-12T15:32:37.257 回答