Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import an EditorConfig file into Visual Studio for all solutions?

How do I import code conventions (code formatting settings) from an EditorConfig file into Visual Studio?

I would like to have one .editorconfig file for all solutions and not one per project.

like image 672
Dmitry Avatar asked Oct 30 '19 13:10

Dmitry


People also ask

Does VSCode read EditorConfig?

In VSCode you need to install an add-in called EditorConfig. This doesn't enable the . editorconfig even if it is telling you about it. Maybe it actually does, but it doesn't work with C# because Omnisharp does something.

What is an EditorConfig file?

The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.

How do I enforce coding standards in Visual Studio?

Use an EditorConfig file when you want to enforce coding styles for all contributors to your repo or project. You can manually populate your EditorConfig file, or you can automatically generate the file based on the code style settings you've chosen in the Visual Studio Options dialog box.


1 Answers

EditorConfig is supported in Visual Studio 2017 and newer.

Creating an EditorConfig file

If you like you can create an empty text file and just add the settings you want. However, Visual Studio can create a .editorconfig file with sensible defaults for .NET for you. From MSDN:

  • From the menu bar, choose Project > Add New Item; or press Ctrl+Shift+A

  • Select the editorconfig File (.NET) template to add an EditorConfig file prepopulated with default .NET code style, formatting, and naming conventions

enter image description here

Visual Studio 2019 - Creating an EditorConfig file from current settings

In Visual Studio 2019, you can instead create an EditorConfig file from your current settings. Just click the following button in the Options dialog under Text Editor > C# > Code Style > General:

enter image description here

Sharing an EditorConfig across solutions

To share settings across solutions and projects, you need to create a .editorconfig file in a top-level directory/solutions folder. For example, I have mine in trunk and I commit it to source control so that my colleagues share the same settings.

The settings in this file apply to all projects in trunk and subfolders, unless overridden by another .editorconfig file further down the folder tree e.g. you might you have a project specific EditorConfig file in a subfolder which has different settings. You don't need to "import" the file - it should be detected automatically.

See File hierarchy and precedence for more details.

If you're using Visual Studio 2019, I suggest you use the Generate .editorconfig file from settings button detailed above, and save the .editorconfig file to your solutions folder. Otherwise, you can create a throwaway project, create the config file, and then cut and paste that file to the folder where it needs to live.

like image 159
Stephen Kennedy Avatar answered Oct 11 '22 13:10

Stephen Kennedy