Skip to content

Commit

Permalink
Disable SA1027 when useTabs is true
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jan 11, 2016
1 parent b290142 commit deb1b95
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace StyleCop.Analyzers.Test.SpacingRules
/// </summary>
public class SA1027UnitTests : CodeFixVerifier
{
private string settings;

/// <summary>
/// Verifies that tabs used inside string and char literals are not producing diagnostics.
/// </summary>
Expand Down Expand Up @@ -230,6 +232,38 @@ Comment 2
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestUseTabsSettingAsync()
{
this.settings = @"
{
""settings"": {
""indentation"": {
""useTabs"": true
}
}
}
";

var testCode =
"using\tSystem.Diagnostics;\r\n" +
"\r\n" +
"public\tclass\tFoo\r\n" +
"{\r\n" +
"\tpublic void Bar()\r\n" +
"\t{\r\n" +
"\t \t// Comment\r\n" +
"\t \tDebug.Indent();\r\n" +
" \t}\r\n" +
"}\r\n";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <inheritdoc/>
protected override string GetSettings() =>
this.settings;

/// <inheritdoc/>
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.SpacingRules
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Settings.ObjectModel;

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

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxTreeAnalysisContext> SyntaxTreeAction = HandleSyntaxTree;
private static readonly Action<SyntaxTreeAnalysisContext, StyleCopSettings> SyntaxTreeAction = HandleSyntaxTree;

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

private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings)
{
if (settings.Indentation.UseTabs)
{
return;
}

SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
foreach (var trivia in root.DescendantTrivia(descendIntoTrivia: true))
{
Expand Down

0 comments on commit deb1b95

Please sign in to comment.