@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.ReadabilityRules
66 using System . Collections . Generic ;
77 using System . Collections . Immutable ;
88 using System . Composition ;
9+ using System . Linq ;
910 using System . Threading ;
1011 using System . Threading . Tasks ;
1112 using Microsoft . CodeAnalysis ;
@@ -56,12 +57,12 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5657 }
5758
5859 SemanticModel semanticModel = await document . GetSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
59- var replacementNode = GenerateReplacementNode ( semanticModel , node , cancellationToken ) ;
60+ var replacementNode = GetReplacementNode ( semanticModel , node , cancellationToken ) ;
6061 var newSyntaxRoot = syntaxRoot . ReplaceNode ( node , replacementNode ) ;
6162 return document . WithSyntaxRoot ( newSyntaxRoot ) ;
6263 }
6364
64- private static SyntaxNode GenerateReplacementNode ( SemanticModel semanticModel , UsingDirectiveSyntax node , CancellationToken cancellationToken )
65+ private static SyntaxNode GetReplacementNode ( SemanticModel semanticModel , UsingDirectiveSyntax node , CancellationToken cancellationToken )
6566 {
6667 SymbolInfo symbolInfo = semanticModel . GetSymbolInfo ( node . Name , cancellationToken ) ;
6768 return node . WithName ( SyntaxFactory . ParseName ( symbolInfo . Symbol . ToString ( ) ) ) ;
@@ -75,9 +76,8 @@ private class FixAll : DocumentBasedFixAllProvider
7576 protected override string CodeActionTitle =>
7677 ReadabilityResources . SA1135CodeFix ;
7778
78- protected override async Task < SyntaxNode > FixAllInDocumentAsync ( FixAllContext fixAllContext , Document document )
79+ protected override async Task < SyntaxNode > FixAllInDocumentAsync ( FixAllContext fixAllContext , Document document , ImmutableArray < Diagnostic > diagnostics )
7980 {
80- var diagnostics = await fixAllContext . GetDocumentDiagnosticsAsync ( document ) . ConfigureAwait ( false ) ;
8181 if ( diagnostics . IsEmpty )
8282 {
8383 return null ;
@@ -86,20 +86,9 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
8686 SyntaxNode syntaxRoot = await document . GetSyntaxRootAsync ( fixAllContext . CancellationToken ) . ConfigureAwait ( false ) ;
8787 SemanticModel semanticModel = await document . GetSemanticModelAsync ( fixAllContext . CancellationToken ) . ConfigureAwait ( false ) ;
8888
89- var replaceMap = new Dictionary < SyntaxNode , SyntaxNode > ( ) ;
89+ var nodes = diagnostics . Select ( diagnostic => syntaxRoot . FindNode ( diagnostic . Location . SourceSpan , getInnermostNodeForTie : true ) . FirstAncestorOrSelf < UsingDirectiveSyntax > ( ) ) ;
9090
91- foreach ( Diagnostic diagnostic in diagnostics )
92- {
93- var node = syntaxRoot . FindNode ( diagnostic . Location . SourceSpan , false , true ) as UsingDirectiveSyntax ;
94- if ( node == null || node . IsMissing )
95- {
96- continue ;
97- }
98-
99- replaceMap [ node ] = GenerateReplacementNode ( semanticModel , node , fixAllContext . CancellationToken ) ;
100- }
101-
102- return syntaxRoot . ReplaceNodes ( replaceMap . Keys , ( originalNode , rewrittenNode ) => replaceMap [ originalNode ] ) ;
91+ return syntaxRoot . ReplaceNodes ( nodes , ( originalNode , rewrittenNode ) => GetReplacementNode ( semanticModel , rewrittenNode , fixAllContext . CancellationToken ) ) ;
10392 }
10493 }
10594 }
0 commit comments