📜 ⬆️ ⬇️

Tabs or spaces: solve with Visual Studio

Hi, Habr! Cultural warriors continue, people fight on opposite sides of the barricades, trying to decide: tabs or spaces. On the same topic, we found an interesting article by Scott Hanselman in which he talks about a tool that solves this dispute, EditorConfig in Visual Studio. I ask all interested under the cat.



Remember, in the summer at StackOverflow there was an article stating that people earn more in spaces.


')
We will understand this together with Gina Trapani. Find the working code.

Developers can break spears further, but the problem of code formatting is solved with the help of the EditorConfig file. Surprisingly, many people use it without knowing it, so this publication will be just a little hint. Tell your colleagues about it.

Open the project, create a new .editorconfig file and paste the following code into it. I will use the “Hello, world!” Program in the new .NET console.

[*.cs] indent_style = tab indent_size = tab tab_size = 4 

In my example, only the * .cs extension is indicated, but you can also specify [*.{cs,js}] or, if you like, only [*], or multiple sections at once.

Make sure that the file is entered into the system along with the project so that each team member can appreciate all its benefits.

You can see in Notepad2 if someone, like in the Stone Age, uses spaces as a separator. Spaces in this editor appear as pale dots.



I open this project in Visual Studio 2017 with built-in support for EditorConfig. Pay attention to the Visual Studio warning below that there are designations in the project that do not coincide with ours.

Visual Studio commands for formatting documents in this project will use tabs, not spaces. Here is the same document reformatted in Visual Studio:



Now I can sleep peacefully: the gaps are defeated, sober minds triumphed (at least within the framework of this project).

.NET extensions for EditorConfig


Moreover, if the used editor allows, you can add EditorConfig extensions for specific files or languages. This will ensure consistency in the work of your staff on the project. If you are familiar with FxCop and StyleCop, then this is about the same.

You can use a lot of convenient .NET EditorConfig options . Thanks to them, team members will apply the same locales, naming conventions and formatting rules.


You can also set the severity of rules using options such as suggestion, warning, or even error.

For example, let's make my employees use predefined types for local objects:

dotnet_style_predefined_type_for_locals_parameters_members = true:error

In Visual Studio, the lightbulb icon with the proposed correction will appear in the corresponding line, because, most likely, my employees will print just “string” instead of the full form “System.String”.



EditorConfig for .NET Docs has a LOT of excellent settings that can be used or ignored. Here are just a few (ambiguous) examples:


If you work with Visual Studios 2010, 2012, 2013 or 2015, then you can be sure. At a minimum, there is a basic free EditorConfig extension , with which you can apply basic rules. There is also one extension for working with EditorConfig files in Visual Studio Code, and you can install it in seconds. But there is no extension for C # yet - there is only one for converting spaces.



By the way, in the summer we conducted a vote on this topic in Microsoft Developer. Then beat the Tabs. We offer to repeat here.

Source: https://habr.com/ru/post/341722/


All Articles