有没有办法强制使用制表符而不是空格?

StyleCop提供检查空间的一致使用,但遗憾的是缺乏相反的想法:强制源代码使用选项卡。 有没有办法添加此function? 它不一定是StyleCop,也欢迎其他工具。

您可以使用StyleCop +插件来强制使用选项卡。

下载StyleCopPlus.dll将其放在主StyleCop文件夹C:\Program Files (x86)\StyleCop 4.7\Custom Rules文件夹中,或直接放在主文件夹中。

现在,当使用StyleCopSettingsEditor打开Settings.StyleCop ,您将能够设置规则SP2001: CheckAllowedIndentationCharacters

此规则可在StyleCop+选项卡下的“ More Custom Rules子选项卡下的“ Formatting标题下找到:

规则选项

虽然有很多理由可以使用其中任何一个,但我还有其他地方可以理解为什么你认为一个比另一个好。 🙂

我实际上想要相同的东西 – 检查制表符缩进的规则 – 所以我根据StyleCop的SpacingRules源编写它。 它似乎工作得相当好,虽然到目前为止我只在一些项目中使用它。 它可能是优化的或任何……但它的工作原理。

 using System; using System.Text.RegularExpressions; using Microsoft.StyleCop; using Microsoft.StyleCop.CSharp; namespace CustomRules.StyleCop.CSharp { [SourceAnalyzer(typeof(CsParser))] public class SpacingRules : SourceAnalyzer { public SpacingRules() { } public override void AnalyzeDocument(CodeDocument document) { Param.RequireNotNull(document, "document"); CsDocument csdocument = (CsDocument)document; if (csdocument.RootElement != null && !csdocument.RootElement.Generated) { this.CheckSpacing(csdocument.Tokens); } } private void CheckSpacing(MasterList tokens) { Param.AssertNotNull(tokens, "tokens"); foreach (var token in tokens) { if (this.Cancel) { break; } if (token.Generated) { continue; } switch (token.CsTokenType) { case CsTokenType.WhiteSpace: this.CheckWhitespace(token as Whitespace); break; case CsTokenType.XmlHeader: XmlHeader header = (XmlHeader)token; foreach (var xmlChild in header.ChildTokens) { this.CheckTabsInComment(xmlChild); } break; case CsTokenType.SingleLineComment: case CsTokenType.MultiLineComment: this.CheckTabsInComment(token); break; } switch (token.CsTokenClass) { case CsTokenClass.ConstructorConstraint: this.CheckSpacing(((ConstructorConstraint)token).ChildTokens); break; case CsTokenClass.GenericType: this.CheckGenericSpacing((GenericType)token); this.CheckSpacing(((TypeToken)token).ChildTokens); break; case CsTokenClass.Type: this.CheckSpacing(((TypeToken)token).ChildTokens); break; } } } private void CheckGenericSpacing(GenericType generic) { Param.AssertNotNull(generic, "generic"); if (generic.ChildTokens.Count == 0) { return; } foreach (var token in generic.ChildTokens) { if (this.Cancel) { break; } if (token.CsTokenClass == CsTokenClass.GenericType) { this.CheckGenericSpacing(token as GenericType); } if (!token.Generated && token.CsTokenType == CsTokenType.WhiteSpace) { this.CheckWhitespace(token as Whitespace); } } } private void CheckWhitespace(Whitespace whitespace) { Param.AssertNotNull(whitespace, "whitespace"); if (whitespace.Location.StartPoint.IndexOnLine == 0 && Regex.IsMatch(whitespace.Text, "^ +")) { this.AddViolation(whitespace.FindParentElement(), whitespace.LineNumber, "TabsMustBeUsed"); } } private void CheckTabsInComment(CsToken comment) { Param.AssertNotNull(comment, "comment"); var lines = comment.Text.Split('\n'); for (int i = 0; i < lines.Length; i++) { if (Regex.IsMatch(lines[i], "^ +")) { this.AddViolation(comment.FindParentElement(), comment.LineNumber + i, "TabsMustBeUsed"); } } } } } 

请注意,您还必须在程序集中包含嵌入式XML文件“SpacingRules.xml”。 (有关详细信息,请阅读StyleCop SDK文档。)

    Rules which verify the spacing placed between keywords and symbols in the code.    Spaces are not allowed. Use tabs instead. Verifies that the code does not contain spaces.    

假设您使用Visual Studio作为IDE,并且您的团队成员支持这个想法,那么您可以做的一件事就是将VS设置为使用制表符而不是空格,导出和共享设置文件。

可以在工具>选项>文本编辑器>所有语言(或您要使用的语言)>选项卡下找到该设置,然后在右侧可以选择“插入空格”或“保留选项卡”。

从Visual Studio导出设置:工具>导入和导出设置>导出所选环境设置>选择’选项’

只是一个想法 – 但说实话,真正的问题似乎是你的队友的支持。 否则,他们总是可以恢复到他们的设置。 或者,在办理登机手续时,正如Sam建议的那样,您可以进行一些自动重新格式化。

HTH

StyleCop支持创建自定义规则,因此您可以添加自己的“使用前导选项卡而不是空格”规则。 如果您不想尝试开发自己的规则,可以在http://stylecopcontrib.codeplex.com/或http://github.com/AArnott/nerdbank.stylecop.rules上获取现有规则。

好的,我设法找出了问题,因为出现此警告的原因,看起来是因为开发人员有时会复制并粘贴代码

如果您正在使用VS2010转到解决方案资源管理器检入设置样式警察然后修改样式警察内部的设置例如禁用或取消选中设置[间距…..]

在源代码管理服务器中执行此操作。 使用预提交脚本检查文件以查找以多个空格开头的行并阻止提交。

我同意标签比空格更好。 这是个人偏好,但团队的一致性非常重要。

包方式:

目前的趋势是通过nuget包来实现这一目标(并且经典的StyleCop可能会在某些时候逐步淘汰)。 因此,要对包执行此操作,请执行以下操作:

通过nuget:

 Install-Package Microsoft.CodeAnalysis.FxCopAnalyzers Install-Package StyleCop.Analyzers -Version 1.1.0-beta006 

请注意对pre-releaset的引用(此时),选项卡的设置仅在测试版中可用。

将以下代码添加到您的项目中作为ca.ruleset:

                                                                              

通过编辑.csproj文件并添加以下内容将其添加到项目文件中:

  ca.ruleset  

要覆盖选项卡(和其他)设置,您需要将stylecop.json文件添加到项目中。 在文件属性中,将“构建操作”设置为“(分析器)附加文件”。 根据项目类型,可能不会出现实际的“分析器”字样。

将stylecop.json文件编辑为如下所示:

 { "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", "settings": { "documentationRules": { "companyName": "YourCompanyName", "copyrightText": "Copyright (c) {companyName}. All Rights Reserved.\r\nLicensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.", "xmlHeader": false, "fileNamingConvention": "metadata" }, "indentation": { "useTabs": true } } } 

至少对于.NET标准项目,您需要确保以下内容位于csproj文件中(并且没有其他文件引用):

    

您可能必须重新加载项目和包以使它们识别stylecop.json文件。

参考文献:

DotNetAnalyzers / StyleCopAnalyzers

.NET核心,代码分析和StyleCop