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

Support TrySelectDiagnosticToFix for Fix All operations #871

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -695,24 +695,35 @@ private async Task VerifyProjectAsync(ProjectState newState, Project project, IV
return (project, ExceptionDispatchInfo.Capture(ex));
}

var fixableDiagnostics = analyzerDiagnostics
.Where(diagnostic => codeFixProviders.Any(provider => provider.FixableDiagnosticIds.Contains(diagnostic.diagnostic.Id)))
.Where(diagnostic => project.Solution.GetDocument(diagnostic.diagnostic.Location.SourceTree) is object)
.ToImmutableArray();

if (CodeFixTestBehaviors.HasFlag(CodeFixTestBehaviors.FixOne))
{
var diagnosticToFix = TrySelectDiagnosticToFix(fixableDiagnostics.Select(x => x.diagnostic).ToImmutableArray());
fixableDiagnostics = diagnosticToFix is object ? ImmutableArray.Create(fixableDiagnostics.Single(x => x.diagnostic == diagnosticToFix)) : ImmutableArray<(Project project, Diagnostic diagnostic)>.Empty;
}

Diagnostic? firstDiagnostic = null;
CodeFixProvider? effectiveCodeFixProvider = null;
string? equivalenceKey = null;
foreach (var (_, diagnostic) in analyzerDiagnostics)
foreach (var (_, diagnostic) in fixableDiagnostics)
{
var actions = new List<(CodeAction, CodeFixProvider)>();

var diagnosticDocument = project.Solution.GetDocument(diagnostic.Location.SourceTree);
foreach (var codeFixProvider in codeFixProviders)
{
if (!codeFixProvider.FixableDiagnosticIds.Contains(diagnostic.Id)
|| !(project.Solution.GetDocument(diagnostic.Location.SourceTree) is { } document))
if (!codeFixProvider.FixableDiagnosticIds.Contains(diagnostic.Id))
{
// do not pass unsupported diagnostics to a code fix provider
continue;
}

var actionsBuilder = ImmutableArray.CreateBuilder<CodeAction>();
var context = new CodeFixContext(document, diagnostic, (a, d) => actionsBuilder.Add(a), cancellationToken);
var context = new CodeFixContext(diagnosticDocument, diagnostic, (a, d) => actionsBuilder.Add(a), cancellationToken);
await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);
actions.AddRange(FilterCodeActions(actionsBuilder.ToImmutable()).Select(action => (action, codeFixProvider)));
}
Expand Down