Skip to content

Commit 1a4de48

Browse files
authored
Merge pull request #41512 from CyrusNajmabadi/fixAllCleaup
Cleanup fix all code.
2 parents 67b582b + 51e6f7f commit 1a4de48

File tree

7 files changed

+70
-85
lines changed

7 files changed

+70
-85
lines changed

src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.FixAllProvider.cs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,34 @@ public async override Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
2626
return null;
2727
}
2828

29-
var batchFixer = WellKnownFixAllProviders.BatchFixer;
3029
var suppressionFixer = (AbstractSuppressionCodeFixProvider)((WrapperCodeFixProvider)fixAllContext.CodeFixProvider).SuppressionFixProvider;
31-
var isGlobalSuppression = NestedSuppressionCodeAction.IsEquivalenceKeyForGlobalSuppression(fixAllContext.CodeActionEquivalenceKey);
32-
if (!isGlobalSuppression)
33-
{
34-
var isPragmaWarningSuppression = NestedSuppressionCodeAction.IsEquivalenceKeyForPragmaWarning(fixAllContext.CodeActionEquivalenceKey);
35-
Contract.ThrowIfFalse(isPragmaWarningSuppression || NestedSuppressionCodeAction.IsEquivalenceKeyForRemoveSuppression(fixAllContext.CodeActionEquivalenceKey));
3630

37-
batchFixer = isPragmaWarningSuppression ?
38-
new PragmaWarningBatchFixAllProvider(suppressionFixer) :
39-
RemoveSuppressionCodeAction.GetBatchFixer(suppressionFixer);
31+
if (NestedSuppressionCodeAction.IsEquivalenceKeyForGlobalSuppression(fixAllContext.CodeActionEquivalenceKey))
32+
{
33+
// For global suppressions, we defer to the global suppression system to handle directly.
34+
var title = fixAllContext.CodeActionEquivalenceKey;
35+
return fixAllContext.Document != null
36+
? GlobalSuppressMessageFixAllCodeAction.Create(
37+
title, suppressionFixer, fixAllContext.Document,
38+
await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(false))
39+
: GlobalSuppressMessageFixAllCodeAction.Create(
40+
title, suppressionFixer, fixAllContext.Project,
41+
await fixAllContext.GetProjectDiagnosticsToFixAsync().ConfigureAwait(false));
4042
}
4143

42-
var title = fixAllContext.CodeActionEquivalenceKey;
43-
if (fixAllContext.Document != null)
44+
if (NestedSuppressionCodeAction.IsEquivalenceKeyForPragmaWarning(fixAllContext.CodeActionEquivalenceKey))
4445
{
45-
var documentsAndDiagnosticsToFixMap =
46-
await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(false);
47-
48-
return !isGlobalSuppression
49-
? await batchFixer.GetFixAsync(
50-
documentsAndDiagnosticsToFixMap, fixAllContext.State, fixAllContext.CancellationToken).ConfigureAwait(false)
51-
: GlobalSuppressMessageFixAllCodeAction.Create(title, suppressionFixer, fixAllContext.Document, documentsAndDiagnosticsToFixMap);
46+
var batchFixer = new PragmaWarningBatchFixAllProvider(suppressionFixer);
47+
return await batchFixer.GetFixAsync(fixAllContext).ConfigureAwait(false);
5248
}
53-
else
54-
{
55-
var projectsAndDiagnosticsToFixMap =
56-
await fixAllContext.GetProjectDiagnosticsToFixAsync().ConfigureAwait(false);
5749

58-
return !isGlobalSuppression
59-
? await batchFixer.GetFixAsync(
60-
projectsAndDiagnosticsToFixMap, fixAllContext.State, fixAllContext.CancellationToken).ConfigureAwait(false)
61-
: GlobalSuppressMessageFixAllCodeAction.Create(title, suppressionFixer, fixAllContext.Project, projectsAndDiagnosticsToFixMap);
50+
if (NestedSuppressionCodeAction.IsEquivalenceKeyForRemoveSuppression(fixAllContext.CodeActionEquivalenceKey))
51+
{
52+
var batchFixer = RemoveSuppressionCodeAction.GetBatchFixer(suppressionFixer);
53+
return await batchFixer.GetFixAsync(fixAllContext).ConfigureAwait(false);
6254
}
55+
56+
throw ExceptionUtilities.Unreachable;
6357
}
6458
}
6559
}

src/Workspaces/Core/Portable/CodeFixes/FixAllOccurrences/BatchFixAllProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
4646

4747
#endregion
4848

49-
internal override async Task<CodeAction> GetFixAsync(
49+
private async Task<CodeAction> GetFixAsync(
5050
ImmutableDictionary<Document, ImmutableArray<Diagnostic>> documentsAndDiagnosticsToFixMap,
5151
FixAllState fixAllState, CancellationToken cancellationToken)
5252
{
@@ -131,7 +131,7 @@ protected async virtual Task AddDocumentFixesAsync(
131131
await Task.WhenAll(fixerTasks).ConfigureAwait(false);
132132
}
133133

134-
internal override async Task<CodeAction> GetFixAsync(
134+
private async Task<CodeAction> GetFixAsync(
135135
ImmutableDictionary<Project, ImmutableArray<Diagnostic>> projectsAndDiagnosticsToFixMap,
136136
FixAllState fixAllState, CancellationToken cancellationToken)
137137
{

src/Workspaces/Core/Portable/CodeFixes/FixAllOccurrences/FixAllContext.DiagnosticProvider.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public partial class FixAllContext
2424
/// </summary>
2525
public abstract class DiagnosticProvider
2626
{
27-
internal virtual bool IsFixMultiple => false;
28-
2927
/// <summary>
3028
/// Gets all the diagnostics to fix in the given document in a <see cref="FixAllContext"/>.
3129
/// </summary>
@@ -42,27 +40,30 @@ public abstract class DiagnosticProvider
4240
/// </summary>
4341
public abstract Task<IEnumerable<Diagnostic>> GetAllDiagnosticsAsync(Project project, CancellationToken cancellationToken);
4442

45-
internal async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixAsync(
46-
FixAllContext fixAllContext)
43+
internal async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixAsync(FixAllContext fixAllContext)
4744
{
4845
var result = await GetDocumentDiagnosticsToFixWorkerAsync(fixAllContext).ConfigureAwait(false);
4946

5047
// Filter out any documents that we don't have any diagnostics for.
5148
return result.Where(kvp => !kvp.Value.IsDefaultOrEmpty).ToImmutableDictionary();
52-
}
5349

54-
internal virtual async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixWorkerAsync(
55-
FixAllContext fixAllContext)
56-
{
57-
using (Logger.LogBlock(
58-
FunctionId.CodeFixes_FixAllOccurrencesComputation_Document_Diagnostics,
59-
FixAllLogger.CreateCorrelationLogMessage(fixAllContext.State.CorrelationId),
60-
fixAllContext.CancellationToken))
50+
static async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixWorkerAsync(FixAllContext fixAllContext)
6151
{
62-
return await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(
63-
fixAllContext,
64-
fixAllContext.ProgressTracker,
65-
(document, cancellationToken) => document.IsGeneratedCode(cancellationToken)).ConfigureAwait(false);
52+
if (fixAllContext.State.DiagnosticProvider is FixAllState.FixMultipleDiagnosticProvider fixMultipleDiagnosticProvider)
53+
{
54+
return fixMultipleDiagnosticProvider.DocumentDiagnosticsMap;
55+
}
56+
57+
using (Logger.LogBlock(
58+
FunctionId.CodeFixes_FixAllOccurrencesComputation_Document_Diagnostics,
59+
FixAllLogger.CreateCorrelationLogMessage(fixAllContext.State.CorrelationId),
60+
fixAllContext.CancellationToken))
61+
{
62+
return await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(
63+
fixAllContext,
64+
fixAllContext.ProgressTracker,
65+
(d, c) => DocumentExtensions.IsGeneratedCode(d, c)).ConfigureAwait(false);
66+
}
6667
}
6768
}
6869

src/Workspaces/Core/Portable/CodeFixes/FixAllOccurrences/FixAllProvider.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,5 @@ public virtual IEnumerable<string> GetSupportedFixAllDiagnosticIds(CodeFixProvid
4242
/// Gets fix all occurrences fix for the given fixAllContext.
4343
/// </summary>
4444
public abstract Task<CodeAction> GetFixAsync(FixAllContext fixAllContext);
45-
46-
internal virtual Task<CodeAction> GetFixAsync(
47-
ImmutableDictionary<Document, ImmutableArray<Diagnostic>> documentsAndDiagnosticsToFixMap,
48-
FixAllState fixAllState, CancellationToken cancellationToken)
49-
{
50-
return Task.FromResult<CodeAction>(null);
51-
}
52-
53-
internal virtual Task<CodeAction> GetFixAsync(
54-
ImmutableDictionary<Project, ImmutableArray<Diagnostic>> projectsAndDiagnosticsToFixMap,
55-
FixAllState fixAllState, CancellationToken cancellationToken)
56-
{
57-
return Task.FromResult<CodeAction>(null);
58-
}
5945
}
6046
}

src/Workspaces/Core/Portable/CodeFixes/FixAllOccurrences/FixAllState.FixMultipleDiagnosticProvider.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,37 @@ internal partial class FixAllState
1818
/// </summary>
1919
internal sealed class FixMultipleDiagnosticProvider : FixAllContext.DiagnosticProvider
2020
{
21-
internal override bool IsFixMultiple => true;
22-
23-
private readonly ImmutableDictionary<Document, ImmutableArray<Diagnostic>> _documentDiagnosticsMap;
24-
private readonly ImmutableDictionary<Project, ImmutableArray<Diagnostic>> _projectDiagnosticsMap;
21+
public ImmutableDictionary<Document, ImmutableArray<Diagnostic>> DocumentDiagnosticsMap { get; }
22+
public ImmutableDictionary<Project, ImmutableArray<Diagnostic>> ProjectDiagnosticsMap { get; }
2523

2624
public FixMultipleDiagnosticProvider(ImmutableDictionary<Document, ImmutableArray<Diagnostic>> diagnosticsMap)
2725
{
28-
_documentDiagnosticsMap = diagnosticsMap;
29-
_projectDiagnosticsMap = ImmutableDictionary<Project, ImmutableArray<Diagnostic>>.Empty;
26+
DocumentDiagnosticsMap = diagnosticsMap;
27+
ProjectDiagnosticsMap = ImmutableDictionary<Project, ImmutableArray<Diagnostic>>.Empty;
3028
}
3129

3230
public FixMultipleDiagnosticProvider(ImmutableDictionary<Project, ImmutableArray<Diagnostic>> diagnosticsMap)
3331
{
34-
_projectDiagnosticsMap = diagnosticsMap;
35-
_documentDiagnosticsMap = ImmutableDictionary<Document, ImmutableArray<Diagnostic>>.Empty;
36-
}
37-
38-
internal override Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixWorkerAsync(
39-
FixAllContext context)
40-
{
41-
return Task.FromResult(_documentDiagnosticsMap);
42-
}
43-
44-
internal override Task<ImmutableDictionary<Project, ImmutableArray<Diagnostic>>> GetProjectDiagnosticsToFixAsync(
45-
FixAllContext context)
46-
{
47-
return Task.FromResult(_projectDiagnosticsMap);
32+
ProjectDiagnosticsMap = diagnosticsMap;
33+
DocumentDiagnosticsMap = ImmutableDictionary<Document, ImmutableArray<Diagnostic>>.Empty;
4834
}
4935

5036
public override Task<IEnumerable<Diagnostic>> GetAllDiagnosticsAsync(Project project, CancellationToken cancellationToken)
5137
{
5238
var allDiagnosticsBuilder = ArrayBuilder<Diagnostic>.GetInstance();
5339
ImmutableArray<Diagnostic> diagnostics;
54-
if (!_documentDiagnosticsMap.IsEmpty)
40+
if (!DocumentDiagnosticsMap.IsEmpty)
5541
{
5642
foreach (var document in project.Documents)
5743
{
58-
if (_documentDiagnosticsMap.TryGetValue(document, out diagnostics))
44+
if (DocumentDiagnosticsMap.TryGetValue(document, out diagnostics))
5945
{
6046
allDiagnosticsBuilder.AddRange(diagnostics);
6147
}
6248
}
6349
}
6450

65-
if (_projectDiagnosticsMap.TryGetValue(project, out diagnostics))
51+
if (ProjectDiagnosticsMap.TryGetValue(project, out diagnostics))
6652
{
6753
allDiagnosticsBuilder.AddRange(diagnostics);
6854
}
@@ -72,7 +58,7 @@ public override Task<IEnumerable<Diagnostic>> GetAllDiagnosticsAsync(Project pro
7258

7359
public override Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
7460
{
75-
if (_documentDiagnosticsMap.TryGetValue(document, out var diagnostics))
61+
if (DocumentDiagnosticsMap.TryGetValue(document, out var diagnostics))
7662
{
7763
return Task.FromResult<IEnumerable<Diagnostic>>(diagnostics);
7864
}
@@ -82,7 +68,7 @@ public override Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Docume
8268

8369
public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)
8470
{
85-
if (_projectDiagnosticsMap.TryGetValue(project, out var diagnostics))
71+
if (ProjectDiagnosticsMap.TryGetValue(project, out var diagnostics))
8672
{
8773
return Task.FromResult<IEnumerable<Diagnostic>>(diagnostics);
8874
}

src/Workspaces/Core/Portable/CodeFixes/FixAllOccurrences/FixAllState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private FixAllState(
9191
this.DiagnosticProvider = fixAllDiagnosticProvider ?? throw new ArgumentNullException(nameof(fixAllDiagnosticProvider));
9292
}
9393

94-
internal bool IsFixMultiple => this.DiagnosticProvider.IsFixMultiple;
94+
internal bool IsFixMultiple => this.DiagnosticProvider is FixMultipleDiagnosticProvider;
9595

9696
public FixAllState WithScopeAndEquivalenceKey(FixAllScope scope, string codeActionEquivalenceKey)
9797
{

src/Workspaces/Core/Portable/CodeFixes/SyntaxEditorBasedCodeFixProvider.FixAllProvider.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.CodeActions;
11+
using Microsoft.CodeAnalysis.CodeStyle;
12+
using Microsoft.CodeAnalysis.Shared.Extensions;
1113

1214
namespace Microsoft.CodeAnalysis.CodeFixes
1315
{
@@ -34,7 +36,23 @@ public sealed override async Task<CodeAction> GetFixAsync(FixAllContext fixAllCo
3436
return await GetFixAsync(documentsAndDiagnosticsToFixMap, fixAllContext.State, fixAllContext.CancellationToken).ConfigureAwait(false);
3537
}
3638

37-
internal sealed override async Task<CodeAction> GetFixAsync(
39+
private async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixAsync(FixAllContext fixAllContext)
40+
{
41+
var result = await GetDocumentDiagnosticsToFixWorkerAsync(fixAllContext).ConfigureAwait(false);
42+
43+
// Filter out any documents that we don't have any diagnostics for.
44+
return result.Where(kvp => !kvp.Value.IsDefaultOrEmpty).ToImmutableDictionary();
45+
46+
static async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixWorkerAsync(FixAllContext fixAllContext)
47+
{
48+
return await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(
49+
fixAllContext,
50+
progressTrackerOpt: null,
51+
(d, c) => DocumentExtensions.IsGeneratedCode(d, c)).ConfigureAwait(false);
52+
}
53+
}
54+
55+
private async Task<CodeAction> GetFixAsync(
3856
ImmutableDictionary<Document, ImmutableArray<Diagnostic>> documentsAndDiagnosticsToFixMap,
3957
FixAllState fixAllState, CancellationToken cancellationToken)
4058
{

0 commit comments

Comments
 (0)