EditorConfig for VSCode を使って Unity C# のコードを整える

規約に違反するコードにWarningやErrorの波線をつけてプロジェクトのコードに一貫性を持たせたい.
そういう時にEditorConfigが使える.プロジェクトメンバーみんなVSCoderなら合わせやすい.

EditorConfig

エディタ全般におけるルールを設けるほか、強力なリファクタリング支援を受けることが出来る機能を持つ.
UnityではC#を主に使うので,C#や.NETのコード規約に合わせ気味になる.

VSCode に EditorConfig を導入する

こちらの記事の「editorconfigを使えるようにする」を参照のこと.
www.neputa-note.net

.editorconfig ファイル

設定ファイルが必要で,「.editorconfig拡張子」がそれとなる.自分が作成したのは以下の通り.
Unity C#用にカスタマイズした.おそらくどなたでも使えると思う.

# 上位ディレクトリから .editorconfig 設定を継承する場合は、以下の行を削除します
root = true

# C# ファイル
[*.cs]
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.IDE0044.severity = none
dotnet_diagnostic.RCS1213.severity = none
dotnet_diagnostic.RCS1169.severity = none

#### コア EditorConfig オプション ####

# インデントと間隔
indent_size = 4
indent_style = space
tab_width = 4

# 改行設定
end_of_line = crlf
insert_final_newline = false

#### .NET コーディング規則 ####

# using の整理
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = false
file_header_template = unset

# this. と Me. の設定
dotnet_style_qualification_for_event = false:silent
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_method = false:silent
dotnet_style_qualification_for_property = false:silent

# 言語キーワードと BCL の種類の設定
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent

# かっこの設定
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:suggestion
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:suggestion
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:suggestion

# 修飾子設定
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent

# 式レベルの設定
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_object_initializer = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion

# フィールド設定
dotnet_style_readonly_field = true:suggestion

# パラメーターの設定
dotnet_code_quality_unused_parameters = all:suggestion

# 抑制の設定
dotnet_remove_unnecessary_suppression_exclusions = none

#### C# コーディング規則 ####

# var を優先
csharp_style_var_elsewhere = false:silent
csharp_style_var_for_built_in_types = false:silent
csharp_style_var_when_type_is_apparent = false:silent

# 式のようなメンバー
csharp_style_expression_bodied_accessors = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_lambdas = true:suggestion
csharp_style_expression_bodied_local_functions = true:suggestion
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion

# パターン マッチング設定
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_prefer_not_pattern = true:suggestion
csharp_style_prefer_pattern_matching = true:suggestion
csharp_style_prefer_switch_expression = true:suggestion

# Null チェック設定
csharp_style_conditional_delegate_call = true:suggestion

# 修飾子設定
csharp_prefer_static_local_function = true:suggestion
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent

# コード ブロックの設定
csharp_prefer_braces = true:suggestion
csharp_prefer_simple_using_statement = true:suggestion

# 式レベルの設定
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_throw_expression = true:suggestion

# 'using' ディレクティブの基本設定
csharp_using_directive_placement = outside_namespace:suggestion

#### C# 書式ルール ####

# 改行設定
csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_open_brace = all
csharp_new_line_between_query_expression_clauses = true

# インデント設定
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = true
csharp_indent_labels = flush_left
csharp_indent_switch_labels = true

# スペース設定
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# 折り返しの設定
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false

#### 命名スタイル ####

# 名前付けルール

dotnet_naming_rule.interface_should_be_begins_with_i.severity = warning
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = warning
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.method_should_be_pascal_case.severity = warning
dotnet_naming_rule.method_should_be_pascal_case.symbols = method
dotnet_naming_rule.method_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = warning
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.private_or_internal_field_should_be_underscore_and_pascal_case.severity = warning
dotnet_naming_rule.private_or_internal_field_should_be_underscore_and_pascal_case.symbols = private_or_internal_field
dotnet_naming_rule.private_or_internal_field_should_be_underscore_and_pascal_case.style = underscore_and_pascal_case

dotnet_naming_rule.public_or_protected_field_should_be_pascal_case.severity = warning
dotnet_naming_rule.public_or_protected_field_should_be_pascal_case.symbols = public_or_protected_field
dotnet_naming_rule.public_or_protected_field_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.class_should_be_pascal_case.severity = warning
dotnet_naming_rule.class_should_be_pascal_case.symbols = class
dotnet_naming_rule.class_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.static_field_should_be_all_upper.severity = warning
dotnet_naming_rule.static_field_should_be_all_upper.symbols = static_field
dotnet_naming_rule.static_field_should_be_all_upper.style = all_upper

dotnet_naming_rule.namespace_should_be_pascal_case.severity = warning
dotnet_naming_rule.namespace_should_be_pascal_case.symbols = namespace
dotnet_naming_rule.namespace_should_be_pascal_case.style = pascal_case

# 記号の仕様

dotnet_naming_symbols.class.applicable_kinds = class
dotnet_naming_symbols.class.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.class.required_modifiers = 

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers = 

dotnet_naming_symbols.method.applicable_kinds = method
dotnet_naming_symbols.method.applicable_accessibilities = public
dotnet_naming_symbols.method.required_modifiers = 

dotnet_naming_symbols.public_or_protected_field.applicable_kinds = field
dotnet_naming_symbols.public_or_protected_field.applicable_accessibilities = public, protected
dotnet_naming_symbols.public_or_protected_field.required_modifiers = 

dotnet_naming_symbols.static_field.applicable_kinds = field
dotnet_naming_symbols.static_field.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.static_field.required_modifiers = static

dotnet_naming_symbols.private_or_internal_field.applicable_kinds = field
dotnet_naming_symbols.private_or_internal_field.applicable_accessibilities = internal, private, private_protected
dotnet_naming_symbols.private_or_internal_field.required_modifiers = 

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers = 

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers = 

dotnet_naming_symbols.namespace.applicable_kinds = namespace
dotnet_naming_symbols.namespace.applicable_accessibilities = *
dotnet_naming_symbols.namespace.required_modifiers = 

# 命名スタイル

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

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix = 
dotnet_naming_style.begins_with_i.word_separator = 
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.underscore_and_pascal_case.required_prefix = _
dotnet_naming_style.underscore_and_pascal_case.required_suffix = 
dotnet_naming_style.underscore_and_pascal_case.word_separator = 
dotnet_naming_style.underscore_and_pascal_case.capitalization = pascal_case

dotnet_naming_style.all_upper.required_prefix = 
dotnet_naming_style.all_upper.required_suffix = 
dotnet_naming_style.all_upper.word_separator = _
dotnet_naming_style.all_upper.capitalization = all_upper

privateフィールドは先頭アンダースコア,以降パスカル.publicフィールドは先頭パスカルみたいな感じ.
定数(static)とかはアッパースネーク.インタフェースには先頭Iをつける.というのが主なルールとした.

.editorconfig ファイルの設置場所

editorconfigの影響範囲については,以下記事の「editorconfigの影響範囲」を参照のこと.

www.neputa-note.net

.editorconfigファイルはAssetsフォルダに設定してもよいが,プロジェクトによって,
Assetsフォルダ内に外部パッケージのフォルダが置いてある場合,そのスクリプトも監査範囲になってしまう.
よって,個人的にはプロジェクト独自のフォルダ内に設置するのがよい.

.editorconfig ファイルの作り方

Visual StudioGUIに沿って作成するのがよい.
「ツール」>「オプション」>「テキストエディタ」>「C#」>「コードスタイル」で色々設定箇所がある.
各々プロジェクトごとにコード規約があると思うので,それに沿って設定しよう.

f:id:xrdnk:20201201185758p:plain

名前指定も以下のようにカスタマイズできる.

f:id:xrdnk:20201201185826p:plain

設定が終わったら「全般」の「設定から.editorconfigファイルを生成」で完了.

Unity C# 用の独自設定

上の私が作成した.editorconfigファイル例でこんな記述がある.

# C# ファイル
[*.cs]
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.IDE0044.severity = none
dotnet_diagnostic.RCS1213.severity = none
dotnet_diagnostic.RCS1169.severity = none

これらはUnity C#用に設定した設定.これがないので余計なところに波線が入り,邪魔だったので記述した.

参考.
github.com

EditorConfig の効果を見てみよう

privateフィールドについてみてみる.

先頭をアンダースコアにしなかった場合.
f:id:xrdnk:20201201190754p:plain

アンダースコアいれてくれ~~って感じのメッセージが出る.
今回満たさない場合はwarning波線が出るように設定している.ビルドも成功しなくなる.

アンダースコア入れているが,パスカルにしなかった場合.
f:id:xrdnk:20201201190939p:plain

こんな感じのメッセージが出る.

よし,じゃあちゃんとしよう.
f:id:xrdnk:20201201190958p:plain

やっと波線がなくなった.やったぜ.めでたし.めでたし.