Skip to content

Commit

Permalink
Merge pull request #60317 from CyrusNajmabadi/tsFixer
Browse files Browse the repository at this point in the history
Fix issue when multiple different diagnostics share a single fixer
  • Loading branch information
CyrusNajmabadi committed Mar 23, 2022
2 parents df7da42 + 8395cf4 commit 3fb22f1
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,13 @@ private async IAsyncEnumerable<CodeFixCollection> StreamFixesAsync(
var isInteractive = document.Project.Solution.Workspace.Kind == WorkspaceKind.Interactive;

// gather CodeFixProviders for all distinct diagnostics found for current span
using var _1 = ArrayBuilder<CodeFixProvider>.GetInstance(out var allFixers);
using var _2 = PooledDictionary<CodeFixProvider, List<(TextSpan range, List<DiagnosticData> diagnostics)>>.GetInstance(out var fixerToRangesAndDiagnostics);
using var _1 = PooledDictionary<CodeFixProvider, List<(TextSpan range, List<DiagnosticData> diagnostics)>>.GetInstance(out var fixerToRangesAndDiagnostics);
using var _2 = PooledHashSet<CodeFixProvider>.GetInstance(out var currentFixers);

foreach (var (range, diagnostics) in spanToDiagnostics)
{
currentFixers.Clear();

foreach (var diagnosticId in diagnostics.Select(d => d.Id).Distinct())
{
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -385,8 +387,9 @@ private async IAsyncEnumerable<CodeFixCollection> StreamFixesAsync(
}

// Now, sort the fixers so that the ones that are ordered before others get their chance to run first.
if (allFixers.Count >= 2 && TryGetWorkspaceFixersPriorityMap(document, out var fixersForLanguage))
allFixers.Sort(new FixerComparer(allFixers, fixersForLanguage.Value));
var allFixers = fixerToRangesAndDiagnostics.Keys.ToImmutableArray();
if (TryGetWorkspaceFixersPriorityMap(document, out var fixersForLanguage))
allFixers = allFixers.Sort(new FixerComparer(allFixers, fixersForLanguage.Value));

var extensionManager = document.Project.Solution.Workspace.Services.GetService<IExtensionManager>();

Expand Down Expand Up @@ -468,11 +471,8 @@ void AddAllFixers(
{
foreach (var fixer in fixers)
{
if (allFixers.Contains(fixer))
continue;

allFixers.Add(fixer);
fixerToRangesAndDiagnostics.MultiAdd(fixer, (range, diagnostics));
if (currentFixers.Add(fixer))
fixerToRangesAndDiagnostics.MultiAdd(fixer, (range, diagnostics));
}
}
}
Expand Down Expand Up @@ -965,7 +965,7 @@ private sealed class FixerComparer : IComparer<CodeFixProvider>
private readonly ImmutableDictionary<CodeFixProvider, int> _priorityMap;

public FixerComparer(
ArrayBuilder<CodeFixProvider> allFixers,
ImmutableArray<CodeFixProvider> allFixers,
ImmutableDictionary<CodeFixProvider, int> priorityMap)
{
_fixerToIndex = allFixers.Select((fixer, index) => (fixer, index)).ToDictionary(t => t.fixer, t => t.index);
Expand Down

0 comments on commit 3fb22f1

Please sign in to comment.