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中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左