rksoftware

Visual Studio とか C# とかが好きです

新しい EditorConfig (IntelliCode)

Visual Studio 2019 で IntelliCode という機能が非常に注目されています。このブログでも何度か記事を書きました。

そんな IntelliCode にあまり注目されていない(ような気がする)「新しい EditorConfig (IntelliCode)」という機能があります。

■ EditorConfig

EditorConfig は様々な IDE で統一して使えるコードスタイルを定義する config で、Visual Studio も対応しています。

■ 新しい EditorConfig (IntelliCode)

IntelliCode によって、この config をいい感じに作ってくれるというのが「新しい EditorConfig (IntelliCode)」です。

手順

  • Visual Studio で設定を作りたいソリューションを開く
  • ソリューションを右クリックして 追加 > 新しい EditorConfig (IntelliCode)
    f:id:rksoftware:20190425202058j:plain

これだけで .editorconfig ファイルが作られます。

■ 作られる .editorconfig ファイル

新規に作ったコンソールアプリプロジェクトで作成したら、次の様な .editorconfig ファイルが作られました。(コメントは削除しています)

[*.cs]
indent_style = space
csharp_style_expression_bodied_methods = false:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion

コードを変更して作成

.editorconfig ファイルを削除し、コードを変更してもう一度作成してみます。
コードは次のように変更しました。
変更前

using System;

namespace IntelliCode_ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

変更後

using System;

namespace IntelliCode_ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

作成された .editorconfig ファイル

[*.cs]
indent_style = tab
csharp_style_expression_bodied_methods = false:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion

ソースコードのインデントをスペースからタブに変えて再作成したところ、期待通りインデントをタブで行う設定を作成してくれました。
素晴らしい。