0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C# / Unity】Microsoft公式準拠 .editorconfigでコーディング規約を自動化する設定

0
Posted at

UnityなどC#を使用してアプリケーションを開発する際、インデントや改行コードといった成形をプロジェクトごとに設定する場合があると思います。

そういった際.editorconfigを使用するのが正攻法かと思います。

本記事では、最も多いであろうMicrosoftの公式に準拠した.editorconfigのテンプレートを紹介したいと思います。

前提知識

  • .editorconfigを知っている
  • Microsoftの公式規約を知っている

まだご存知でない方は、下記の記事を通読することを推奨します。

Microsoft公式準拠 .editorconfig テンプレート

.editorconfig
root = true

[*.cs]

# -----------------------------------------------------------------------------
# スタイル / レイアウト
# -----------------------------------------------------------------------------

# インデントにはタブ文字を使用せず4つのスペースを使用
indent_style = space
indent_size = 4
tab_width = 4

# 中かっこはAllmanスタイル
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

# 1つの行には1つのステートメントのみを記述
csharp_preserve_single_line_statements = false

# 必要に応じて、2項演算子の前に改行を行う必要がある
dotnet_style_operator_placement_when_wrapping = beginning_of_line

# 式内の句を明確にするために、かっこを使用
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:suggestion
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:suggestion
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:suggestion

# -----------------------------------------------------------------------------
# 型指定
# -----------------------------------------------------------------------------

# ランタイム型ではなく、データ型には言語キーワードを使用
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# varは、代入の右側から型が明らかな場合にのみ使用
csharp_style_var_for_built_in_types = false:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = false:suggestion

# -----------------------------------------------------------------------------
# 初期化 (new / オブジェクト初期化子 / コレクション)
# -----------------------------------------------------------------------------

# 変数型がオブジェクト型と一致する場合は、オブジェクトインスタンス化の簡潔な形式のいずれかを使用する。
# インターフェイスや基底クラス型の変数には new()は使えない。 
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion

# オブジェクト初期化子を使用してオブジェクトの作成を簡略化
dotnet_style_object_initializer = true:suggestion

# コレクション初期化の簡略化
dotnet_style_collection_initializer = true:suggestion

# -----------------------------------------------------------------------------
# using / 名前空間
# -----------------------------------------------------------------------------

# 名前空間宣言の外側に usingディレクティブを配置する
csharp_using_directive_placement = outside_namespace:warning

# try-finallyのfinallyがDispose呼び出しだけならusingステートメントを使う
csharp_prefer_simple_using_statement = true:suggestion

# -----------------------------------------------------------------------------
# 例外処理
# -----------------------------------------------------------------------------

# 適切に処理できる例外のみをキャッチする
dotnet_diagnostic.CA1031.severity = suggestion

# 再スローに `throw;` を使用
dotnet_diagnostic.CA2200.severity = warning

# -----------------------------------------------------------------------------
# 静的メンバー
# -----------------------------------------------------------------------------

# 静的メンバーは定義したクラス名で呼び出す
dotnet_diagnostic.IDE0002.severity = suggestion

# -----------------------------------------------------------------------------
# 【C# 10 以降のみ】Unity (C# 9) では有効にしないこと
# -----------------------------------------------------------------------------
# ほとんどのコードファイルでは名前空間は1つ
# ファイルスコープの名前空間宣言 (namespace MyGame.Characters;) を使用する
# csharp_style_namespace_declarations = file_scoped:warning

# コレクション式を使用して、すべてのコレクション型を初期化する
# dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion

# -----------------------------------------------------------------------------
# 【Unity 固有の補足】実用上の調整。Unity以外では削除してよい。
# -----------------------------------------------------------------------------

# Start/Update等のUnityメッセージや、リフレクション / シリアライズ経由で
# 使われるprivateメンバーを「未使用」と誤判定するため抑制。
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.IDE0052.severity = none

# [SerializeField] のprivateフィールドは Inspectorから書き換えられるため
# readonly 化の提案が不適切。
dotnet_diagnostic.IDE0044.severity = none

# UnityEngine.Objectは == を独自実装 (破棄済みオブジェクトを null 扱い) している。
# `?.` `??` `??=` はこれを迂回するため、null 伝播 / 合体演算子への書き換え提案を抑制。
dotnet_diagnostic.IDE0031.severity = none
dotnet_diagnostic.IDE0029.severity = none
dotnet_diagnostic.IDE0030.severity = none
dotnet_diagnostic.IDE0270.severity = none
dotnet_diagnostic.IDE0074.severity = none

# -----------------------------------------------------------------------------
# 命名規則 C# 識別子の名前付け規則と表記規則
# -----------------------------------------------------------------------------
# 慣例として、型名・名前空間・すべてのパブリック メンバーに PascalCase。
# インターフェイス名は大文字 I で始める。

dotnet_naming_style.pascal_case_style.capitalization = pascal_case

dotnet_naming_style.begins_with_i_style.capitalization = pascal_case
dotnet_naming_style.begins_with_i_style.required_prefix = I

dotnet_naming_symbols.interface_symbols.applicable_kinds = interface

dotnet_naming_symbols.type_symbols.applicable_kinds = class, struct, enum, delegate

dotnet_naming_symbols.public_member_symbols.applicable_kinds = property, method, field, event
dotnet_naming_symbols.public_member_symbols.applicable_accessibilities = public, internal, protected, protected_internal

# 具体的なルール
dotnet_naming_rule.interfaces_begin_with_i.severity = suggestion
dotnet_naming_rule.interfaces_begin_with_i.symbols = interface_symbols
dotnet_naming_rule.interfaces_begin_with_i.style = begins_with_i_style

dotnet_naming_rule.types_are_pascal_case.severity = suggestion
dotnet_naming_rule.types_are_pascal_case.symbols = type_symbols
dotnet_naming_rule.types_are_pascal_case.style = pascal_case_style

dotnet_naming_rule.public_members_are_pascal_case.severity = suggestion
dotnet_naming_rule.public_members_are_pascal_case.symbols = public_member_symbols
dotnet_naming_rule.public_members_are_pascal_case.style = pascal_case_style

総括

本記事では、Unityでの開発に焦点を当ててMicrosoft公式準拠の.editorconfig構成について紹介しました。

よろしければ、是非ご自身のプロジェクトなどにご活用ください。

0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?