In this post I show how to install the experimental IntelliCode extension in Visual Studio, and how to use it to generate an EditorConfig file from your existing code.
I was catching up on some Merge Conflict episodes recently. In an episode back in September James and Frank discussed using an EditorConfig file to codify your C# style in a way that can be understood across multiple IDEs.
EditorConfig has been supported in IDEs for years, but it's only relatively recently that Visual Studio introduced support for C#/.NET syntax formatting conventions. This means you can enforce a variety of basic formatting styles, such as naming conventions, brace position, spacing, etc.
Keeping a consistent style throughout a codebase is important for readability, but can be hard to achieve without some sort of automated tooling to help you.
Having said that, pretty much none of my projects have an .editorconfig file.
Every time I've tried to add one to an existing project, I get bogged down trying to decide what value I want to use for each of the rules. And there's a lot of them.
That's why I was very interested when James mentioned on Merge Conflict that there's an experimental Visual Studio extension that can generate this file for you!
Visual Studio IntelliCode was announced some time ago, but it passed me by. According to their website:
Visual Studio IntelliCode is an experimental set of AI-assisted development capabilities for next-generation developer productivity.
That's a pretty bold claim, and there's a lot in the works, but right now it basically has two features:
- Assisted IntelliSense - which tries to guess which item you're most likely to pick from the IntelliSense completion list, and puts it at the top. I'm pretty sure ReSharper has done this for years, but you know… AI…
- Infer code style and formatting conventions - generate an .editorconfig file from the code in your solution. This is what I came for!
I decided to give the extension a go. I run Visual Studio in various places both with and without ReSharper, so I'll try out the enhanced IntelliSense in good time. In this post, I'll show how you can generate an .editorconfig file for your project.
Even if you don't normally use Visual Studio, it might be worth installing the extension, generating the .editorconfig file, and committing it to your source code repository. You can then go back to using your preferred IDE, but with the benefit of an auto-generated .editorconfig.
You can install the extension from Visual Studio by choosing Tools > Extensions and Updates. Search in the Online section for IntelliCode and download it:
You'll have to close Visual Studio to complete the install. When you do, the VSIX installer window should pop up:
When you start up Visual Studio, you should see little stars next to your IntelliSense lists - that's the IntelliCode extension at work:
Now the extension is installed, you can generate an .editorconfig file.
You can generate an .editorconfig file anywhere in your solution by right-clicking in Solution Explorer and choosing Add > New EditorConfig (IntelliCode):
IntelliCode will pause for a while while it analyses your solution, before presenting the generated .editorconfig. I tried it on one of my GitHub projects and it generated the file below:
# Rules in this file were initially inferred by Visual Studio IntelliCode from the NetEscapades.AspNetCore.SecurityHeaders codebase based on best match to current usage at 16/11/2018 # You can modify the rules from these initially generated values to suit your own policies # You can learn more about editorconfig here: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference [*.cs] #Core editorconfig formatting - indentation #use soft tabs (spaces) for indentation indent_style = space #Formatting - indentation options #indent switch case contents. csharp_indent_case_contents = true #indent switch labels csharp_indent_switch_labels = true #Formatting - new line options #require braces to be on a new line for types, object_collection, methods, control_blocks, and lambdas (also known as "Allman" style) csharp_new_line_before_open_brace = types, object_collection, methods, control_blocks, lambdas #Formatting - organize using options #sort System.* using directives alphabetically, and place them before other usings dotnet_sort_system_directives_first = true ... #Style - qualification options #prefer fields not to be prefaced with this. or Me. in Visual Basic dotnet_style_qualification_for_field = false:suggestion #prefer methods not to be prefaced with this. or Me. in Visual Basic dotnet_style_qualification_for_method = false:suggestion #prefer properties not to be prefaced with this. or Me. in Visual Basic dotnet_style_qualification_for_property = false:suggestion
One of the really nice things about this file is the annotations describing what the fields do. Some of the names can be pretty obscure. Unfortunately if want to add any extra rules, you're left having to trawl through the documentation to find what you want.
To help tweak the .editorconfig I decided to install one more extension, the EditorConfig Language Service.
The EditorConfig Language Service is a Visual Studio extension by the prolific Mads Kristensen. It puts your Visual Studio editor on steroids for .editorconfig files! Install the extension from Tools > Extensions and Updates, restart Visual Studio again, and you'll be good to go.
The key feature I was after is IntelliSense for .editorconfig. Now when you start typing you'll get the classic Visual Studio completion list, with all the options available, as well as a clear description of what it does.
It includes a bunch of other nice-to-haves like auto-formatting your code when you type
Ctrl+K,D to line up all the
csharp_space_between_method_declaration_parameter_list_parentheses = false csharp_preserve_single_line_blocks = true : warning dotnet_style_qualification_for_field = false : suggestion dotnet_style_qualification_for_method = false : suggestion dotnet_style_qualification_for_property = false : suggestion
Overall, definitely worth installing, even if you only use it once!
I think I might try generating .editorconfig files for a bunch of my different projects, and see how they compare. Anything that takes the hassle out of having to create one manually, or making a million and one tiny decisions!
In this post I showed how to install the experimental IntelliCode extension in Visual Studio. This extension is able to automatically generate an EditorConfig file from your project's code, so that the rules reflect the styles you're already using. If it works for your code base, then this seems like a great way to set a baseline for an existing code base, that you can then migrate gradually towards a more consistent style.