Skip to content

Commit deb1b95

Browse files
committed
Disable SA1027 when useTabs is true
1 parent b290142 commit deb1b95

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1027UnitTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace StyleCop.Analyzers.Test.SpacingRules
1717
/// </summary>
1818
public class SA1027UnitTests : CodeFixVerifier
1919
{
20+
private string settings;
21+
2022
/// <summary>
2123
/// Verifies that tabs used inside string and char literals are not producing diagnostics.
2224
/// </summary>
@@ -230,6 +232,38 @@ Comment 2
230232
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
231233
}
232234

235+
[Fact]
236+
public async Task TestUseTabsSettingAsync()
237+
{
238+
this.settings = @"
239+
{
240+
""settings"": {
241+
""indentation"": {
242+
""useTabs"": true
243+
}
244+
}
245+
}
246+
";
247+
248+
var testCode =
249+
"using\tSystem.Diagnostics;\r\n" +
250+
"\r\n" +
251+
"public\tclass\tFoo\r\n" +
252+
"{\r\n" +
253+
"\tpublic void Bar()\r\n" +
254+
"\t{\r\n" +
255+
"\t \t// Comment\r\n" +
256+
"\t \tDebug.Indent();\r\n" +
257+
" \t}\r\n" +
258+
"}\r\n";
259+
260+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
261+
}
262+
263+
/// <inheritdoc/>
264+
protected override string GetSettings() =>
265+
this.settings;
266+
233267
/// <inheritdoc/>
234268
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
235269
{

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027TabsMustNotBeUsed.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.SpacingRules
88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.Diagnostics;
11+
using StyleCop.Analyzers.Settings.ObjectModel;
1112

1213
/// <summary>
1314
/// The C# code contains a tab character.
@@ -38,7 +39,7 @@ internal class SA1027TabsMustNotBeUsed : DiagnosticAnalyzer
3839
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpacingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
3940

4041
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
41-
private static readonly Action<SyntaxTreeAnalysisContext> SyntaxTreeAction = HandleSyntaxTree;
42+
private static readonly Action<SyntaxTreeAnalysisContext, StyleCopSettings> SyntaxTreeAction = HandleSyntaxTree;
4243

4344
/// <inheritdoc/>
4445
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -55,8 +56,13 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
5556
context.RegisterSyntaxTreeActionHonorExclusions(SyntaxTreeAction);
5657
}
5758

58-
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
59+
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings)
5960
{
61+
if (settings.Indentation.UseTabs)
62+
{
63+
return;
64+
}
65+
6066
SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
6167
foreach (var trivia in root.DescendantTrivia(descendIntoTrivia: true))
6268
{

0 commit comments

Comments
 (0)