diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs index 3a6dbcdc4..5fb8ea893 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs @@ -16,6 +16,7 @@ namespace StyleCop.Analyzers.ReadabilityRules using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Settings.ObjectModel; /// /// Implements a code fix for . @@ -31,7 +32,7 @@ internal class SA1134CodeFixProvider : CodeFixProvider /// public override FixAllProvider GetFixAllProvider() { - return CustomFixAllProviders.BatchFixer; + return FixAll.Instance; } /// @@ -58,7 +59,18 @@ 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 tokensToReplace = new Dictionary(); + + AddTokensToReplaceToMap(tokensToReplace, syntaxRoot, diagnostic, settings); + + var newSyntaxRoot = syntaxRoot.ReplaceTokens(tokensToReplace.Keys, (original, rewritten) => tokensToReplace[original]); + var newDocument = document.WithSyntaxRoot(newSyntaxRoot.WithoutFormatting()); + + return newDocument; + } + private static void AddTokensToReplaceToMap(Dictionary tokensToReplace, SyntaxNode syntaxRoot, Diagnostic diagnostic, StyleCopSettings settings) + { var attributeListSyntax = (AttributeListSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan); // use the containing type to determine the indentation level, anything else is less reliable. @@ -66,8 +78,6 @@ private static async Task GetTransformedDocumentAsync(Document documen var indentationSteps = (containingType != null) ? IndentationHelper.GetIndentationSteps(settings.Indentation, containingType) + 1 : 0; var indentationTrivia = IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps); - var tokensToReplace = new Dictionary(); - if (diagnostic.Properties.ContainsKey(SA1134AttributesMustNotShareLine.FixWithNewLineBeforeKey)) { var token = attributeListSyntax.OpenBracketToken; @@ -89,11 +99,34 @@ private static async Task GetTransformedDocumentAsync(Document documen var newLeadingTrivia = nextToken.LeadingTrivia.Insert(0, indentationTrivia); tokensToReplace[nextToken] = nextToken.WithLeadingTrivia(newLeadingTrivia); } + } - var newSyntaxRoot = syntaxRoot.ReplaceTokens(tokensToReplace.Keys, (original, rewritten) => tokensToReplace[original]); - var newDocument = document.WithSyntaxRoot(newSyntaxRoot.WithoutFormatting()); + private class FixAll : DocumentBasedFixAllProvider + { + public static FixAllProvider Instance { get; } = new FixAll(); - return newDocument; + protected override string CodeActionTitle => ReadabilityResources.SA1134CodeFix; + + protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics) + { + if (diagnostics.IsEmpty) + { + return null; + } + + var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); + var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken); + var tokensToReplace = new Dictionary(); + + foreach (var diagnostic in diagnostics) + { + AddTokensToReplaceToMap(tokensToReplace, syntaxRoot, diagnostic, settings); + } + + var newSyntaxRoot = syntaxRoot.ReplaceTokens(tokensToReplace.Keys, (original, rewritten) => tokensToReplace[original]); + + return newSyntaxRoot; + } } } }