diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs index 5e076a3fc..e7a95ed08 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs @@ -79,7 +79,7 @@ private static async Task GetTransformedDocumentAsync(Document documen private static async Task GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken) { var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(root.SyntaxTree, cancellationToken); + var settings = document.Project.AnalyzerOptions.GetStyleCopSettingsInCodeFix(root.SyntaxTree, cancellationToken); var fileHeader = FileHeaderHelpers.ParseFileHeader(root); SyntaxNode newSyntaxRoot; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs index 716937ebd..15c7a52cf 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs @@ -85,7 +85,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) internal static ImmutableArray GenerateStandardText(Document document, BaseMethodDeclarationSyntax methodDeclaration, BaseTypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken) { bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration) || typeDeclaration.IsKind(SyntaxKindEx.RecordStructDeclaration); - var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(methodDeclaration.SyntaxTree, cancellationToken); + var settings = document.Project.AnalyzerOptions.GetStyleCopSettingsInCodeFix(methodDeclaration.SyntaxTree, cancellationToken); var culture = settings.DocumentationRules.DocumentationCultureInfo; var resourceManager = DocumentationResources.ResourceManager; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs index b16c739be..eb9c1bdca 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs @@ -58,7 +58,7 @@ private static async Task GetTransformedDocumentAsync(Document documen { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var braceToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); var tokenReplacements = GenerateBraceFixes(settings, ImmutableArray.Create(braceToken)); @@ -290,7 +290,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi .OrderBy(token => token.SpanStart) .ToImmutableArray(); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); var tokenReplacements = GenerateBraceFixes(settings, tokens); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs index 592c958c5..e9aad42da 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs @@ -60,7 +60,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is StatementSyntax statement)) { return document; @@ -304,7 +304,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi var tokenReplaceMap = new Dictionary(); SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); foreach (var diagnostic in diagnostics.Sort(DiagnosticComparer.Instance)) { diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs index f24bd818e..f9f013eba 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs @@ -55,7 +55,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var newDocument = this.CreateCodeFix(document, settings.Indentation, diagnostic, syntaxRoot); return newDocument; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs index 51c7acb51..62aea1639 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs @@ -111,7 +111,7 @@ private static bool IsAllowedTrivia(SyntaxTrivia trivia) private static async Task GetTransformedDocumentForSingleLineAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan); var accessorList = GetAccessorList(node); @@ -190,7 +190,7 @@ private static BlockSyntax ReformatBodyAsSingleLine(BlockSyntax body) private static async Task GetTransformedDocumentForMutipleLinesAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan); var accessorList = GetAccessorList(node); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs index 6ad31f1db..e67fa7c3e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs @@ -37,7 +37,7 @@ public override FixAllProvider GetFixAllProvider() public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var syntaxTree = await context.Document.GetSyntaxTreeAsync(context.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(context.Document.Project.AnalyzerOptions, syntaxTree, context.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(context.Document.Project.AnalyzerOptions, syntaxTree, context.CancellationToken); foreach (var diagnostic in context.Diagnostics) { context.RegisterCodeFix( @@ -80,7 +80,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi } var syntaxTree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxTree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxTree, fixAllContext.CancellationToken); Document updatedDocument = await FixEndOfFileAsync(document, diagnostics[0], settings.LayoutRules.NewlineAtEndOfFile, fixAllContext.CancellationToken).ConfigureAwait(false); return await updatedDocument.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs index 2f1d0740e..63b38d8e9 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs @@ -68,7 +68,7 @@ private static async Task GetTransformedSolutionAsync(Document documen DocumentId extractedDocumentId = DocumentId.CreateNewId(document.Project.Id); string suffix; FileNameHelpers.GetFileNameAndSuffix(document.Name, out suffix); - var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(root.SyntaxTree, cancellationToken); + var settings = document.Project.AnalyzerOptions.GetStyleCopSettingsInCodeFix(root.SyntaxTree, cancellationToken); string extractedDocumentName = FileNameHelpers.GetConventionalFileName(memberDeclarationSyntax, settings.DocumentationRules.FileNamingConvention) + suffix; List nodesToRemoveFromExtracted = new List(); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs index 808f1bd2a..0339c3a69 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs @@ -61,7 +61,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var elementOrder = settings.OrderingRules.ElementOrder; var memberDeclaration = syntaxRoot.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf(); @@ -267,7 +267,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi } var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); var elementOrder = settings.OrderingRules.ElementOrder; var trackedDiagnosticMembers = new List(); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs index 93366215b..7be71dbaa 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs @@ -84,7 +84,7 @@ private static async Task GetTransformedDocumentAsync(Document documen var compilationUnit = (CompilationUnitSyntax)syntaxRoot; var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, semanticModel.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, semanticModel.SyntaxTree, cancellationToken); var usingDirectivesPlacement = forcePreservePlacement ? UsingDirectivesPlacement.Preserve : DeterminePlacement(compilationUnit, settings); var usingsHelper = new UsingsSorter(settings, semanticModel, compilationUnit, fileHeader); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs index bece491ab..23ad1887f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs @@ -54,7 +54,7 @@ private static async Task GetTransformedDocumentAsync(Document documen var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var indentationTrivia = QueryIndentationHelpers.GetQueryIndentationTrivia(settings.Indentation, token); var precedingToken = token.GetPreviousToken(); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs index dc634ec81..c038487e0 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs @@ -120,7 +120,7 @@ private static async Task GetTransformedDocumentForMultipleLinesAsync( var nodeList = CreateQueryNodeList(queryExpression); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var indentationTrivia = QueryIndentationHelpers.GetQueryIndentationTrivia(settings.Indentation, queryExpression); for (var i = 1; i < nodeList.Length; i++) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs index fd8ffb8c4..64c7a6a5b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs @@ -56,7 +56,7 @@ private static async Task GetTransformedDocumentAsync(Document documen var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var indentationTrivia = QueryIndentationHelpers.GetQueryIndentationTrivia(settings.Indentation, token); var precedingToken = token.GetPreviousToken(); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs index 9bafce580..a91b778bf 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs @@ -72,7 +72,7 @@ private static async Task GetTransformedDocumentAsync(Document documen } } - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, tree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, tree, cancellationToken); SyntaxTriviaList newTrivia = SyntaxFactory.TriviaList( SyntaxFactory.CarriageReturnLineFeed, diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs index c0ea7a3f7..96cd34535 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs @@ -62,7 +62,7 @@ private static async Task GetTransformedDocumentAsync(Document documen var afterEndToken = endToken.GetNextToken(); var parentIndentation = GetParentIndentation(whereToken); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var indentationTrivia = SyntaxFactory.Whitespace(parentIndentation + IndentationHelper.GenerateIndentationString(settings.Indentation, 1)); var replaceMap = new Dictionary() diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs index 09b4230f7..494c1092b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs @@ -57,7 +57,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var newLine = FormattingHelper.GetNewLineTrivia(document); var constructorInitializer = (ConstructorInitializerSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan); @@ -110,7 +110,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi } var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); var newLine = FormattingHelper.GetNewLineTrivia(document); var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan).Parent); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs index ebb9a6a56..39500c0e0 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs @@ -61,7 +61,7 @@ private static async Task GetTransformedDocumentAsync(Document documen var nodeInSourceSpan = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true); AttributeListSyntax attributeList = nodeInSourceSpan.FirstAncestorOrSelf(); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var indentationSteps = IndentationHelper.GetIndentationSteps(settings.Indentation, attributeList); var indentationTrivia = IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps); @@ -114,7 +114,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi } var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); // 🐉 Need to eagerly evaluate this with ToList() to ensure nodes are not garbage collected between the // call to TrackNodes and subsequent enumeration. diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs index 5fb8ea893..0dd705d1d 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs @@ -58,7 +58,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var tokensToReplace = new Dictionary(); AddTokensToReplaceToMap(tokensToReplace, syntaxRoot, diagnostic, settings); @@ -115,7 +115,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi } var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); var tokensToReplace = new Dictionary(); foreach (var diagnostic in diagnostics) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs index 114d839a1..4e03e3617 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs @@ -53,7 +53,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken); var enumMemberDeclaration = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan); var enumDeclaration = (EnumDeclarationSyntax)enumMemberDeclaration.Parent; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs index 8eacf22e4..5af98430a 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs @@ -55,7 +55,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxTree, cancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxTree, cancellationToken); SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); return document.WithText(sourceText.WithChanges(FixDiagnostic(settings.Indentation, sourceText, diagnostic))); } @@ -161,7 +161,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi } SyntaxTree tree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false); - var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, tree, fixAllContext.CancellationToken); + var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, tree, fixAllContext.CancellationToken); SourceText sourceText = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false); List changes = new List(); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs index b3826f2f7..3f74dfb43 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs @@ -53,7 +53,7 @@ public async Task VerifyEditorConfigSettingsAreReadCorrectlyAsync() "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName); Assert.Equal("Custom copyright text.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -80,7 +80,7 @@ public async Task VerifyFileHeaderTemplateFromEditorConfigAsync() "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("Line 1\nLine 2.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); } @@ -95,7 +95,7 @@ public async Task VerifyStyleCopDocumentationCopyrightTextFromEditorConfigAsync( "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("Line 1\nLine 2.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); } @@ -116,7 +116,7 @@ public async Task VerifyBooleanDocumentationSettingsFromEditorConfigAsync(bool v "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal(value, styleCopSettings.DocumentationRules.DocumentExposedElements); Assert.Equal(value, styleCopSettings.DocumentationRules.DocumentInternalElements); @@ -137,7 +137,7 @@ public async Task VerifySettingsWillUseCompanyNameInDefaultCopyrightTextFromEdit "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal(companyName, styleCopSettings.DocumentationRules.CompanyName); Assert.Equal($"Copyright (c) {companyName}. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -153,7 +153,7 @@ public async Task VerifyCircularReferenceBehaviorFromEditorConfigAsync() "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("[CircularReference]", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); } @@ -168,7 +168,7 @@ public async Task VerifyInvalidReferenceBehaviorFromEditorConfigAsync() "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("[InvalidReference]", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); } @@ -191,7 +191,7 @@ public async Task VerifyEditorConfigSettingsReadCorrectlyDirectivePlacementWitho "; var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.NotNull(styleCopSettings.OrderingRules); Assert.Equal(expected, styleCopSettings.OrderingRules.UsingDirectivesPlacement); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/IndentationHelperTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/IndentationHelperTests.cs index 3a46f34f5..a53776127 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/IndentationHelperTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/IndentationHelperTests.cs @@ -115,7 +115,7 @@ public async Task VerifyGetIndentationStepsAsync(string indentationString, int e var testSource = $"{indentationString}public class TestClass {{}}"; var document = await CreateTestDocumentAsync(testSource, indentationSize, false, tabSize, CancellationToken.None).ConfigureAwait(false); var syntaxRoot = await document.GetSyntaxRootAsync(CancellationToken.None).ConfigureAwait(false); - StyleCopSettings settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, CancellationToken.None); + StyleCopSettings settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, CancellationToken.None); var firstToken = syntaxRoot.GetFirstToken(); @@ -133,7 +133,7 @@ public async Task VerifyGetIndentationStepsForTokenNotAtStartOfLineAsync() var testSource = " public class TestClass {}"; var document = await CreateTestDocumentAsync(testSource, cancellationToken: CancellationToken.None).ConfigureAwait(false); var syntaxRoot = await document.GetSyntaxRootAsync(CancellationToken.None).ConfigureAwait(false); - StyleCopSettings settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, CancellationToken.None); + StyleCopSettings settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, CancellationToken.None); var secondToken = syntaxRoot.GetFirstToken().GetNextToken(); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsUnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsUnitTests.cs index ac0f16e83..f28af26f2 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsUnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsUnitTests.cs @@ -26,7 +26,7 @@ public class SettingsUnitTests [Fact] public void VerifySettingsDefaults() { - var styleCopSettings = SettingsHelper.GetStyleCopSettings(default(SyntaxTreeAnalysisContext), CancellationToken.None); + var styleCopSettings = SettingsHelper.GetStyleCopSettingsInTests(default(SyntaxTreeAnalysisContext), CancellationToken.None); Assert.Equal("PlaceholderCompany", styleCopSettings.DocumentationRules.CompanyName); Assert.Equal("Copyright (c) PlaceholderCompany. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -65,7 +65,7 @@ public async Task VerifyDefaultCultureIsReadCorrectlyAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("en-US", styleCopSettings.DocumentationRules.DocumentationCulture); Assert.Same(CultureInfo.InvariantCulture, styleCopSettings.DocumentationRules.DocumentationCultureInfo); @@ -116,7 +116,7 @@ public async Task VerifySettingsAreReadCorrectlyAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName); Assert.Equal("Custom copyright text.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -159,7 +159,7 @@ public async Task VerifyBooleanDocumentationSettingsAsync(bool value) "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal(value, styleCopSettings.DocumentationRules.DocumentExposedElements); Assert.Equal(value, styleCopSettings.DocumentationRules.DocumentInternalElements); @@ -189,7 +189,7 @@ public async Task VerifyDocumentationVariablesAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Single(styleCopSettings.DocumentationRules.Variables); Assert.Equal("value", styleCopSettings.DocumentationRules.Variables["var"]); @@ -217,7 +217,7 @@ public async Task VerifySettingsWillUseCompanyNameInDefaultCopyrightTextAsync(st "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal(companyName, styleCopSettings.DocumentationRules.CompanyName); Assert.Equal($"Copyright (c) {companyName}. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -237,7 +237,7 @@ public async Task VerifyCircularReferenceBehaviorAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("[CircularReference]", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); } @@ -256,7 +256,7 @@ public async Task VerifyInvalidReferenceBehaviorAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("[InvalidReference]", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); } @@ -280,7 +280,7 @@ public async Task VerifySettingsSupportsLineCommentsAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName); Assert.Equal("Copyright (c) TestCompany. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -307,7 +307,7 @@ public async Task VerifySettingsSupportsBlockCommentsAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName); Assert.Equal("Copyright (c) TestCompany. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -334,7 +334,7 @@ public async Task VerifySettingsSupportsTrailingCommasAsync() "; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName); Assert.Equal("Copyright (c) TestCompany. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); @@ -357,7 +357,7 @@ public async Task VerifySettingsFileNameSupportsDotPrefixAsync() "; var context = await CreateAnalysisContextAsync(settings, ".stylecop.json").ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName); } @@ -368,7 +368,7 @@ public async Task VerifyInvalidJsonBehaviorAsync() var settings = @"This is not a JSON file."; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); // The result is the same as the default settings. Assert.Equal("PlaceholderCompany", styleCopSettings.DocumentationRules.CompanyName); @@ -381,7 +381,7 @@ public async Task VerifyEmptyOrMissingFileAsync() var settings = string.Empty; var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false); - var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + var styleCopSettings = context.GetStyleCopSettingsInTests(CancellationToken.None); // The result is the same as the default settings. Assert.Equal("PlaceholderCompany", styleCopSettings.DocumentationRules.CompanyName); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerExtensions.cs b/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerExtensions.cs index e905b2236..07fbe6869 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerExtensions.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerExtensions.cs @@ -9,6 +9,7 @@ namespace StyleCop.Analyzers using System.Collections.Concurrent; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; + using System.Runtime.CompilerServices; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using StyleCop.Analyzers.Settings.ObjectModel; @@ -32,9 +33,12 @@ public static void RegisterSyntaxTreeAction(this CompilationStartAnalysisContext context.RegisterSyntaxTreeAction( context => { - StyleCopSettings settings = context.GetStyleCopSettings(settingsFile); + StyleCopSettings settings = context.GetStyleCopSettings(GetOrCreateSettingsStorage(context.Tree), settingsFile); action(context, settings); }); + + StrongBox GetOrCreateSettingsStorage(SyntaxTree tree) + => SettingsHelper.GetOrCreateSettingsStorage(context, tree); } /// @@ -74,10 +78,13 @@ public static void RegisterSyntaxNodeAction(this CompilationS context.RegisterSyntaxNodeAction( context => { - StyleCopSettings settings = context.GetStyleCopSettings(settingsFile); + StyleCopSettings settings = context.GetStyleCopSettings(GetOrCreateSettingsStorage(context.Node.SyntaxTree), settingsFile); action(context, settings); }, syntaxKinds); + + StrongBox GetOrCreateSettingsStorage(SyntaxTree tree) + => SettingsHelper.GetOrCreateSettingsStorage(context, tree); } private static class LanguageKindArrays diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/LightJson/JsonObject.cs b/StyleCop.Analyzers/StyleCop.Analyzers/LightJson/JsonObject.cs index 49e8b9b54..2b7551906 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/LightJson/JsonObject.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/LightJson/JsonObject.cs @@ -16,7 +16,7 @@ namespace LightJson [DebuggerTypeProxy(typeof(JsonObjectDebugView))] internal sealed class JsonObject : IEnumerable>, IEnumerable { - private readonly IDictionary properties; + private readonly Dictionary properties; /// /// Initializes a new instance of the class. @@ -158,14 +158,23 @@ public bool ContainsKey(string key) /// Returns true if the value is found; otherwise, false. public bool Contains(JsonValue value) { - return this.properties.Values.Contains(value); + return this.properties.ContainsValue(value); } /// /// Returns an enumerator that iterates through this collection. /// /// The enumerator that iterates through this collection. - public IEnumerator> GetEnumerator() + public Dictionary.Enumerator GetEnumerator() + { + return this.properties.GetEnumerator(); + } + + /// + /// Returns an enumerator that iterates through this collection. + /// + /// The enumerator that iterates through this collection. + IEnumerator> IEnumerable>.GetEnumerator() { return this.properties.GetEnumerator(); } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/JsonHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/JsonHelper.cs index 533e4511d..d433ebd73 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/JsonHelper.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/JsonHelper.cs @@ -7,6 +7,7 @@ namespace StyleCop.Analyzers { using System; using System.Collections.Generic; + using System.Collections.Immutable; using LightJson; /// @@ -82,7 +83,7 @@ internal static string ToStringValue(this JsonValue jsonValue, string elementNam /// The key value pair identifying the JSON value. /// The enum value contained within the JSON value. internal static TEnum ToEnumValue(this KeyValuePair jsonValue) - where TEnum : struct + where TEnum : struct, Enum { if (!jsonValue.Value.IsString) { @@ -90,7 +91,7 @@ internal static TEnum ToEnumValue(this KeyValuePair js } TEnum result; - if (!Enum.TryParse(jsonValue.Value.AsString, true, out result)) + if (!EnumHelper.TryParse(jsonValue.Value.AsString, out result)) { throw new InvalidSettingsException($"{jsonValue.Key} cannot contain enum value '{jsonValue.Value.AsString}'"); } @@ -106,7 +107,7 @@ internal static TEnum ToEnumValue(this KeyValuePair js /// The element name to report in exceptions. /// The enum value contained within the JSON value. internal static TEnum ToEnumValue(this JsonValue jsonValue, string elementName) - where TEnum : struct + where TEnum : struct, Enum { if (!jsonValue.IsString) { @@ -114,7 +115,7 @@ internal static TEnum ToEnumValue(this JsonValue jsonValue, string elemen } TEnum result; - if (!Enum.TryParse(jsonValue.AsString, true, out result)) + if (!EnumHelper.TryParse(jsonValue.AsString, out result)) { throw new InvalidSettingsException($"{elementName} cannot contain enum value '{jsonValue.AsString}'"); } @@ -145,5 +146,25 @@ internal static void AssertIsObject(this KeyValuePair jsonVal throw new InvalidSettingsException($"{jsonValue.Key} must contain an object"); } } + + private static class EnumHelper + where TEnum : struct, Enum + { + private static ImmutableDictionary> values = ImmutableDictionary>.Empty; + + public static bool TryParse(string value, out TEnum result) + { + var successAndResult = ImmutableInterlocked.GetOrAdd( + ref values, + value, + static value => + { + bool success = Enum.TryParse(value, true, out TEnum result); + return new KeyValuePair(success, result); + }); + result = successAndResult.Value; + return successAndResult.Key; + } + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs index 0b7278f91..0f2f64c21 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs @@ -67,7 +67,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi case "allowedNamespaceComponents": kvp.AssertIsArray(); allowedNamespaceComponents = ImmutableArray.CreateBuilder(); - allowedNamespaceComponents.AddRange(kvp.Value.AsJsonArray.Select(x => x.ToStringValue(kvp.Key))); + allowedNamespaceComponents.AddRange(kvp.Value.AsJsonArray.Select(static x => x.ToStringValue("allowedNamespaceComponents"))); break; case "includeInferredTupleElementNames": @@ -85,7 +85,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi allowCommonHungarianPrefixes ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.naming.allowCommonHungarianPrefixes"); allowedHungarianPrefixes ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedHungarianPrefixes") - ?.Where(value => IsValidHungarianPrefix(value)) + ?.Where(static value => IsValidHungarianPrefix(value)) .ToImmutableArray() .ToBuilder(); allowedNamespaceComponents ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedNamespaceComponents")?.ToBuilder(); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs index 9e903af33..f00a9e608 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; + using System.Runtime.CompilerServices; using System.Threading; using LightJson; using LightJson.Serialization; @@ -29,7 +30,10 @@ internal static class SettingsHelper private static SourceTextValueProvider> JsonValueProvider { get; } = new SourceTextValueProvider>( - text => ParseJson(text)); + static text => ParseJson(text)); + + private static SyntaxTreeValueProvider> SettingsStorageValueProvider { get; } = + new SyntaxTreeValueProvider>(static _ => new StrongBox()); /// /// Gets the StyleCop settings from the JSON file, i.e. without adding information frmo the .editorconfig. @@ -58,6 +62,27 @@ Lazy GetJsonValue(SourceText settingsText) } } + [SuppressMessage("MicrosoftCodeAnalysisPerformance", "RS1012:Start action has no registered actions", Justification = "This is not a start action")] + internal static StrongBox GetOrCreateSettingsStorage(this CompilationStartAnalysisContext context, SyntaxTree tree) + { + if (!context.TryGetValue(tree, SettingsStorageValueProvider, out var storage)) + { + storage = new StrongBox(); + } + + return storage; + } + + internal static StrongBox GetOrCreateSettingsStorage(this CompilationAnalysisContext context, SyntaxTree tree) + { + if (!context.TryGetValue(tree, SettingsStorageValueProvider, out var storage)) + { + storage = new StrongBox(); + } + + return storage; + } + /// /// Gets the StyleCop settings. /// @@ -66,11 +91,12 @@ Lazy GetJsonValue(SourceText settingsText) /// deserializing the settings file, a default settings instance is returned. /// /// The context that will be used to determine the StyleCop settings. + /// The storage location for caching an evaluated object. /// Information about the StyleCop settings file. /// A instance that represents the StyleCop settings for the given context. - internal static StyleCopSettings GetStyleCopSettings(this SyntaxTreeAnalysisContext context, SettingsFile settingsFile) + internal static StyleCopSettings GetStyleCopSettings(this SyntaxTreeAnalysisContext context, StrongBox settingsStorage, SettingsFile settingsFile) { - return GetSettings(context.Options, context.Tree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); + return GetSettings(context.Options, settingsStorage, context.Tree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); } /// @@ -84,10 +110,10 @@ internal static StyleCopSettings GetStyleCopSettings(this SyntaxTreeAnalysisCont /// The context that will be used to determine the StyleCop settings. /// The cancellation token that the operation will observe. /// A instance that represents the StyleCop settings for the given context. - internal static StyleCopSettings GetStyleCopSettings(this SyntaxTreeAnalysisContext context, CancellationToken cancellationToken) + internal static StyleCopSettings GetStyleCopSettingsInTests(this SyntaxTreeAnalysisContext context, CancellationToken cancellationToken) { var settingsFile = GetSettingsFile(context.Options, ParseJson, cancellationToken); - return GetSettings(context.Options, context.Tree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); + return GetSettings(context.Options, settingsStorage: new StrongBox(), context.Tree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); } /// @@ -98,11 +124,12 @@ internal static StyleCopSettings GetStyleCopSettings(this SyntaxTreeAnalysisCont /// deserializing the settings file, a default settings instance is returned. /// /// The context that will be used to determine the StyleCop settings. + /// The storage location for caching an evaluated object. /// The contents of the StyleCop settnigs file. /// A instance that represents the StyleCop settings for the given context. - internal static StyleCopSettings GetStyleCopSettings(this SyntaxNodeAnalysisContext context, SettingsFile settingsFile) + internal static StyleCopSettings GetStyleCopSettings(this SyntaxNodeAnalysisContext context, StrongBox settingsStorage, SettingsFile settingsFile) { - return GetSettings(context.Options, context.Node.SyntaxTree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); + return GetSettings(context.Options, settingsStorage, context.Node.SyntaxTree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); } /// @@ -117,10 +144,10 @@ internal static StyleCopSettings GetStyleCopSettings(this SyntaxNodeAnalysisCont /// The syntax tree. /// The cancellation token that the operation will observe. /// A instance that represents the StyleCop settings for the given context. - internal static StyleCopSettings GetStyleCopSettings(this AnalyzerOptions options, SyntaxTree tree, CancellationToken cancellationToken) + internal static StyleCopSettings GetStyleCopSettingsInCodeFix(this AnalyzerOptions options, SyntaxTree tree, CancellationToken cancellationToken) { var settingsFile = GetSettingsFile(options, ParseJson, cancellationToken); - return GetSettings(options, tree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); + return GetSettings(options, settingsStorage: new StrongBox(), tree, settingsFile, DeserializationFailureBehavior.ReturnDefaultSettings); } /// @@ -135,7 +162,7 @@ internal static StyleCopSettings GetStyleCopSettings(this AnalyzerOptions option internal static StyleCopSettings GetStyleCopSettings(this CompilationAnalysisContext context, SyntaxTree tree, DeserializationFailureBehavior failureBehavior, CancellationToken cancellationToken) { var settingsFile = GetSettingsFile(context.Options, GetJsonValue, cancellationToken); - return GetSettings(context.Options, tree, settingsFile, failureBehavior); + return GetSettings(context.Options, GetOrCreateSettingsStorage(context, tree), tree, settingsFile, failureBehavior); Lazy GetJsonValue(SourceText settingsText) { @@ -171,21 +198,32 @@ internal static bool IsStyleCopSettingsFile(string path) || string.Equals(fileName, AltSettingsFileName, StringComparison.OrdinalIgnoreCase); } - private static StyleCopSettings GetSettings(AnalyzerOptions options, SyntaxTree tree, SettingsFile settingsFile, DeserializationFailureBehavior failureBehavior) + private static StyleCopSettings GetSettings(AnalyzerOptions options, StrongBox settingsStorage, SyntaxTree tree, SettingsFile settingsFile, DeserializationFailureBehavior failureBehavior) { - if (settingsFile != null) + if (settingsStorage.Value is { } settings) { - return CreateSettingsObjectFromFile(options, tree, settingsFile, failureBehavior); + return settings; } - // TODO: Can this really be null? Review when nullable references has been enabled - if (tree != null) + if (settingsFile != null) + { + settings = CreateSettingsObjectFromFile(options, tree, settingsFile, failureBehavior); + } + else { - var analyzerConfigOptions = options.AnalyzerConfigOptionsProvider().GetOptions(tree); - return new StyleCopSettings(new JsonObject(), analyzerConfigOptions); + // TODO: Can this really be null? Review when nullable references has been enabled + if (tree != null) + { + var analyzerConfigOptions = options.AnalyzerConfigOptionsProvider().GetOptions(tree); + settings = new StyleCopSettings(new JsonObject(), analyzerConfigOptions); + } + else + { + settings = new StyleCopSettings(); + } } - return new StyleCopSettings(); + return Interlocked.CompareExchange(ref settingsStorage.Value, settings, null) ?? settings; } private static StyleCopSettings CreateSettingsObjectFromFile(AnalyzerOptions options, SyntaxTree tree, SettingsFile settingsFile, DeserializationFailureBehavior failureBehavior)