@@ -14,6 +14,7 @@ namespace StyleCop.Analyzers.ReadabilityRules
1414 using Microsoft . CodeAnalysis . CSharp ;
1515 using Microsoft . CodeAnalysis . CSharp . Syntax ;
1616 using StyleCop . Analyzers . Helpers ;
17+ using StyleCop . Analyzers . Settings . ObjectModel ;
1718
1819 /// <summary>
1920 /// Implements a code fix for <see cref="SA1134AttributesMustNotShareLine"/>.
@@ -29,7 +30,7 @@ internal class SA1134CodeFixProvider : CodeFixProvider
2930 /// <inheritdoc/>
3031 public override FixAllProvider GetFixAllProvider ( )
3132 {
32- return CustomFixAllProviders . BatchFixer ;
33+ return FixAll . Instance ;
3334 }
3435
3536 /// <inheritdoc/>
@@ -52,16 +53,25 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5253 {
5354 var syntaxRoot = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
5455 var settings = SettingsHelper . GetStyleCopSettings ( document . Project . AnalyzerOptions , cancellationToken ) ;
56+ var tokensToReplace = new Dictionary < SyntaxToken , SyntaxToken > ( ) ;
57+
58+ AddTokensToReplaceToMap ( tokensToReplace , syntaxRoot , diagnostic , settings ) ;
59+
60+ var newSyntaxRoot = syntaxRoot . ReplaceTokens ( tokensToReplace . Keys , ( original , rewritten ) => tokensToReplace [ original ] ) ;
61+ var newDocument = document . WithSyntaxRoot ( newSyntaxRoot . WithoutFormatting ( ) ) ;
62+
63+ return newDocument ;
64+ }
5565
66+ private static void AddTokensToReplaceToMap ( Dictionary < SyntaxToken , SyntaxToken > tokensToReplace , SyntaxNode syntaxRoot , Diagnostic diagnostic , StyleCopSettings settings )
67+ {
5668 var attributeListSyntax = ( AttributeListSyntax ) syntaxRoot . FindNode ( diagnostic . Location . SourceSpan ) ;
5769
5870 // use the containing type to determine the indentation level, anything else is less reliable.
5971 var containingType = attributeListSyntax . Parent ? . Parent ;
6072 var indentationSteps = ( containingType != null ) ? IndentationHelper . GetIndentationSteps ( settings . Indentation , containingType ) + 1 : 0 ;
6173 var indentationTrivia = IndentationHelper . GenerateWhitespaceTrivia ( settings . Indentation , indentationSteps ) ;
6274
63- var tokensToReplace = new Dictionary < SyntaxToken , SyntaxToken > ( ) ;
64-
6575 if ( diagnostic . Properties . ContainsKey ( SA1134AttributesMustNotShareLine . FixWithNewLineBeforeKey ) )
6676 {
6777 var token = attributeListSyntax . OpenBracketToken ;
@@ -83,11 +93,34 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
8393 var newLeadingTrivia = nextToken . LeadingTrivia . Insert ( 0 , indentationTrivia ) ;
8494 tokensToReplace [ nextToken ] = nextToken . WithLeadingTrivia ( newLeadingTrivia ) ;
8595 }
96+ }
8697
87- var newSyntaxRoot = syntaxRoot . ReplaceTokens ( tokensToReplace . Keys , ( original , rewritten ) => tokensToReplace [ original ] ) ;
88- var newDocument = document . WithSyntaxRoot ( newSyntaxRoot . WithoutFormatting ( ) ) ;
98+ private class FixAll : DocumentBasedFixAllProvider
99+ {
100+ public static FixAllProvider Instance { get ; } = new FixAll ( ) ;
89101
90- return newDocument ;
102+ protected override string CodeActionTitle => ReadabilityResources . SA1134CodeFix ;
103+
104+ protected override async Task < SyntaxNode > FixAllInDocumentAsync ( FixAllContext fixAllContext , Document document , ImmutableArray < Diagnostic > diagnostics )
105+ {
106+ if ( diagnostics . IsEmpty )
107+ {
108+ return null ;
109+ }
110+
111+ var syntaxRoot = await document . GetSyntaxRootAsync ( fixAllContext . CancellationToken ) . ConfigureAwait ( false ) ;
112+ var settings = SettingsHelper . GetStyleCopSettings ( document . Project . AnalyzerOptions , fixAllContext . CancellationToken ) ;
113+ var tokensToReplace = new Dictionary < SyntaxToken , SyntaxToken > ( ) ;
114+
115+ foreach ( var diagnostic in diagnostics )
116+ {
117+ AddTokensToReplaceToMap ( tokensToReplace , syntaxRoot , diagnostic , settings ) ;
118+ }
119+
120+ var newSyntaxRoot = syntaxRoot . ReplaceTokens ( tokensToReplace . Keys , ( original , rewritten ) => tokensToReplace [ original ] ) ;
121+
122+ return newSyntaxRoot ;
123+ }
91124 }
92125 }
93126}
0 commit comments