From deb1b95152bf8cf1cce9e883874c6fb63b49dfb8 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 11 Jan 2016 08:51:18 -0600 Subject: [PATCH] Disable SA1027 when useTabs is true --- .../SpacingRules/SA1027UnitTests.cs | 34 +++++++++++++++++++ .../SpacingRules/SA1027TabsMustNotBeUsed.cs | 10 ++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1027UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1027UnitTests.cs index 6a10fe23c..71201ef52 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1027UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1027UnitTests.cs @@ -17,6 +17,8 @@ namespace StyleCop.Analyzers.Test.SpacingRules /// public class SA1027UnitTests : CodeFixVerifier { + private string settings; + /// /// Verifies that tabs used inside string and char literals are not producing diagnostics. /// @@ -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); + } + + /// + protected override string GetSettings() => + this.settings; + /// protected override IEnumerable GetCSharpDiagnosticAnalyzers() { diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027TabsMustNotBeUsed.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027TabsMustNotBeUsed.cs index c4854ebd3..e42310afe 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027TabsMustNotBeUsed.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027TabsMustNotBeUsed.cs @@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.SpacingRules using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; + using StyleCop.Analyzers.Settings.ObjectModel; /// /// The C# code contains a tab character. @@ -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 CompilationStartAction = HandleCompilationStart; - private static readonly Action SyntaxTreeAction = HandleSyntaxTree; + private static readonly Action SyntaxTreeAction = HandleSyntaxTree; /// public override ImmutableArray SupportedDiagnostics { get; } = @@ -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)) {