Skip to content

Commit

Permalink
Merge pull request #884 from JoeRobich/improve-fixall-support
Browse files Browse the repository at this point in the history
Improve FixAll support by using equivalence key when available
  • Loading branch information
JoeRobich authored Dec 2, 2020
2 parents 0b4ae80 + 4415afc commit 8fe6083
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/Analyzers/SolutionCodeFixApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,57 @@ public async Task<Solution> ApplyCodeFixesAsync(
return solution;
}

var document = result.Diagnostics
var diagnostic = result.Diagnostics
.SelectMany(kvp => kvp.Value)
.Select(diagnostic => solution.GetDocument(diagnostic.Location.SourceTree))
.Where(diagnostic => diagnostic.Location.SourceTree != null)
.FirstOrDefault();

if (diagnostic is null)
{
return solution;
}

var document = solution.GetDocument(diagnostic.Location.SourceTree);

if (document is null)
{
return solution;
}

CodeAction? action = null;
var context = new CodeFixContext(document, diagnostic,
(a, _) =>
{
if (action == null)
{
action = a;
}
},
cancellationToken);

await codeFix.RegisterCodeFixesAsync(context).ConfigureAwait(false);

var fixAllContext = new FixAllContext(
document: document,
codeFixProvider: codeFix,
scope: FixAllScope.Solution,
codeActionEquivalenceKey: null!, // FixAllState supports null equivalence key. This should still be supported.
codeActionEquivalenceKey: action?.EquivalenceKey!, // FixAllState supports null equivalence key. This should still be supported.
diagnosticIds: new[] { diagnosticId },
fixAllDiagnosticProvider: new DiagnosticProvider(result),
cancellationToken: cancellationToken);

try
{
var action = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);
if (action is null)
var fixAllAction = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);
if (fixAllAction is null)
{
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_didnt_return_a_Fix_All_action, diagnosticId, codeFix.GetType().Name);
return solution;
}

var operations = await action.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
var operations = await fixAllAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
var applyChangesOperation = operations.OfType<ApplyChangesOperation>().SingleOrDefault();
if (action is null)
if (applyChangesOperation is null)
{
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_returned_an_unexpected_operation, diagnosticId, codeFix.GetType().Name);
return solution;
Expand Down

0 comments on commit 8fe6083

Please sign in to comment.