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

相关文章

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J