StyleCop提供了检查空格的一致使用,但遗憾的是缺少相反的想法:强制源代码使用制表符。是否有一些方法可以添加此功能?它不必是StyleCop,其他工具也可以。
注:保留了html标签,没有进行解释
StyleCop提供了检查空格的一致使用,但遗憾的是缺少相反的想法:强制源代码使用制表符。是否有一些方法可以添加此功能?它不必是StyleCop,其他工具也可以。
注:保留了html标签,没有进行解释
我也是一个喜欢用制表符而非空格的人,虽然有很多理由可以使用任何一种方式,也有其他的地方可以讨论为什么认为这种方式更好。:)
实际上,我也想要同样的东西-一条规则来检查制表符缩进-所以我基于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<CsToken> 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文档以获取更多信息.)
<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Custom Spacing Rules">
<Description>
Rules which verify the spacing placed between keywords and symbols in the code.
</Description>
<Rules>
<Rule Name="TabsMustBeUsed" CheckId="MY1027">
<Context>Spaces are not allowed. Use tabs instead.</Context>
<Description>Verifies that the code does not contain spaces.</Description>
</Rule>
</Rules>
</SourceAnalyzer>
你可以使用StyleCop+插件来强制使用制表符。
下载 StyleCopPlus.dll
后,将其放置在主 StyleCop 文件夹内的 Custom Rules
文件夹中:C:\Program Files (x86)\StyleCop 4.7\Custom Rules
,或直接放在主文件夹中。
现在,在使用 StyleCopSettingsEditor
打开 Settings.StyleCop
时,您将能够设置规则 SP2001: CheckAllowedIndentationCharacters
。
此规则可在 StyleCop+
选项卡下的 More Custom Rules
子选项卡下的 Formatting
标题下找到:
包装方式:
目前的趋势似乎是通过NuGet包来实现这一点(经典的StyleCop可能会在某个时候逐渐被淘汰)。因此,要使用这些包,请按照以下步骤操作:
通过NuGet:
Install-Package Microsoft.CodeAnalysis.FxCopAnalyzers
Install-Package StyleCop.Analyzers -Version 1.1.0-beta006
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Custom Rulset" Description="Custom Rulset" ToolsVersion="14.0">
<Rules AnalyzerId="AsyncUsageAnalyzers" RuleNamespace="AsyncUsageAnalyzers">
<Rule Id="UseConfigureAwait" Action="Warning" />
</Rules>
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1001" Action="Warning" />
<Rule Id="CA1009" Action="Warning" />
<Rule Id="CA1016" Action="Warning" />
<Rule Id="CA1033" Action="Warning" />
<Rule Id="CA1049" Action="Warning" />
<Rule Id="CA1060" Action="Warning" />
<Rule Id="CA1061" Action="Warning" />
<Rule Id="CA1063" Action="Warning" />
<Rule Id="CA1065" Action="Warning" />
<Rule Id="CA1301" Action="Warning" />
<Rule Id="CA1400" Action="Warning" />
<Rule Id="CA1401" Action="Warning" />
<Rule Id="CA1403" Action="Warning" />
<Rule Id="CA1404" Action="Warning" />
<Rule Id="CA1405" Action="Warning" />
<Rule Id="CA1410" Action="Warning" />
<Rule Id="CA1415" Action="Warning" />
<Rule Id="CA1821" Action="Warning" />
<Rule Id="CA1900" Action="Warning" />
<Rule Id="CA1901" Action="Warning" />
<Rule Id="CA2002" Action="Warning" />
<Rule Id="CA2100" Action="Warning" />
<Rule Id="CA2101" Action="Warning" />
<Rule Id="CA2108" Action="Warning" />
<Rule Id="CA2111" Action="Warning" />
<Rule Id="CA2112" Action="Warning" />
<Rule Id="CA2114" Action="Warning" />
<Rule Id="CA2116" Action="Warning" />
<Rule Id="CA2117" Action="Warning" />
<Rule Id="CA2122" Action="Warning" />
<Rule Id="CA2123" Action="Warning" />
<Rule Id="CA2124" Action="Warning" />
<Rule Id="CA2126" Action="Warning" />
<Rule Id="CA2131" Action="Warning" />
<Rule Id="CA2132" Action="Warning" />
<Rule Id="CA2133" Action="Warning" />
<Rule Id="CA2134" Action="Warning" />
<Rule Id="CA2137" Action="Warning" />
<Rule Id="CA2138" Action="Warning" />
<Rule Id="CA2140" Action="Warning" />
<Rule Id="CA2141" Action="Warning" />
<Rule Id="CA2146" Action="Warning" />
<Rule Id="CA2147" Action="Warning" />
<Rule Id="CA2149" Action="Warning" />
<Rule Id="CA2200" Action="Warning" />
<Rule Id="CA2202" Action="Warning" />
<Rule Id="CA2207" Action="Warning" />
<Rule Id="CA2212" Action="Warning" />
<Rule Id="CA2213" Action="Warning" />
<Rule Id="CA2214" Action="Warning" />
<Rule Id="CA2216" Action="Warning" />
<Rule Id="CA2220" Action="Warning" />
<Rule Id="CA2229" Action="Warning" />
<Rule Id="CA2231" Action="Warning" />
<Rule Id="CA2232" Action="Warning" />
<Rule Id="CA2235" Action="Warning" />
<Rule Id="CA2236" Action="Warning" />
<Rule Id="CA2237" Action="Warning" />
<Rule Id="CA2238" Action="Warning" />
<Rule Id="CA2240" Action="Warning" />
<Rule Id="CA2241" Action="Warning" />
<Rule Id="CA2242" Action="Warning" />
<Rule Id="CA1012" Action="Warning" />
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1305" Action="Warning" />
<Rule Id="SA1412" Action="Warning" />
<Rule Id="SA1600" Action="None" />
<Rule Id="SA1609" Action="Warning" />
</Rules>
</RuleSet>
通过编辑.csproj文件并添加以下内容,将其添加到您的项目文件中:
<PropertyGroup>
<CodeAnalysisRuleSet>ca.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
{
"$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
}
}
}
<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>
StyleCop支持创建自定义规则,因此您可以添加自己的“使用前导制表符而不是空格”的规则。如果您不想尝试开发自己的规则,可以在http://stylecopcontrib.codeplex.com/或http://github.com/AArnott/nerdbank.stylecop.rules获取现有规则。
好的,我成功地找出了问题所在,原因是这个警告出现的原因是开发人员有时会复制和粘贴代码
如果您正在使用VS2010 转到解决方案资源管理器 检查设置样式警察 然后修改样式警察内部的设置,如禁用或取消选择设置[间距.....]
在您的源代码控制服务器内完成。使用预提交脚本检查文件中以多个空格开头的行,并防止提交。
我同意制表符比空格更好。这是个人偏好,但团队一致性非常重要。