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

相关文章

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca