Tabs vs Spaces-在Visual Studio中使用EditorConfig进行和平解决。 加上.NET扩展!

The culture wars continue. The country is divided with no end in sight. Tabs or spaces? There's even an insane (IMHO) assertion that the spaces people make more money.

文化大战仍在继续。 这个国家分裂了,没有尽头。 制表符或空格? 甚至有人疯狂地宣称,太空人会赚更多的钱

I'm going with Gina Trapani on this one. I choose working code.

我要和吉娜·特拉帕尼(Gina Trapani)合作。 我选择工作代码。

@ftrain I choose the 3rd option: working code

— Gina Trapani ️‍ (@ginatrapani)

@ftrain我选择第三个选项:工作代码

—吉娜·特拉帕尼(@ginatrapani) December 27, 2015 2015年12月27日

Teams can fight but the problem of formatting code across teams is solved by EditorConfig. I'm surprised more people don't know about it and use it, so this blog post is my small way of getting the word out. TELL THE PEOPLE.

团队可以战斗,但是EditorConfig解决了跨团队格式化代码的问题。 我感到惊讶的是,更多的人不知道并使用它,所以这篇博客文章是我传达信息的一种小方法。 告诉人们。

Take a project and make a new .editorconfig file and put this in it. I'll use a dotnet new console example hello world app.

进行一个项目,制作一个新的.editorconfig文件,并将其放入其中。 我将使用一个新的dotnet控制台示例hello world应用程序。

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

I've set mine in this example to just *.cs, but you could also say [*.{cs,js}] or just [*] if you like, as well as have multiple sections.

在本示例中,我将我的名称设置为* .cs,但是如果您愿意,也可以说[*。{cs,js}]或[*],还有多个部分。

You'll check this file in WITH your project so that everyone on the team shares the team's values.

您将在项目中检查此文件,以便团队中的每个人都共享团队的价值观。

Here in Notepad2 we can see someone has used spaces for whitespace, like a savage. Whitespace appears as pale dots in this editor.

在Notepad2中,我们可以看到有人使用空格作为野蛮人。 在此编辑器中,空白显示为浅点。

image

I'll open this project in Visual Studio 2017 which supports the EditorConfig file natively. Notice the warning at the bottom where VS lets me know that this project has conventions that are different than my own.

我将在Visual Studio 2017中打开此项目,该项目本机支持EditorConfig文件。 注意底部的警告,VS让我知道此项目的约定与我的约定不同。

user preferences for this file type are overwidden by this project's coding conventions

VS Format Document commands will use tabs rather than spaces for this project. Here is the same doc reformatted in VS:

VS格式文档命令将为此项目使用制表符而不是空格。 这是在VS中重新格式化的同一文档:

image

At this point I'm comforted that the spaces have been defeated and that cooler heads have prevailed - at least for this project.

在这一点上,我感到很安慰的是,这些空间已经被打败,而且冷静的头脑占了上风-至少对于这个项目而言。

.NET对EditorConfig的扩展 (.NET Extensions to EditorConfig)

Even better, if your editor supports it, you can include "EditorConfig Extensions" for specific files or languages. This way your team can keep things consistent across projects. If you're familiar with FxCop and StyleCop, this is like those.

更好的是,如果您的编辑器支持它,则可以包括用于特定文件或语言的“ EditorConfig扩展名”。 这样,您的团队就可以使整个项目保持一致。 如果您熟悉FxCop和StyleCop,则与此类似。

There's a ton of great .NET EditorConfig options you can set to ensure the team uses consistent Language Conventions, Naming Conventions, and Formatting Rules.

您可以设置大量的.NET EditorConfig选项,以确保团队使用一致的语言约定,命名约定和格式设置规则。

  • Language Conventions are rules pertaining to the C# or Visual Basic language, for example, var/explicit type, use expression-bodied member.

    语言约定是与C#或Visual Basic语言相关的规则,例如var / explicit类型,使用表达式主体成员。

  • Formatting Rules are rules regarding the layout and structure of your code in order to make it easier to read, for example, Allman braces, spaces in control blocks.

    格式规则是与代码的布局和结构有关的规则,以便于阅读,例如Allman大括号,控制块中的空格。

  • Naming Conventions are rules respecting the way objects are named, for example, async methods must end in "Async".

    命名约定是关于对象命名方式的规则,例如, async方法必须以“异步”结尾。

You can also set the importance of these rules with things like "suggestion," or "warning," or even "error."

您还可以通过“建议”或“警告”甚至“错误”之类的东西来设置这些规则的重要性。

As an example, I'll set that my team wants predefined types for locals:

例如,我将设置团队要为本地人预定义类型:

dotnet_style_predefined_type_for_locals_parameters_members = true:error

dotnet_style_predefined_type_for_locals_parameters_members = true:error

Visual Studio here puts up a lightbulb and the suggested fix because my team would rather I use "string" than the full "System.String.

Visual Studio在这里放了一个灯泡和建议的修复程序,因为我的团队宁愿我使用“ string”而不是完整的“ System.String”。

Visual Studio respects EditorConfig

The excellent editorconfig for .NET docs have a LOT of great options you can use or ignore. Here's just a FEW (controversial) examples:

出色的.NET文档编辑器,有很多不错的选择,您可以使用或忽略。 这只是一个有争议的例子:

  • csharp_new_line_before_open_brace - Do we put open braces at the end of a line, or on their own new line?

    csharp_new_line_before_open_brace-我们是在行的末尾还是在他们自己的新行上放开括号?
  • csharp_new_line_before_members_in_object_initializers - Do we allow A = 3, B = 4, for insist on a new line for each?

    csharp_new_line_before_members_in_object_initializers-我们是否允许A = 3,B = 4,以便每个都坚持换一行?
  • csharp_indent_case_contents - Do we freakishly line up all our switch/case statements, or do we indent each case like the creator intended?

    csharp_indent_case_contents-我们是异常地将所有switch / case语句排列在一起,还是像创建者所希望的那样缩进每种情况?
  • You can even decide on how you Want To Case Things And Oddly Do Sentence Case: pascal_case, camel_case, first_word_upper, all_upper, all_lower

    您甚至可以决定如何对事物进行大小写和奇怪的句子大小写: pascal_casecamel_casefirst_word_upperall_upperall_lower

If you're using Visual Studios 2010, 2012, 2013, or 2015, fear not. There's at least a basic EditorConfig free extension for you that enforces the basic rules. There is also an extension for Visual Studio Code to support EditorConfig files that takes just seconds to install although I don't see a C# one for now, just one for whitespace.

如果您使用的是Visual Studios 2010、2012、2013或2015,请不要担心。 至少有一个基本的EditorConfig free扩展可以为您实施基本规则。 Visual Studio Code还有一个扩展以支持EditorConfig文件,扩展仅需几秒钟即可安装,尽管我目前看不到C#,空白也只有一个。

Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test and debug ASP.NET, .NET Framework, .NET Core, Xamarin or Unity applications. Learn more and download a 30-day trial!

赞助商:查看JetBrains Rider :一个新的跨平台.NET IDE 。 编辑,重构,测试和调试ASP.NET,.NET Framework,.NET Core,Xamarin或Unity应用程序。 了解更多信息并下载30天试用版

翻译自: https://www.hanselman.com/blog/tabs-vs-spaces-a-peaceful-resolution-with-editorconfig-in-visual-studio-plus-net-extensions

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值