Skip to content

Commit 4415afc

Browse files
committed
Improve FixAll support by using equivalence key when available
1 parent e5991c0 commit 4415afc

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/Analyzers/SolutionCodeFixApplier.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,57 @@ public async Task<Solution> ApplyCodeFixesAsync(
2929
return solution;
3030
}
3131

32-
var document = result.Diagnostics
32+
var diagnostic = result.Diagnostics
3333
.SelectMany(kvp => kvp.Value)
34-
.Select(diagnostic => solution.GetDocument(diagnostic.Location.SourceTree))
34+
.Where(diagnostic => diagnostic.Location.SourceTree != null)
3535
.FirstOrDefault();
3636

37+
if (diagnostic is null)
38+
{
39+
return solution;
40+
}
41+
42+
var document = solution.GetDocument(diagnostic.Location.SourceTree);
43+
3744
if (document is null)
3845
{
3946
return solution;
4047
}
4148

49+
CodeAction? action = null;
50+
var context = new CodeFixContext(document, diagnostic,
51+
(a, _) =>
52+
{
53+
if (action == null)
54+
{
55+
action = a;
56+
}
57+
},
58+
cancellationToken);
59+
60+
await codeFix.RegisterCodeFixesAsync(context).ConfigureAwait(false);
61+
4262
var fixAllContext = new FixAllContext(
4363
document: document,
4464
codeFixProvider: codeFix,
4565
scope: FixAllScope.Solution,
46-
codeActionEquivalenceKey: null!, // FixAllState supports null equivalence key. This should still be supported.
66+
codeActionEquivalenceKey: action?.EquivalenceKey!, // FixAllState supports null equivalence key. This should still be supported.
4767
diagnosticIds: new[] { diagnosticId },
4868
fixAllDiagnosticProvider: new DiagnosticProvider(result),
4969
cancellationToken: cancellationToken);
5070

5171
try
5272
{
53-
var action = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);
54-
if (action is null)
73+
var fixAllAction = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);
74+
if (fixAllAction is null)
5575
{
5676
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_didnt_return_a_Fix_All_action, diagnosticId, codeFix.GetType().Name);
5777
return solution;
5878
}
5979

60-
var operations = await action.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
80+
var operations = await fixAllAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
6181
var applyChangesOperation = operations.OfType<ApplyChangesOperation>().SingleOrDefault();
62-
if (action is null)
82+
if (applyChangesOperation is null)
6383
{
6484
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_returned_an_unexpected_operation, diagnosticId, codeFix.GetType().Name);
6585
return solution;

0 commit comments

Comments
 (0)