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

本文主要是介绍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

这篇关于Tabs vs Spaces-在Visual Studio中使用EditorConfig进行和平解决。 加上.NET扩展!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/227421

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本