Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FixAll support by using equivalence key when available #884

Merged
merged 1 commit into from Dec 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/Analyzers/SolutionCodeFixApplier.cs
Expand Up @@ -29,37 +29,57 @@ internal class SolutionCodeFixApplier : ICodeFixApplier
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