Skip to content

Commit 0fbbca3

Browse files
committed
Update SA1412FixAllProvider to use FixAllContextHelpers
1 parent 784d3cc commit 0fbbca3

1 file changed

Lines changed: 60 additions & 26 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412FixAllProvider.cs

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,111 @@
33

44
namespace StyleCop.Analyzers.MaintainabilityRules
55
{
6+
using System.Collections.Immutable;
7+
using System.Linq;
8+
using System.Threading;
69
using System.Threading.Tasks;
10+
using Helpers;
711
using Microsoft.CodeAnalysis;
812
using Microsoft.CodeAnalysis.CodeActions;
913
using Microsoft.CodeAnalysis.CodeFixes;
1014

1115
internal sealed class SA1412FixAllProvider : FixAllProvider
1216
{
13-
public override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
17+
public override Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
1418
{
15-
Solution newSolution;
19+
string title = string.Format(MaintainabilityResources.SA1412CodeFix, fixAllContext.CodeActionEquivalenceKey.Substring(fixAllContext.CodeActionEquivalenceKey.IndexOf('.') + 1));
1620

21+
CodeAction fixAction;
1722
switch (fixAllContext.Scope)
1823
{
1924
case FixAllScope.Document:
20-
newSolution = await FixDocumentAsync(fixAllContext, fixAllContext.Document).ConfigureAwait(false);
25+
fixAction = CodeAction.Create(
26+
title,
27+
cancellationToken => this.GetDocumentFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
28+
nameof(SA1412FixAllProvider));
2129
break;
2230

2331
case FixAllScope.Project:
24-
newSolution = await GetProjectFixesAsync(fixAllContext, fixAllContext.Project).ConfigureAwait(false);
32+
fixAction = CodeAction.Create(
33+
title,
34+
cancellationToken => this.GetProjectFixesAsync(fixAllContext.WithCancellationToken(cancellationToken), fixAllContext.Project),
35+
nameof(SA1412FixAllProvider));
2536
break;
2637

2738
case FixAllScope.Solution:
28-
newSolution = fixAllContext.Solution;
29-
var projectIds = newSolution.ProjectIds;
30-
for (int i = 0; i < projectIds.Count; i++)
31-
{
32-
newSolution = await GetProjectFixesAsync(fixAllContext, newSolution.GetProject(projectIds[i])).ConfigureAwait(false);
33-
}
34-
39+
fixAction = CodeAction.Create(
40+
title,
41+
cancellationToken => this.GetSolutionFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
42+
nameof(SA1412FixAllProvider));
3543
break;
3644

3745
case FixAllScope.Custom:
3846
default:
39-
return null;
47+
fixAction = null;
48+
break;
4049
}
4150

42-
return CodeAction.Create(
43-
string.Format(MaintainabilityResources.SA1412CodeFix, fixAllContext.CodeActionEquivalenceKey.Substring(fixAllContext.CodeActionEquivalenceKey.IndexOf('.') + 1)),
44-
token => Task.FromResult(newSolution));
51+
return Task.FromResult(fixAction);
4552
}
4653

47-
private static async Task<Solution> FixDocumentAsync(FixAllContext fixAllContext, Document document)
54+
private static async Task<Solution> FixDocumentAsync(Solution solution, DocumentId documentId, ImmutableArray<Diagnostic> diagnostics, string codeActionEquivalenceKey, CancellationToken cancellationToken)
4855
{
49-
Solution solution = document.Project.Solution;
50-
var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false);
51-
if (diagnostics.Length == 0)
56+
if (diagnostics.IsEmpty)
5257
{
5358
return solution;
5459
}
5560

5661
string equivalenceKey = nameof(SA1412CodeFixProvider) + "." + diagnostics[0].Properties[SA1412StoreFilesAsUtf8.EncodingProperty];
57-
if (fixAllContext.CodeActionEquivalenceKey != equivalenceKey)
62+
if (codeActionEquivalenceKey != equivalenceKey)
5863
{
5964
return solution;
6065
}
6166

62-
return await SA1412CodeFixProvider.GetTransformedSolutionAsync(document, fixAllContext.CancellationToken).ConfigureAwait(false);
67+
Document document = solution.GetDocument(documentId);
68+
return await SA1412CodeFixProvider.GetTransformedSolutionAsync(document, cancellationToken).ConfigureAwait(false);
6369
}
6470

65-
private static async Task<Solution> GetProjectFixesAsync(FixAllContext fixAllContext, Project project)
71+
private async Task<Solution> GetDocumentFixesAsync(FixAllContext fixAllContext)
6672
{
67-
Solution solution = project.Solution;
73+
var documentDiagnosticsToFix = await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(fixAllContext).ConfigureAwait(false);
74+
ImmutableArray<Diagnostic> diagnostics;
75+
if (!documentDiagnosticsToFix.TryGetValue(fixAllContext.Document, out diagnostics))
76+
{
77+
return fixAllContext.Document.Project.Solution;
78+
}
79+
80+
return await FixDocumentAsync(fixAllContext.Document.Project.Solution, fixAllContext.Document.Id, diagnostics, fixAllContext.CodeActionEquivalenceKey, fixAllContext.CancellationToken).ConfigureAwait(false);
81+
}
6882

69-
var documentIds = project.DocumentIds;
83+
private async Task<Solution> GetSolutionFixesAsync(FixAllContext fixAllContext, ImmutableArray<Document> documents)
84+
{
85+
var documentDiagnosticsToFix = await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(fixAllContext).ConfigureAwait(false);
7086

71-
foreach (var documentId in documentIds)
87+
Solution solution = fixAllContext.Solution;
88+
foreach (var document in documents)
7289
{
73-
solution = await FixDocumentAsync(fixAllContext, solution.GetDocument(documentId)).ConfigureAwait(false);
90+
ImmutableArray<Diagnostic> diagnostics;
91+
if (!documentDiagnosticsToFix.TryGetValue(document, out diagnostics))
92+
{
93+
continue;
94+
}
95+
96+
solution = await FixDocumentAsync(solution, document.Id, diagnostics, fixAllContext.CodeActionEquivalenceKey, fixAllContext.CancellationToken).ConfigureAwait(false);
7497
}
7598

7699
return solution;
77100
}
101+
102+
private Task<Solution> GetProjectFixesAsync(FixAllContext fixAllContext, Project project)
103+
{
104+
return this.GetSolutionFixesAsync(fixAllContext, project.Documents.ToImmutableArray());
105+
}
106+
107+
private Task<Solution> GetSolutionFixesAsync(FixAllContext fixAllContext)
108+
{
109+
ImmutableArray<Document> documents = fixAllContext.Solution.Projects.SelectMany(i => i.Documents).ToImmutableArray();
110+
return this.GetSolutionFixesAsync(fixAllContext, documents);
111+
}
78112
}
79113
}

0 commit comments

Comments
 (0)